
How can I determine if four points form a rectangle?
As the title says, I'm trying to determine if there's an algorithm to take four X,Y points and determine if they form a rectangle.
Currently, I have a somewhat convoluted ifelse based function where I compare if the first two points are on the same X or Y plane and continue through the remaining points with that same kind of logic until I decide if it forms a rectangle. This seems to be working but it seems awfully clunky.
Does anyone know of a more elegant/efficient approach? Thanks in advance!

Modulator
The problem with that method is it will only work if the rectangle sides are parallel to the axes i.e.:
Code:
_______________________
*******************
*******************
*******************
*******************
_______________________
But what if its not, i.e.
Code:
................____________
............../**********/
............/**********/
........../**********/
......../**********/
....../___________/
Note: This is actually a parallelogram.
I can't draw a tilted rectangles using ASCII characters TT_TT
This is a different method:
 Arrange the points into an array e.g.(ShapePoints[]);
 Get the gradient of the lines that connect each point (with a four pointed object, there should be 6 gradients). Put this in another array e.g.(ShapeGrad[]);
 Check each grad(gradient) against each other, and see if any match (there should be two sets that match). This removes the two gradient lines that crossover within the rectangle.
 Finally, multiply the gradients by their respective lengths, and add them. The result should be 0.
NOTE: this shows that it's a parallelogram of any sorts including rhombus', squares and rectangles. If you want to check if the corners are at rightangles, you need to take two sides of different lengths and dotproduct them (or something like that...can't remember off the top of my head ^_^;; )
Also note, that I cannot guarantee that this will work, nor that it will shorten the code. But if it does work (which I can't see any reason why not), then it will work for all rectangles, not just ones parallel to the x,yaxes.
As a Gamer, you play by the rules.
As a Programmer, you play as God.

All angles must be 90 degrees in a rectangle by definition. So you can just use the dotproduct to detect if your point set is a rectangle.
Loop through all points and compute vector P[i1], P[i] and P[i], P[i+1] = v0, v1. Compute dot product. If zero (or very close < float errors), then the three points are in right angle.
If not, you may have 2 points, that are in opposite order (diagonal). Skip them and try the next point.
You need 4 dotproduct equal zero to make sure, that you have a rectangle, even a rotated one.
Posting Permissions
 You may not post new threads
 You may not post replies
 You may not post attachments
 You may not edit your posts

Forum Rules

Click Here to Expand Forum to Full Width
