给定四个整数a 、 b 、 c和d ,它们表示平面ax + by + cz + d = 0的方程系数和两个整数坐标(x1, y1, z1)和(x2, y2, z2) ,任务是找出两个点是否位于同一侧,或不同侧,或在平面的表面上。
例子:
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
方法:这个想法是基于这样一个事实,如果应用于方程的两个点具有相同的奇偶性(符号),那么它们将位于平面的同一侧,如果它们具有不同的奇偶性,则它们将位于平面的同一侧飞机的不同侧面。请按照以下步骤解决问题:
- 将给定点的坐标放入平面方程并将值存储在变量P1和P2 中。
- 检查获得的值的符号:
- 如果P1和P2具有相同的奇偶校验,则它们在平面的同一侧。
- 如果P1和P2具有不同的奇偶性,则它们位于平面的相对两侧。
- 如果P1和P2为零,则它们位于平面上。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
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
On same side
时间复杂度: O(1)
辅助空间: O(1)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。