Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Help applying line algorithm  (Read 2583 times)

0 Members and 1 Guest are viewing this topic.

Austin J

  • Newbie
  • *
  • Posts: 29
    • View Profile
    • Email
Help applying line algorithm
« on: February 13, 2014, 01:44:02 am »
  I really wish I could just solve this myself but working with non-Cartesian coordinate system plus a few other things can confuse the hell out of me :(

  I'm working on a map editor that I want to be able to handle quite a few tiles. I've already set it up so that it won't render anything that is outside the view of the window. I can also get the tile the mouse is on by some simple math. (Use the mouses position to know what the index should be). However holding the mouse and moving it more than medium speed will still leave many gaps. I've been wanting to use a line algorithm to solve this, however I'm confusing the hell out of myself I feel like.

  Current implementation I have going it seems like it sort of does what its supposed to, but horribly. If I move the mouse just somewhat fast it's not that big a deal but if I move the mouse very fast it does odd things like this.



The most top right tile and the very bottom left tile, I went in a mostly straight diagonal line between the two, never releasing the mouse. So yeah what I'm doing now definitely doesn't work.

Here's the code I've got trying to deal with this. Note that I'm not worried about views yet, it's not on my to do list currently.

void Map::handleClick(sf::RenderWindow & WINDOW,std::string tile)
{
        sf::Vector2i mp = sf::Mouse::getPosition(WINDOW);
        sf::Vector2f mousePosition = static_cast<sf::Vector2f>(mp);
        if(lastTile != NULL) // When the mouse is released lastTile gets reset to NULL
        {
                int x= lastTile->getPosition().x;
                int y = lastTile->getPosition().y;
                int x2 = Map::getTileByCoords(mousePosition).getPosition().x;
                int y2 = Map::getTileByCoords(mousePosition).getPosition().y;
                int dx = x - x2; int dy = y - y2;
                for(int i = x/16; i != x2/16;)
                {
                        int cy = (i*(dy/dx))*16;
                        Map::assignTile(Map::getTileByCoords(i*16,y),tile);
                        if(i > x2/16)
                        {
                                i --;
                        }
                        else{ i++; }
                }
        }
        Map::assignTile(Map::getTileByCoords(mousePosition),tile);
        Map::lastTile = &Map::getTileByCoords(mousePosition);
}
 



Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Help applying line algorithm
« Reply #1 on: February 13, 2014, 02:03:58 am »
You could try interpolating the mouse positions using something like this.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Austin J

  • Newbie
  • *
  • Posts: 29
    • View Profile
    • Email
Re: Help applying line algorithm
« Reply #2 on: February 13, 2014, 02:29:40 am »
Nice, it even has an example using SFML type grid. Thanks, wish I had seen this before.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Help applying line algorithm
« Reply #3 on: February 13, 2014, 02:48:55 am »
You're welcome.
Good luck with it. Looks too complicated for me  ;D
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Mercy404

  • Guest
Re: Help applying line algorithm
« Reply #4 on: February 15, 2014, 12:34:53 am »
I would also recommend Bresenham's Line Algorithm, but I found this resource to be a lot easier to wrap my head around the first time I implemented it.

 

anything