Results 1 to 4 of 4

Thread: Rectangle's coordinates

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2002
    Posts
    65

    Rectangle's coordinates

    I would like to accept (x,y) coordinate values from the user and decide if the four coordinates form a rectangle.

    So I will accept (x1,y1), (x2,y2), (x3,y3) and (x4,y4). The user will just input these in no particular order. What will be the logic to find if these four coordinates can form a rectangle.

    I think it is relatively easy, if we can ask the user to enter the left lower, right lower, right upper and left upper coordinates in the order, i.e. (x1,y1)....(x4,y4). Then it is a matter of finding if pairs of sides are equal.

    By the way, how do we determine if the lines are going to be at right angles :-)))

    Just throw in your ideas (VB code will be appreciated too :-)) )

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    if the rectangle has to be parallel to the screen it's easy:

    the x value of two points have to be equal to each other, and the x points of the other two must also be equal to each other (but not the same as the first.

    it's essentially the same for the Y (except that the two points that are equal y must have different x values)



    if the rectangle can be at any angle to the screen then it's more awkward...

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jun 2002
    Posts
    65
    Yup more in line with the logic I had in mind.

    Any other suggestions?

  4. #4
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141
    This is what I came up with real quick. See if it gives you some ideas.
    VB Code:
    1. '   Define Corners 1, 2, 3, and 4
    2. '   Find the lengths of
    3. '       Side 1_2
    4. '       Side 2_3
    5. '       Side 3_4
    6. '       Side 4_1
    7. '       Diagonal 1_3
    8. '       Diagonal 2_4
    9. '
    10. '   1-----------2
    11. '   |           |
    12. '   |           |
    13. '   |           |
    14. '   4-----------3
    15. '
    16. '   If Side 1_2 = Side 3_4 and Side 4_1 = Side 2_3 Then
    17. '       Either Square, parallagram or Rectangle
    18. '   If Diagonal 1_3 = Diagonal 2_4 Then Rectangle or Square
    19. '       Else parallagram
    20. Option Explicit
    21.  
    22. Private Type Corners
    23.     X As Integer
    24.     Y As Integer
    25. End Type
    26.  
    27. Dim Corner(1 To 4) As Corners
    28. Dim Side(1 To 4) As Single
    29. Dim Diagonal(1) As Single
    30.  
    31. Private Sub Form_Load()
    32.  
    33.     Me.AutoRedraw = True
    34.     Me.Show
    35.    
    36. '   Define the corners of the rectangle
    37.     Corner(1).X = 400
    38.     Corner(1).Y = 400
    39.    
    40.     Corner(2).X = 2400
    41.     Corner(2).Y = 200
    42.    
    43.     Corner(3).X = 2500
    44.     Corner(3).Y = 1200
    45.    
    46.     Corner(4).X = 500
    47.     Corner(4).Y = 1400
    48.    
    49. '   Draw the rectangle
    50.     Me.Line (Corner(1).X, Corner(1).Y)-(Corner(2).X, Corner(2).Y)
    51.     Me.Line -(Corner(3).X, Corner(3).Y)
    52.     Me.Line -(Corner(4).X, Corner(4).Y)
    53.     Me.Line -(Corner(1).X, Corner(1).Y)
    54.    
    55. '   Get the length of each side
    56.     Side(1) = GetLength(Corner(1), Corner(2)) ' Side 1_2
    57.     Side(2) = GetLength(Corner(3), Corner(4)) ' Side 3_4
    58.     Side(3) = GetLength(Corner(2), Corner(3)) ' Side 2_3
    59.     Side(4) = GetLength(Corner(4), Corner(1)) ' Side 4_1
    60.    
    61. '   Get Diagonal Lengths
    62.     Diagonal(0) = GetLength(Corner(1), Corner(3))
    63.     Diagonal(1) = GetLength(Corner(2), Corner(4))
    64.    
    65. End Sub
    66.  
    67. Private Function GetLength(FirstCorner As Corners, SecondCorner As Corners) As Single
    68. Dim A As Long
    69. Dim B As Long
    70. Dim C As Long
    71.  
    72.  
    73.     A = Abs(FirstCorner.X - SecondCorner.X)
    74.     B = Abs(FirstCorner.Y - SecondCorner.Y)
    75.    
    76.     C = A ^ 2 + B ^ 2
    77.    
    78.     GetLength = Sqr(C)
    79.    
    80. End Function
    Last edited by MarkT; May 27th, 2003 at 04:03 PM.

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