Jump to content

  • Log In with Google      Sign In   
  • Create Account

Line drawing? (C#)


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 Nyxenon   Members   -  Reputation: 104

Like
0Likes
Like

Posted 23 January 2012 - 03:53 PM

I'm working on a 2D game, and I need to make a map editor. I've made map editors in the past, but there has always been a problem. When you're drawing, if you swipe the mouse across the screen, it will skip large chunks of space. So what I want to know how to do, essentially, is draw a line.

I want to be able to take 2 points and fill every point that touches the line between those two points. Once I know how to do that, I can implement it in my map editor. Does anyone know any good resources that tell how to do this, or maybe some sample code?

Sponsor:

#2 SimonForsman   Crossbones+   -  Reputation: 6324

Like
0Likes
Like

Posted 23 January 2012 - 04:08 PM

I'm working on a 2D game, and I need to make a map editor. I've made map editors in the past, but there has always been a problem. When you're drawing, if you swipe the mouse across the screen, it will skip large chunks of space. So what I want to know how to do, essentially, is draw a line.

I want to be able to take 2 points and fill every point that touches the line between those two points. Once I know how to do that, I can implement it in my map editor. Does anyone know any good resources that tell how to do this, or maybe some sample code?


Most 2D APIs will have functions to draw lines given two points, what API are you using for the editor ? (For normal C# forms you can use the DrawLine method in System.Drawing.Graphics)
I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!

#3 Nyxenon   Members   -  Reputation: 104

Like
0Likes
Like

Posted 23 January 2012 - 04:22 PM


I'm working on a 2D game, and I need to make a map editor. I've made map editors in the past, but there has always been a problem. When you're drawing, if you swipe the mouse across the screen, it will skip large chunks of space. So what I want to know how to do, essentially, is draw a line.

I want to be able to take 2 points and fill every point that touches the line between those two points. Once I know how to do that, I can implement it in my map editor. Does anyone know any good resources that tell how to do this, or maybe some sample code?


Most 2D APIs will have functions to draw lines given two points, what API are you using for the editor ? (For normal C# forms you can use the DrawLine method in System.Drawing.Graphics)


Actually, I'm not using an API. I'm using Xna, and I need to set tiles along a line between two points on the map. I figured it out though.

		private void DrawTileLine(Point p1, Point p2,PointU16 tile)
		{
			Vector2 dir = new Vector2(p2.X - p1.X, p2.Y - p1.Y);
			float len = dir.Length();
			Vector2 st = new Vector2(p1.X, p1.Y);
			Point cur = new Point(p1.X, p1.Y);
			dir.Normalize();
			float i = 0f;
			while (i <= len)
			{
				cur.X = (int)st.X;
				cur.Y = (int)st.Y;
				tileMap.SetTile(cur.X, cur.Y, tile);
				st += dir;
				i++;
			}
		}


#4 roadysix   Members   -  Reputation: 290

Like
0Likes
Like

Posted 23 January 2012 - 04:58 PM

To plot a points along a line segment is fairly easy with linear interpolation:

lerp(a, b, t) : a + t(b - a)

stepwise instructions:
* calcualte and store that vectors length :: √x² + y²
* then in a loop that goes from 0 to the vectors length rounded to the nearest integer create whatever needs to be plotted along the line and position them using the linear interpolation function, like so;
x = lerp(p1.x, p2.x, (float)i / length);
y = lerp(p1.y, p2.y, (float)i / length);

something like that.

Regards

#5 MarkS   Prime Members   -  Reputation: 887

Like
1Likes
Like

Posted 25 January 2012 - 01:18 PM

This is what I've used in the past for my tile level editors. Just modify it with your own map variables.

This is based in a modified version of Bresenham's classic line drawing code.



#define ABS(x) (x < 0 ? -x : x)

#define SIGN(x) ((x) < 0 ? -1 : ((x) > 0 ? 1 : 0))



void LineTool(TileMap *map,POINT p1,POINT p2,long tile_ID)

{

long y,x;

long dy,dx;

long sx,sy;

POINT start,end;

long accum;



start.x = p1.x;

start.y = p1.y;

end.x = p2.x;

end.y = p2.y;



dx = end.x - start.x;

dy = end.y - start.y;



sx = SIGN(dx);

sy = SIGN(dy);



dx = ABS(dx);

dy = ABS(dy);



end.x += sx;

end.y += sy;



if(dx > dy)

{

accum = dx >> 1;

do{

map->map[start.y][start.x] = tile_ID;



accum -= dy;

if(accum < 0)

{

accum += dx;

start.y += sy;

}

start.x += sx;

}while(start.x != end.x);

}else{

accum = dy >> 1;

do{

map->map[start.y][start.x] = tile_ID;



accum -= dx;

if(accum < 0)

{

accum += dy;

start.x += sx;

}

start.y += sy;

}while(start.y != end.y);

}

}



#6 Nyxenon   Members   -  Reputation: 104

Like
1Likes
Like

Posted 26 January 2012 - 10:14 AM

That's a great method, but I noticed that your x and y variables are unused.

#7 MarkS   Prime Members   -  Reputation: 887

Like
0Likes
Like

Posted 27 January 2012 - 12:44 AM

That's a great method, but I noticed that your x and y variables are unused.


lol! I hadn't noticed that. Through several revisions and changes, the x/y coords got replaced with start.x/y. In fact, I'm not too sure why I was doing that!Posted Image

Let's try this again...

#define ABS(x) (x < 0 ? -x : x)
#define SIGN(x) ((x) < 0 ? -1 : ((x) > 0 ? 1 : 0))

void LineTool(TileMap *map,POINT p1,POINT p2,long tile_ID)
{
long y,x;
long dy,dx;
long sx,sy;
long accum;

x = p1.x;
y = p1.y;

dx = p2.x - p1.x;
dy = p2.y - p1.y;

sx = SIGN(dx);
sy = SIGN(dy);

dx = ABS(dx);
dy = ABS(dy);

p2.x += sx;
p2.y += sy;

if(dx > dy)
{
accum = dx >> 1;
do{
map->map[y][x] = tile_ID;

accum -= dy;
if(accum < 0)
{
accum += dx;
y += sy;
}
x += sx;
}while(x != p2.x);
}else{
accum = dy >> 1;
do{
map->map[y][x] = tile_ID;

accum -= dx;
if(accum < 0)
{
accum += dy;
x += sx;
}
y += sy;
}while(y != p2.y);
}
}


#8 Nyxenon   Members   -  Reputation: 104

Like
0Likes
Like

Posted 27 January 2012 - 06:31 AM

I still managed to get the code to work how I need it to. However, I had an unusual error. When I ported the code to my game, I messed up a variable name:

while(start.x != end.x);

Became:

while(start.x != end.y);

Needless to say, my game froze up, and it took me a while to figure out what it was. Then I decided to step through my code when I noticed that the x value was WAY too high. I got it working though, so for that, I thank you. I probably could have written myself given enough thought, but I prefer to see how other people do things rather than inventing way (like I did above), that may or may not be efficient.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS