Jump to content
  • Advertisement
Sign in to follow this  
Element of Power

2D Thick Lines

This topic is 3044 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!