Determine position of two points with respect to a 3D plane
Given four integers a, b, c, and d, which represents the coefficient of the equation of the plane ax + by + cz + d = 0 and two integer coordinates (x1, y1, z1) and (x2, y2, z2), the task is to find whether both the points lie on the same side, or on different sides, or on the surface of the plane.
Input: a = 1, b = 2, c = 3, d = 4, x1 = -2, y1 = -2, z1 = 1, x2 = -4, y2 = 11, z2 = -1
Output: On same side
Explanation: On applying (x1, y1, z1) and (x2, y2, z2) on ax+by+cz+d=0 gives 1 and 19 respectively, both of which have the same sign, hence both the point lies on the same side of the plane.
Input: a = 4, b = 2, c = 1, d = 3, x1 = -2, y1 = -2, z1 = 1, x2 = -4, y2 = 11, z2 = -1
Output: On different sides
Approach: The idea is based on the fact that if the two points applied to the equation have the same parity (sign), then they will lie on the same side of the plane, and if they have different parity then they will lie on the different sides of the plane. Follow the steps below to solve the problem:
- Put the coordinates of the given points in the equation of plane and store the values in variables P1 and P2.
- Check the sign of the obtained values:
- If P1 and P2 have the same parity, then they are on the same side of the plane.
- If P1 and P2 have different parity then they lie on the opposite sides of the plane.
- If P1 and P2 are zero, then they lie on the plane.
Below is the implementation of the above approach:
On same side
Time Complexity: O(1)
Auxiliary Space: O(1)