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++
#include <iostream>
using namespace std;
void check_position( int a, int b, int c, int d,
int x1, int y1, int z1,
int x2, int y2, int z2)
{
int value_1 = a * x1 + b * y1 + c * z1 + d;
int value_2 = a * x2 + b * y2 + c * z2 + d;
if ((value_1 > 0 && value_2 > 0)
|| (value_1 < 0 && value_2 < 0))
cout << "On same side" ;
if ((value_1 > 0 && value_2 < 0)
|| (value_1 < 0 && value_2 > 0))
cout << "On different sides" ;
if (value_1 == 0 && value_2 == 0)
cout << "Both on the plane" ;
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" ;
}
int main()
{
int a = 1, b = 2, c = 3, d = 4;
int x1 = -2, y1 = -2, z1 = 1;
int x2 = -4, y2 = 11, z2 = -1;
check_position(a, b, c, d,
x1, y1, z1,
x2, y2, z2);
return 0;
}
|
Java
public class GFG {
static void check_position( int a, int b, int c, int d,
int x1, int y1, int z1,
int x2, int y2, int z2)
{
int value_1 = a * x1 + b * y1 + c * z1 + d;
int value_2 = a * x2 + b * y2 + c * z2 + d;
if ((value_1 > 0 && value_2 > 0 )
|| (value_1 < 0 && value_2 < 0 ))
System.out.print( "On same side" );
if ((value_1 > 0 && value_2 < 0 )
|| (value_1 < 0 && value_2 > 0 ))
System.out.print( "On different sides" );
if (value_1 == 0 && value_2 == 0 )
System.out.print( "Both on the plane" );
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" );
}
public static void main(String[] args)
{
int a = 1 , b = 2 , c = 3 , d = 4 ;
int x1 = - 2 , y1 = - 2 , z1 = 1 ;
int x2 = - 4 , y2 = 11 , z2 = - 1 ;
check_position(a, b, c, d, x1, y1, z1, x2, y2, z2);
}
}
|
Python3
def check_position(a, b, c, d, x1, y1,
z1, x2, y2, z2):
value_1 = a * x1 + b * y1 + c * z1 + d
value_2 = a * x2 + b * y2 + c * z2 + d
if ((value_1 > 0 and value_2 > 0 ) or
(value_1 < 0 and value_2 < 0 )):
print ( "On same side" )
if ((value_1 > 0 and value_2 < 0 ) or
(value_1 < 0 and value_2 > 0 )):
print ( "On different sides" )
if (value_1 = = 0 and value_2 = = 0 ):
print ( "Both on the plane" )
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" )
if __name__ = = '__main__' :
a, b, c, d = 1 , 2 , 3 , 4
x1, y1, z1 = - 2 , - 2 , 1
x2, y2, z2 = - 4 , 11 , - 1
check_position(a, b, c, d, x1,
y1, z1, x2, y2, z2)
|
C#
using System;
class GFG{
static void check_position( int a, int b, int c, int d,
int x1, int y1, int z1,
int x2, int y2, int z2)
{
int value_1 = a * x1 + b * y1 + c * z1 + d;
int value_2 = a * x2 + b * y2 + c * z2 + d;
if ((value_1 > 0 && value_2 > 0)
|| (value_1 < 0 && value_2 < 0))
Console.Write( "On same side" );
if ((value_1 > 0 && value_2 < 0)
|| (value_1 < 0 && value_2 > 0))
Console.Write( "On different sides" );
if (value_1 == 0 && value_2 == 0)
Console.Write( "Both on the plane" );
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" );
}
static void Main()
{
int a = 1, b = 2, c = 3, d = 4;
int x1 = -2, y1 = -2, z1 = 1;
int x2 = -4, y2 = 11, z2 = -1;
check_position(a, b, c, d, x1, y1, z1, x2, y2, z2);
}
}
|
Javascript
<script>
function check_position(a , b , c , d,
x1 , y1 , z1,
x2 , y2 , z2)
{
var value_1 = a * x1 + b * y1 + c * z1 + d;
var value_2 = a * x2 + b * y2 + c * z2 + d;
if ((value_1 > 0 && value_2 > 0)
|| (value_1 < 0 && value_2 < 0))
document.write( "On same side" );
if ((value_1 > 0 && value_2 < 0)
|| (value_1 < 0 && value_2 > 0))
document.write( "On different sides" );
if (value_1 == 0 && value_2 == 0)
document.write( "Both on the plane" );
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" );
}
var a = 1, b = 2, c = 3, d = 4;
var x1 = -2, y1 = -2, z1 = 1;
var x2 = -4, y2 = 11, z2 = -1;
check_position(a, b, c, d, x1, y1, z1, x2, y2, z2);
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Last Updated :
20 Oct, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...