Results 1 to 1 of 1
 09192013, 03:27 AM #1Senior Member
 Join Date
 Jan 2009
 Location
 CA, USA
 Posts
 271
 Rep Power
 12
Bresenham's Line Algorithm to Get Points Along Line Segment
I'm developing an application where each agent has a "line of sight" on an image. I attempted to implement Bresenham's line algorithm to identify each point along the line. However, I am also wondering if there are any better alternatives? Since I will have many agents in the application at a given time, I'm looking for efficient solutions. Any additional info is appreciated. ^_^
Here's what I'm doing at the moment...
Java Code:import java.util.ArrayList; import java.util.List; public class LineSegmentHelper { public static List<Coordinate> getPointsInLine(int x1, int y1, int x2, int y2) { final List<Coordinate> coords = new ArrayList<>(); final int dx = Math.abs(x2  x1); final int dy = Math.abs(y2  y1); final int sx = x1 < x2 ? 1 : 1; final int sy = y1 < y2 ? 1 : 1; // Verticle Line if(dx == 0) { while(true) { final Coordinate c = new Coordinate(x1, y1); if(!coords.contains(c)) coords.add(c); if(y1 == y2) break; y1 += sy; } } // Horizontal Line else if(dy == 0) { while(true) { final Coordinate c = new Coordinate(x1, y1); if(!coords.contains(c)) coords.add(c); if(x1 == x2) break; x1 += sx; } } // Other Line else { int err = dx  dy; while(true) { final Coordinate c = new Coordinate(x1, y1); if(!coords.contains(c)) coords.add(c); if(x1 == x2  y1 == y2) break; int e2 = 2 * err; if(e2 > dy) { err = dy; x1 += sx; } if(e2 < dx) { err += dx; y1 += sy; } } } return coords; } }
Similar Threads

Points in a line
By mrguy in forum Java GamingReplies: 7Last Post: 07032012, 04:29 PM 
Open a URL and read it line by line (Works in Eclipse but not from Command Line)
By rosco544 in forum NetworkingReplies: 16Last Post: 09172011, 02:41 AM 
How to move the points on a predrawn line?
By pizzadude223 in forum Java AppletsReplies: 1Last Post: 07202010, 12:56 PM 
Formatting java command line output  Multi line string
By dricco in forum New To JavaReplies: 2Last Post: 07022010, 02:20 PM 
given number of points(cordinates) , find max points lie on the same line ?
By Hayzam in forum New To JavaReplies: 2Last Post: 08242008, 12:30 AM
Bookmarks