Iam doing a java swing application. In that i need to get points along the line. Can you please help me with this?
Please help me :(
Printable View
Iam doing a java swing application. In that i need to get points along the line. Can you please help me with this?
Please help me :(
I have the following code with me. But sometimes it won't give me correct points
public static Point[] PointsAlongLine(Point start, Point end, int spacing) {
int xDifference = end.x - start.x;
int yDifference = end.y - start.y;
int absoluteXdifference = Math.abs(start.x - end.x);
int absoluteYdifference = Math.abs(start.y - end.y);
int lineLength = (int)Math.sqrt((Math.pow(absoluteXdifference, 2) + Math.pow(absoluteYdifference, 2))); //pythagoras
int steps = lineLength / spacing;
int xStep = xDifference / steps;
int yStep = yDifference / steps;
Point[] result = new Point[steps+1];
for (int i = 0; i < steps; i++) {
int x = start.x + (xStep * i);
int y = start.y + (yStep * i);
result[i] = new Point(x, y);
}
result[steps]=end;
return result;
}
Can you post the results of the program and explain what is wrong with them and show what the results should be?