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.
Examples:
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: Â
C++
// C++ program for the above approach#include <iostream>using namespace std;Â
// Function to check position of two// points with respect to a plane in 3Dvoid check_position(int a, int b, int c, int d,                    int x1, int y1, int z1,                    int x2, int y2, int z2){    // Put coordinates in plane equation    int value_1 = a * x1 + b * y1 + c * z1 + d;    int value_2 = a * x2 + b * y2 + c * z2 + d;Â
    // If both values have same sign    if ((value_1 > 0 && value_2 > 0)        || (value_1 < 0 && value_2 < 0))        cout << "On same side";Â
    // If both values have different sign    if ((value_1 > 0 && value_2 < 0)        || (value_1 < 0 && value_2 > 0))        cout << "On different sides";Â
    // If both values are zero    if (value_1 == 0 && value_2 == 0)        cout << "Both on the plane";Â
    // If either of the two values is zero    if (value_1 == 0 && value_2 != 0)        cout << "Point 1 on the plane";    if (value_1 != 0 && value_2 == 0)        cout << "Point 2 on the plane";}Â
// Driver Codeint main(){Â
    // Given Input    int a = 1, b = 2, c = 3, d = 4;Â
    // Coordinates of points    int x1 = -2, y1 = -2, z1 = 1;    int x2 = -4, y2 = 11, z2 = -1;Â
    // Function Call    check_position(a, b, c, d,                   x1, y1, z1,                   x2, y2, z2);Â
    return 0;} |
Java
// Java program for the above approachpublic class GFG {Â
    // Function to check position of two    // points with respect to a plane in 3D    static void check_position(int a, int b, int c, int d,                               int x1, int y1, int z1,                               int x2, int y2, int z2)    {        // Put coordinates in plane equation        int value_1 = a * x1 + b * y1 + c * z1 + d;        int value_2 = a * x2 + b * y2 + c * z2 + d;Â
        // If both values have same sign        if ((value_1 > 0 && value_2 > 0)            || (value_1 < 0 && value_2 < 0))            System.out.print("On same side");Â
        // If both values have different sign        if ((value_1 > 0 && value_2 < 0)            || (value_1 < 0 && value_2 > 0))            System.out.print("On different sides");Â
        // If both values are zero        if (value_1 == 0 && value_2 == 0)            System.out.print("Both on the plane");Â
        // If either of the two values is zero        if (value_1 == 0 && value_2 != 0)            System.out.print("Point 1 on the plane");        if (value_1 != 0 && value_2 == 0)            System.out.print("Point 2 on the plane");    }Â
    // Driver code    public static void main(String[] args)    {        // Given Input        int a = 1, b = 2, c = 3, d = 4;Â
        // Coordinates of points        int x1 = -2, y1 = -2, z1 = 1;        int x2 = -4, y2 = 11, z2 = -1;Â
        // Function Call        check_position(a, b, c, d, x1, y1, z1, x2, y2, z2);    }}Â
// This code is contributed by sk944795. |
Python3
# Python3 program for the above approachÂ
# Function to check position of two# points with respect to a plane in 3Ddef check_position(a, b, c, d, x1, y1,                        z1, x2, y2, z2):                            # Put coordinates in plane equation    value_1 = a * x1 + b * y1 + c * z1 + d    value_2 = a * x2 + b * y2 + c * z2 + dÂ
    # If both values have same sign    if ((value_1 > 0 and value_2 > 0) or        (value_1 < 0 and value_2 < 0)):        print("On same side")Â
    # If both values have different sign    if ((value_1 > 0 and value_2 < 0) or        (value_1 < 0 and value_2 > 0)):        print("On different sides")Â
    # If both values are zero    if (value_1 == 0 and value_2 == 0):        print("Both on the plane")Â
    # If either of the two values is zero    if (value_1 == 0 and value_2 != 0):        print("Point 1 on the plane")    if (value_1 != 0 and value_2 == 0):        print("Point 2 on the plane")Â
# Driver Codeif __name__ == '__main__':Â
    # Given Input    a, b, c, d = 1, 2, 3, 4Â
    # Coordinates of points    x1, y1, z1 = -2, -2, 1    x2, y2, z2 = -4, 11, -1Â
    # Function Call    check_position(a, b, c, d, x1,                    y1, z1, x2, y2, z2)Â
# This code is contributed by mohit kumar 29 |
C#
// C# program for the above approachusing System;Â
class GFG{         // Function to check position of two    // points with respect to a plane in 3D    static void check_position(int a, int b, int c, int d,                               int x1, int y1, int z1,                               int x2, int y2, int z2)    {        // Put coordinates in plane equation        int value_1 = a * x1 + b * y1 + c * z1 + d;        int value_2 = a * x2 + b * y2 + c * z2 + d;          // If both values have same sign        if ((value_1 > 0 && value_2 > 0)            || (value_1 < 0 && value_2 < 0))            Console.Write("On same side");          // If both values have different sign        if ((value_1 > 0 && value_2 < 0)            || (value_1 < 0 && value_2 > 0))            Console.Write("On different sides");          // If both values are zero        if (value_1 == 0 && value_2 == 0)            Console.Write("Both on the plane");          // If either of the two values is zero        if (value_1 == 0 && value_2 != 0)           Console.Write("Point 1 on the plane");        if (value_1 != 0 && value_2 == 0)            Console.Write("Point 2 on the plane");    }Â
// Driver codestatic void Main(){    // Given Input        int a = 1, b = 2, c = 3, d = 4;          // Coordinates of points        int x1 = -2, y1 = -2, z1 = 1;        int x2 = -4, y2 = 11, z2 = -1;          // Function Call        check_position(a, b, c, d, x1, y1, z1, x2, y2, z2);     }}Â
// This code is contributed by sanjoy_62. |
Javascript
<script>Â
// JavaScript program for the above approachÂ
// Function to check position of two// points with respect to a plane in 3Dfunction check_position(a , b , c , d,                           x1 , y1 , z1,                           x2 , y2 , z2){    // Put coordinates in plane equation    var value_1 = a * x1 + b * y1 + c * z1 + d;    var value_2 = a * x2 + b * y2 + c * z2 + d;Â
    // If both values have same sign    if ((value_1 > 0 && value_2 > 0)        || (value_1 < 0 && value_2 < 0))        document.write("On same side");Â
    // If both values have different sign    if ((value_1 > 0 && value_2 < 0)        || (value_1 < 0 && value_2 > 0))        document.write("On different sides");Â
    // If both values are zero    if (value_1 == 0 && value_2 == 0)        document.write("Both on the plane");Â
    // If either of the two values is zero    if (value_1 == 0 && value_2 != 0)        document.write("Point 1 on the plane");    if (value_1 != 0 && value_2 == 0)        document.write("Point 2 on the plane");}Â
// Driver codeÂ
// Given Inputvar a = 1, b = 2, c = 3, d = 4;Â
// Coordinates of pointsvar x1 = -2, y1 = -2, z1 = 1;var x2 = -4, y2 = 11, z2 = -1;Â
// Function Callcheck_position(a, b, c, d, x1, y1, z1, x2, y2, z2);Â
// This code is contributed by 29AjayKumar Â
</script> |
On same side
Â
Time Complexity: O(1)
Auxiliary Space: O(1)
Â
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



