Given three points p1, p2 and p3 that define a plane in 3D space, determine whether a fourth point p4 lies above, below, or on that plane.
"Above" is defined as the side in the direction of the normal vector computed as (p2 - p1) x (p3 - p1).
Return 1 if the point is above the plane, -1 if it is below, and 0 if it lies on the plane.
Function Signature
def orientation(p1: list[int], p2: list[int], p3: list[int], p4: list[int]) -> int:
Rules
-
Each point is a list
[x, y, z]
.
-
The cross product is the standard right-handed one: for vectors
u
and
v
,
u x v = (u[1]*v[2] - u[2]*v[1], u[2]*v[0] - u[0]*v[2], u[0]*v[1] - u[1]*v[0])
. The order matters, so swapping
p2
and
p3
flips which side counts as above.
-
Return exactly
1
,
-1
or
0
.
Constraints
-
Each point is a list of 3 integers.
-
-10^4 <= x, y, z <= 10^4
-
p1
,
p2
and
p3
are not collinear, so they always define a valid plane.
Examples
Input: p1 = [0, 0, 0], p2 = [1, 0, 0], p3 = [0, 1, 0], p4 = [0, 0, 1]
Output: 1
The plane is the xy-plane (z = 0), and the normal (1, 0, 0) x (0, 1, 0) = (0, 0, 1) points in the +z direction. p4 = [0, 0, 1] is on that side, so the result is 1.
Input: p1 = [0, 0, 0], p2 = [1, 0, 0], p3 = [0, 1, 0], p4 = [0, 0, -1]
Output: -1
Same plane and normal as Example 1. p4 = [0, 0, -1] is on the side opposite the normal, so the result is -1.
Input: p1 = [1, 2, 3], p2 = [2, 3, 3], p3 = [1, 2, 5], p4 = [3, 4, 7]
Output: 0
Measured from p1, the other points are (1, 1, 0), (0, 0, 2) and (2, 2, 4). The last one is twice the first plus twice the second, so p4 lies in the plane. The normal is (2, -2, 0), and its dot product with (2, 2, 4) is 4 - 4 + 0 = 0.