**0**

# Line drawing? (C#)

###
#1
Members - Reputation: **104**

Posted 23 January 2012 - 03:53 PM

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?

###
#2
Crossbones+ - Reputation: **6921**

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!

The voices in my head may not be real, but they have some good ideas!

###
#3
Members - Reputation: **104**

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,

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
Members - Reputation: **290**

Posted 23 January 2012 - 04:58 PM

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
GDNet+ - Reputation: **1975**

Posted 25 January 2012 - 01:18 PM

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); } }

###
#7
GDNet+ - Reputation: **1975**

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!

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
Members - Reputation: **104**

Posted 27 January 2012 - 06:31 AM

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.