Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.
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.
Posted 23 January 2012 - 03:53 PM
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?
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)
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++; } }
Posted 23 January 2012 - 04:58 PM
Posted 25 January 2012 - 01:18 PM
#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); } }
Posted 27 January 2012 - 12:44 AM
That's a great method, but I noticed that your x and y variables are unused.
#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); } }
Posted 27 January 2012 - 06:31 AM
while(start.x != end.x);
while(start.x != end.y);
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.
GameDev.net™, the GameDev.net logo, and GDNet™ are trademarks of GameDev.net, LLC.