Line drawing? (C#)

Started by
6 comments, last by derek_of_bodom@hotmail.com 12 years, 2 months ago
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?
Advertisement

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)
[size="1"]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!

[quote name='Nyxenon' timestamp='1327355592' post='4905581']
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)
[/quote]

Actually, [s]I'm not using an API[/s]. 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++;
}
}
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
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.
[source]

#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);
}
}
[/source]
That's a great method, but I noticed that your x and y variables are unused.

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!blink.png

Let's try this again...

[source]
#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);
}
}
[/source]
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.

This topic is closed to new replies.

Advertisement