Sign in to follow this  
Element of Power

2D Thick Lines

Recommended Posts

I'm trying to develop a sand game, like PyroSand or Burning Sand, in C++, and I'm running into problems when it comes to drawing. I've tried drawing a box at the mouse's position, but that doesn't look right because it leaves gaps, so I need a thick line function, which should be as optimised as possible. What I've tried: This uses Bresenham's algorithm, and places a box at each point - this works, but is quite slow, since it draws a whole box at each point.
void Particle::drawThickLine(ParticleType *ept, int width, int x1, int y1, int x2, int y2)
{
	bool steep = abs(y2 - y1) > abs(x2 - x1);
	int hw = width/2;

	if (steep)
	{
		swap(x1, y1);
		swap(x2, y2);
	}
	if (x1 > x2)
	{
		swap(x1, x2);
		swap(y1, y2);
	}

	int dx	= x2 - x1;
	int dy	= abs(y2 - y1);
	int e	= dx/2;
	int ys	= y1 < y2 ? 1 : -1;
	int y	= y1;

	for(int x = x1; x < x2; x ++)
	{
		if (steep) new Particle(ept, y-hw, x-hw, y+hw, x+hw); else drawBox(ept, x-hw, y-hw, x+hw, y+hw);
		e -= dy;
		if (e < 0)
		{
			y += ys;
			e += dx;
		}
	}
}
I tried to make a "parallel line" algorithm, but it screwed up on perfectly diagonal lines, so I scrapped it. A more efficient version of the above would be great, if anyone knows how to do this stuff.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this