I’m having difficulty formulating this elegantly into an algorithm.
So I have a given a straight-edged shape (ie. square, though in the end shape doesn’t matter only endpoints). I get the bounding endpoints on a Cartesian coordinate system : (2,-2) (2,2) (-2,2) (-2,-2)
I’m given an arbitrary number of points (ie. 7) and I want to spread these points (x,y) uniformly along the edges of the shape (in this case a square).
My current idea is to get the total length of all endpoints, divide this by the number of points to get a segment length (which I then normalize against an edge). Then I go from endpoint to endpoint finding the point between by this amount and accrue the normalized slice, when this total exceeds 1.0 I iterate the endpoint and take the remainder and start from there…or something like that.
Could someone help me put this into an algorithm (C# preferably) or if you have a better solution please do tell. I’d imagine there is a sorting or distribution/division algorithm that could have the same affect, but I couldn’t find any. I hope this isn’t blatantly obvious.
How general does this need to be? Also, how are representing your shape, and the points? Your algorithm seems to be ok; do you need help turning it into code?
Alrighty, here’s something i came up with.
Notes on the code:
I’m using the Point class to represent vectors by the way.
I haven’t tested this, so there might be bugs. There might be issues with how this algorithm handles exact regions (e.g. your square with exactly 4 points on it). Let me know if there are issues or if you have any questions! 🙂