forked from williamfiset/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CoplanarPoints.java
93 lines (77 loc) · 2.79 KB
/
CoplanarPoints.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/**
* This file shows you how to determine if four 3D points lie in the same plane as each other.
*
* <p>Time Complexity: O(1)
*
* @author William Fiset, william.alexandre.fiset@gmail.com
*/
package com.williamfiset.algorithms.geometry;
import static java.lang.Math.*;
public class CoplanarPoints {
private static final double EPS = 1e-7;
// A simple 3D vector class
public static class Vector {
double x, y, z;
public Vector(double xx, double yy, double zz) {
x = xx;
y = yy;
z = zz;
}
}
// Determine is four points (ax,ay,az), (bx,by,bz), (cx,cy,cz),
// (dx,dy,dz) all lie in the same plane in 3D space
public static boolean coplanar(
double ax,
double ay,
double az,
double bx,
double by,
double bz,
double cx,
double cy,
double cz,
double dx,
double dy,
double dz) {
// The problem of testing if four points are coplanar can be converted
// into a problem a checking if two vectors are orthogonal which is something
// the dot product is excellent at (if the dot product of two vectors is zero
// then they are orthogonal!)
//
// First, consider the three vectors spanning outwards from say point 'a':
// v1 = b-a, v2 = c-a, and v3 = d-a. If we take the cross product of any
// of these vectors (say v1 and v2) then we can obtain a fourth vector (v4)
// normal to the plane in which v1 and v2 live, so if the point 'd' lies in
// the same plane then the dot product of v3 and v4 should be 0
Vector v1 = new Vector(bx - ax, by - ay, bz - az);
Vector v2 = new Vector(cx - ax, cy - ay, cz - az);
Vector v3 = new Vector(dx - ax, dy - ay, dz - az);
// Find the vector normal (perpendicular) to v1 and v2
Vector v4 = cross(v1, v2);
// Check whether to dot product of v3 and v4 is zero
return abs(dot(v3, v4)) < EPS;
}
// Cross product of two vectors
private static Vector cross(Vector v1, Vector v2) {
double v3x = v1.y * v2.z - v1.z * v2.y;
double v3y = v1.z * v2.x - v1.x * v2.z;
double v3z = v1.x * v2.y - v1.y * v2.x;
return new Vector(v3x, v3y, v3z);
}
// 3D vector dot product
private static double dot(Vector v1, Vector v2) {
return (v1.x * v2.x) + (v1.y * v2.y) + (v1.z * v2.z);
}
// Examples
public static void main(String[] args) {
System.out.println(
"The points (0,0,0), (1,0,1), (0,1,1), (1,1,2) are coplanar: "
+ coplanar(0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1, 2));
System.out.println(
"The points (0,0,0), (3,3,3), (3,0,0), (0,4,0) are coplanar: "
+ coplanar(0, 0, 0, 3, 3, 3, 3, 0, 0, 0, 4, 0));
System.out.println(
"The points (0,0,0), (1,1,1), (2,2,2), (3,3,3) are coplanar: "
+ coplanar(0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3));
}
}