Results 1 to 6 of 6

Thread: Fourth vertex of a Rectangle

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2010
    Posts
    68

    Fourth vertex of a Rectangle

    Hi

    If 3 Vertices' xy coordinates are given from a rectangle, how to calculate the fourth one's xy?
    The rectangle could be in any position.

    Thanks.

  2. #2
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431

    Re: Fourth vertex of a Rectangle

    First you need to orient the three vertices. After that it's very simple.

    Consider the three vertices as three points in a triangle, A, B, and C. We need to figure out which vertex corresponds to the right angle. You can compute the angle from a point P to points Q and R as

    angle(P, Q, R) = Acos[(Q-P) dot (R-P) / [|Q-P| * |R-P|]]

    where Acos is the inverse cosine, (Q-P) etc. is vector subtraction (subtract componentwise), |Q-P| etc. is the magnitude of a vector (sum the squares of the components and take the square root--this is the content of the Pythagorean theorem), and (Q-P) dot (R-P) is the dot product (multiply the x components together, then the y components, etc. and add the results).

    Compute angle(A, B, C), angle(B, C, A), and angle(C, A, B). One of these should be very, very close to 90 degrees (pi/2 radians). If it's the first one, the vertex is at A; the second gives a vertex at B, and the third a vertex at C.

    Now suppose the vertex is at P and the other two points are Q and R. The fourth point on the rectangle is at Q + (R-P).
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jan 2010
    Posts
    68

    Re: Fourth vertex of a Rectangle

    Thanks, really good idea to calculate 90 angle. But write in VB takes time.

  4. #4
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431

    Re: Fourth vertex of a Rectangle

    If you need help with coding my idea, you can ask in the appropriate forum (the .NET or VB6 ones depending on your language). If you need a formula expanded or explained you can ask here in the maths forum.
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jan 2010
    Posts
    68

    Re: Fourth vertex of a Rectangle

    Ye, I will try to code first.
    Thanks

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Jan 2010
    Posts
    68

    Re: Fourth vertex of a Rectangle

    Code:
    Partial Public Class Window2
    
    	Public Sub New()
    
    		' This call is required by the Windows Form Designer.
    		InitializeComponent()
    
    		' Add any initialization after the InitializeComponent() call.
    		
    	End Sub
    	Private Sub msg()
    		Dim Point1 As Point = New Point(-1, -1)
    		Dim Point2 As Point = New Point(-4, -1)
    		Dim Point3 As Point = New Point(-1, -5)
    		Dim PointWithBiggestAngle As Integer = GetPointOnBiggestAngle(Point1, Point2, Point3)
    		Dim P, Q, R As Point
    		Select Case PointWithBiggestAngle
    			Case 1
    				P = Point1
    				Q = Point2
    				R = Point3
    			Case 2
    				P = Point2
    				Q = Point1
    				R = Point3
    			Case 3
    				P = Point3
    				Q = Point2
    				R = Point1
    		End Select
    		Dim Point4X As Long = Q.X + (R.X - P.X)
    		Dim Point4Y As Long = Q.Y + (R.Y - P.Y)
    		Debug.WriteLine(Point4X & " " & Point4Y)
    	End Sub
    
    	Private Sub Window2_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
    		msg()
    	End Sub
    	Private Function GetPointOnBiggestAngle(ByVal P As Point, ByVal Q As Point, ByVal R As Point) As Integer
    		Dim AngleOfP As Double = GetAngle(P, Q, R)
    		Dim AngleOfQ As Double = GetAngle(Q, P, R)
    		Dim AngleOfR As Double = GetAngle(R, Q, P)
    		If AngleOfP > AngleOfQ AndAlso AngleOfP > AngleOfR Then
    			Return 1
    		ElseIf AngleOfQ > AngleOfP AndAlso AngleOfQ > AngleOfR Then
    			Return 2
    		ElseIf AngleOfR > AngleOfP AndAlso AngleOfR > AngleOfQ Then
    			Return 3
    		Else
    			Return 0
    		End If
    	End Function
    	Private Function GetAngle(ByVal anglePoint As Point, ByVal Q As Point, ByVal R As Point) As Double
    		Dim a As Double = Math.Sqrt((Q.X - R.X) ^ 2 + (Q.Y - R.Y) ^ 2)	'anglePoint's opposite side length
    		Dim b As Double = Math.Sqrt((anglePoint.X - R.X) ^ 2 + (anglePoint.Y - R.Y) ^ 2)	' Q's opposite side length
    		Dim c As Double = Math.Sqrt((Q.X - anglePoint.X) ^ 2 + (Q.Y - anglePoint.Y) ^ 2)
    		Return Math.Acos((b ^ 2 + c ^ 2 - a ^ 2) / (2 * b * c))
    	End Function
    End Class
    Last edited by sharethl; Mar 30th, 2012 at 09:19 AM.

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