I’m trying to divide a line into multiple segments. Because of that i’ve created a function below to try get sub points between two points. It’s not quite working, but almost there. Ideally, i’d like to use it something like this:
subPoint(point1, point2, 5, 10); // this would return the half way point
subPoint(point1, point2, 1, 10); // this would return a point 1 tenth towrds point2
Here’s the rough code below – any tips or pointers very welcome.
Vector subPoint(Vector startPoint, Vector endPoint, int segment, int totalSegments) {
int division = (int)(totalSegments / segment);
PVector divPoint = new PVector();
int midX=(int)(startPoint.x+((endPoint.x-startPoint.x)/division));
int midY=(int)(startPoint.y+((endPoint.y-startPoint.y)/division));
divPoint.set(midX, midY, 0);
return(divPoint);
}
Try replacing your calculations with the following:
I transformed the values to doubles in order to make the calculations a little more accurate. You may want to consider keeping the values as doubles as that will give you better accuracy on line segments.