Open In App

Determine position of two points with respect to a 3D plane

Improve
Improve
Like Article
Like
Save
Share
Report

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 3D
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))
        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 Code
int 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 approach
public 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 3D
def 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 Code
if __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 approach
using 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 code
static 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 3D
function 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 Input
var a = 1, b = 2, c = 3, d = 4;
 
// Coordinates of points
var x1 = -2, y1 = -2, z1 = 1;
var 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 29AjayKumar
 
</script>


Output: 

On same side

 

Time Complexity: O(1)
Auxiliary Space: O(1)

 



Last Updated : 20 Oct, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads