Evenly Space Points On Line
-
Hey, I have a List of Points that makes up a line getting from users input. What I would like to do is take the list and create a new list of points where all the points are evenly spaced with an equal, or set distance apart. Does anyone know of the best way to do this, or any good resources to look at. I've searched around but not been able to find what i'm looking for. Thanks
-- John http://blog.yeticode.co.uk
-
Hey, I have a List of Points that makes up a line getting from users input. What I would like to do is take the list and create a new list of points where all the points are evenly spaced with an equal, or set distance apart. Does anyone know of the best way to do this, or any good resources to look at. I've searched around but not been able to find what i'm looking for. Thanks
-- John http://blog.yeticode.co.uk
How about this.
void AddMidpoints(List<Point> points) {
for (int i = points.Count - 1; i >= 1; i -= 1) {
points.Insert(i, midpoint(points[i], points[i - 1]));
}
}
Point midpoint(Point p1, Point p2) {
return new Point(((p1.X + p2.X) / 2), ((p1.Y + p2.Y) / 2));
} -
How about this.
void AddMidpoints(List<Point> points) {
for (int i = points.Count - 1; i >= 1; i -= 1) {
points.Insert(i, midpoint(points[i], points[i - 1]));
}
}
Point midpoint(Point p1, Point p2) {
return new Point(((p1.X + p2.X) / 2), ((p1.Y + p2.Y) / 2));
}I understood the question as taking a line created by a number of points and creating a new set of points that construct the same line, but with the same number of points evenly distributed across the line. If that is the case, here is a solution. Theoretically, all you need to do is calculate the length of the line and divide it into even segments according to the number of points provided by the user. Where P1 and P2 are the end points of the line, and n is the number of points provided by the user; you can calculate an
x offset
as(| P1.X - P2.X |) / n
and ay offset
as(| P1.Y - P2.Y |) / n
. You can then construct a collection of points containing the first and last point provided by the user along with an additional n - 2 points (indices 1...n-1) where the X coordinate =P1.X + (index * x offset)
and the Y coordinate =P1.Y + (index * y offset)
. Pseudocode:N = userPoints.Count
P1 = userPoints[0]
P2 = userPoints[N - 1]
xOffset = Abs(P1.X - P2.X) / N
yOffset = Abs(P1.Y - P2.Y) / N
List newPoints = new List(N)
newPoints.Add(P1)For I = 1; I < N - 1; I++
newPoints.Add(new Point(P1.X + I * xOffset, P1.Y + I * yOffset))newPoints.Add(P2)
Keep It Simple Stupid! (KISS)
-
I understood the question as taking a line created by a number of points and creating a new set of points that construct the same line, but with the same number of points evenly distributed across the line. If that is the case, here is a solution. Theoretically, all you need to do is calculate the length of the line and divide it into even segments according to the number of points provided by the user. Where P1 and P2 are the end points of the line, and n is the number of points provided by the user; you can calculate an
x offset
as(| P1.X - P2.X |) / n
and ay offset
as(| P1.Y - P2.Y |) / n
. You can then construct a collection of points containing the first and last point provided by the user along with an additional n - 2 points (indices 1...n-1) where the X coordinate =P1.X + (index * x offset)
and the Y coordinate =P1.Y + (index * y offset)
. Pseudocode:N = userPoints.Count
P1 = userPoints[0]
P2 = userPoints[N - 1]
xOffset = Abs(P1.X - P2.X) / N
yOffset = Abs(P1.Y - P2.Y) / N
List newPoints = new List(N)
newPoints.Add(P1)For I = 1; I < N - 1; I++
newPoints.Add(new Point(P1.X + I * xOffset, P1.Y + I * yOffset))newPoints.Add(P2)
Keep It Simple Stupid! (KISS)
Hi, I admit your approach is very KISS, however I doubt this is the right answer. You are assuming it is a straight line, but then why would the OP start of with a collection of points to define the line? It looks more like: here is a number of points, imagine a fluent line that goes through all of them and come up with new point that lie on that fluent line but now are equally spaced. So you might try some Bezier curves and calculate lengths on those (something I haven't encountered before). :)
Luc Pattyn [Forum Guidelines] [My Articles]
Fixturized forever. :confused: