A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: How can I determine if four points form a rectangle?

  1. #1
    Junior Member
    Join Date
    Mar 2004
    Posts
    19

    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 if-else 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!

  2. #2
    Modulator Katnap Kaos's Avatar
    Join Date
    Nov 2003
    Location
    A shoebox outside of SM Megamall
    Posts
    149
    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:

    1. Arrange the points into an array e.g.(ShapePoints[]);
    2. 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[]);
    3. 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 cross-over within the rectangle.
    4. 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 right-angles, you need to take two sides of different lengths and dot-product 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,y-axes.
    As a Gamer, you play by the rules.
    As a Programmer, you play as God.

  3. #3
    #
    Join Date
    Apr 2002
    Location
    berlin - germany
    Posts
    107
    All angles must be 90 degrees in a rectangle by definition. So you can just use the dot-product to detect if your point set is a rectangle.

    Loop through all points and compute vector P[i-1], 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 dot-product 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
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center