Bibin Lukose
Bibin Lukose

Reputation: 112

Check if 4 points in space are corner points of a rectangle

I have 4 points in space A(x,y,z), B(x,y,z), C(x,y,z) and D(x,y,z). How can I check if these points are the corner points of a rectangle?

Upvotes: 3

Views: 1799

Answers (3)

pk_11
pk_11

Reputation: 41

Here a function is defined to check whether the 4 points represents the rectangle or not .

from math import sqrt

def Verify(A, B, C, D, epsilon=0.0001):
    # Verify A-B = D-C
    zero = sqrt( (A[0]-B[0]+C[0]-D[0])**2 + (A[1]-B[1]+C[1]-D[1])**2 + (A[2]-B[2]+C[2]-D[2])**2 )
    if zero > epsilon:
        raise ValueError("Points do not form a parallelogram; C is at %g distance from where it should be" % zero)

    # Verify (D-A).(B-A) = 0
    zero = (D[0]-A[0])*(B[0]-A[0]) + (D[1]-A[1])*(B[1]-A[1]) + (D[2]-A[2])*(B[2]-A[2])
    if abs(zero) > epsilon:
        raise ValueError("Corner A is not a right angle; edge vector dot product is %g" % zero)

    else:
        print('rectangle')
        
A = [x1,y1,z1]
print(A)
B = [x2,y2,z2]
C = [x3,y3,z3]
D = [x4,y4,z4]
Verify(A, B, C, D, epsilon=0.0001)

Upvotes: 0

Lutz Lehmann
Lutz Lehmann

Reputation: 26040

Check if V1=B-A and V2=D-A are orthogonal using the dot product. Then check if

C-A == V1+V2

within numerical tolerances. If both are true, the points are coplanar and form a rectangle.

Upvotes: 0

andand
andand

Reputation: 17507

You must first determine whether or not the points are all coplanar, since a rectangle is a 2D geometric object, but your points are in 3-space. You can determine they are coplanar by comparing cross products as in:

V1 = (B-A)×(B-C)
V2 = (C-A)×(C-D)

This will give you two vectors which, if A, B, C, and D are coplanar are linearly dependent. By considering what Wolfram has to say on vector dependence, we can test the vectors for linear dependence by using

C = (V1∙V1)(V2∙V2) - (V1∙V2)(V2∙V1)

If C is 0 then the vectors V1 and V2 are linearly dependent and all the points are coplanar.

Next compute the distances between each pair of points. There should be a total of 6 such distances.

D1 = |A-B|
D2 = |A-C|
D3 = |A-D|
D4 = |B-C|
D5 = |B-D|
D6 = |C-D|

Assuming none of these distances are 0, these points form a rectangle if and only if the vertices are coplanar (already verified) and these lengths can be grouped into three pairs where elements of each pair have the same length. If the figure is a square, two sets of the pairs will have be the same length and will be shorter than the remaining pair.

Update: Reading this again, I realize the the above could define a parallelogram, so an additional check is required to check that the square of the longest distance is equal to the sum of the squares of the two shorter distances. Only then will the parallelogram also be a rectangle.

Keep in mind all of this is assuming infinite precision and within a strictly mathematical construct. If you're planning to code this up, you will need to account for rounding and accept a degree of imprecision that's not really a player when speaking in purely mathematical terms.

Upvotes: 4

Related Questions