|
-
May 27th, 2003, 11:22 AM
#1
Thread Starter
Lively Member
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 :-)) )
-
May 27th, 2003, 11:38 AM
#2
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...
-
May 27th, 2003, 12:53 PM
#3
Thread Starter
Lively Member
Yup more in line with the logic I had in mind.
Any other suggestions?
-
May 27th, 2003, 03:57 PM
#4
This is what I came up with real quick. See if it gives you some ideas.
VB Code:
' Define Corners 1, 2, 3, and 4
' Find the lengths of
' Side 1_2
' Side 2_3
' Side 3_4
' Side 4_1
' Diagonal 1_3
' Diagonal 2_4
'
' 1-----------2
' | |
' | |
' | |
' 4-----------3
'
' If Side 1_2 = Side 3_4 and Side 4_1 = Side 2_3 Then
' Either Square, parallagram or Rectangle
' If Diagonal 1_3 = Diagonal 2_4 Then Rectangle or Square
' Else parallagram
Option Explicit
Private Type Corners
X As Integer
Y As Integer
End Type
Dim Corner(1 To 4) As Corners
Dim Side(1 To 4) As Single
Dim Diagonal(1) As Single
Private Sub Form_Load()
Me.AutoRedraw = True
Me.Show
' Define the corners of the rectangle
Corner(1).X = 400
Corner(1).Y = 400
Corner(2).X = 2400
Corner(2).Y = 200
Corner(3).X = 2500
Corner(3).Y = 1200
Corner(4).X = 500
Corner(4).Y = 1400
' Draw the rectangle
Me.Line (Corner(1).X, Corner(1).Y)-(Corner(2).X, Corner(2).Y)
Me.Line -(Corner(3).X, Corner(3).Y)
Me.Line -(Corner(4).X, Corner(4).Y)
Me.Line -(Corner(1).X, Corner(1).Y)
' Get the length of each side
Side(1) = GetLength(Corner(1), Corner(2)) ' Side 1_2
Side(2) = GetLength(Corner(3), Corner(4)) ' Side 3_4
Side(3) = GetLength(Corner(2), Corner(3)) ' Side 2_3
Side(4) = GetLength(Corner(4), Corner(1)) ' Side 4_1
' Get Diagonal Lengths
Diagonal(0) = GetLength(Corner(1), Corner(3))
Diagonal(1) = GetLength(Corner(2), Corner(4))
End Sub
Private Function GetLength(FirstCorner As Corners, SecondCorner As Corners) As Single
Dim A As Long
Dim B As Long
Dim C As Long
A = Abs(FirstCorner.X - SecondCorner.X)
B = Abs(FirstCorner.Y - SecondCorner.Y)
C = A ^ 2 + B ^ 2
GetLength = Sqr(C)
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|