给定两条线L1和L2 ,每条线都经过一个点,其位置矢量给定为(X,Y,Z),并与方向比给定为(a,b,c)的线平行,任务是检查线是否L1和L2是否共面。
Coplanar: If two lines are in a same plane then lines can be called as coplanar.
例子:
Input:
L1: (x1, y1, z1) = (-3, 1, 5) and (a1, b1, c1) = (-3, 1, 5)
L2: (x1, y1, z1) = (-1, 2, 5) and (a1, b1, c1) = (-1, 2, 5)
Output: Lines are Coplanar
Input:
L1: (x1, y1, z1) = (1, 2, 3) and (a1, b1, c1) = (2, 4, 6)
L2: (x1, y1, z1) = (-1, -2, -3) and (a1, b1, c1) = (3, 4, 5)
Output: Lines are Non-Coplanar
方法:
有两种方法可以在3维中表示一条线:
向量形式:
两行方程的共面性将以矢量形式确定。
在上面的线方程中,向量是3D平面中给定线经过的点,称为位置向量a , b向量是3D平面中与给定线平行的向量线。因此可以说,线(1)通过位置为a1的点A并与矢量b1平行,线2通过位置为a2的点B且与位置b平行。向量b2 。所以:
当且仅当AB向量垂直于向量b1和b2的叉积时,给定的线才是共面的,即
在这里,向量b1和b2的叉积将给出另一条向量线,该向量线将垂直于b1和b2向量线。 AB是连接两个给定线的位置向量a1和a2的线向量。现在,通过确定上述点积是否为零,检查两条线是否共面。
笛卡尔形式:
令(x1,y1,z1)和(x2,y2,z2)分别为点A和B的坐标。
设a1,b1,c1和a2,b2,c2分别为向量b1和b2的方向比。然后
当且仅当以下情况时,给定的线是共面的:
以笛卡尔形式可以表示为:
因此,对于这两种形式的表单,都需要在输入中分别以(x1,y1,z1)和(x2,y2,z2)作为位置向量a1和a2 ,将向量b1和b2的方向比作为(a1,b1,c1)和(a2,b2,c2) 。
请按照以下步骤解决问题:
- 初始化3 X 3矩阵以存储上面显示的行列式的元素。
- 计算b2和b1的叉积和(a2 – a1)的点积。
- 如果行列式的值为0,则线是共面的。否则,它们是非共面的。
下面是上述方法的实现:
C++
// C++ program implement
// the above approach
#include
using namespace std;
// Function to generate determinant
int det(int d[][3])
{
int Sum = d[0][0] * ((d[1][1] * d[2][2]) - (d[2][1] * d[1][2]));
Sum -= d[0][1] * ((d[1][0] * d[2][2]) - (d[1][2] * d[2][0]));
Sum += d[0][2] * ((d[0][1] * d[1][2]) -(d[0][2] * d[1][1]));
// Return the sum
return Sum;
}
// Driver Code
int main()
{
// Position vector of first line
int x1 = -3, y1 = 1, z1 = 5;
// Direction ratios of line to
// which first line is parallel
int a1 = -3, b1 = 1, c1 = 5;
// Position vectors of second line
int x2 = -1, y2 = 2, z2 = 5;
// Direction ratios of line to
// which second line is parallel
int a2 = -1, b2 = 2, c2 = 5;
// Determinant to check coplanarity
int det_list[3][3] = { {x2 - x1, y2 - y1, z2 - z1},
{a1, b1, c1}, {a2, b2, c2}};
// If determinant is zero
if(det(det_list) == 0)
{
cout << "Lines are coplanar" << endl;
}
// Otherwise
else
{
cout << "Lines are non coplanar" << endl;
}
return 0;
}
// This code is contributed by avanitrachhadiya2155
Java
// Java program implement
// the above approach
import java.io.*;
class GFG{
// Function to generate determinant
static int det(int[][] d)
{
int Sum = d[0][0] * ((d[1][1] * d[2][2]) -
(d[2][1] * d[1][2]));
Sum -= d[0][1] * ((d[1][0] * d[2][2]) -
(d[1][2] * d[2][0]));
Sum += d[0][2] * ((d[0][1] * d[1][2]) -
(d[0][2] * d[1][1]));
// Return the sum
return Sum;
}
// Driver Code
public static void main (String[] args)
{
// Position vector of first line
int x1 = -3, y1 = 1, z1 = 5;
// Direction ratios of line to
// which first line is parallel
int a1 = -3, b1 = 1, c1 = 5;
// Position vectors of second line
int x2 = -1, y2 = 2, z2 = 5;
// Direction ratios of line to
// which second line is parallel
int a2 = -1, b2 = 2, c2 = 5;
// Determinant to check coplanarity
int[][] det_list = { {x2 - x1, y2 - y1, z2 - z1},
{a1, b1, c1}, {a2, b2, c2}};
// If determinant is zero
if(det(det_list) == 0)
System.out.print("Lines are coplanar");
// Otherwise
else
System.out.print("Lines are non coplanar");
}
}
// This code is contributed by offbeat
Python3
# Python Program implement
# the above approach
# Function to generate determinant
def det(d):
Sum = d[0][0] * ((d[1][1] * d[2][2])
- (d[2][1] * d[1][2]))
Sum -= d[0][1] * ((d[1][0] * d[2][2])
- (d[1][2] * d[2][0]))
Sum += d[0][2] * ((d[0][1] * d[1][2])
- (d[0][2] * d[1][1]))
# Return the sum
return Sum
# Driver Code
if __name__ == '__main__':
# Position vector of first line
x1, y1, z1 = -3, 1, 5
# Direction ratios of line to
# which first line is parallel
a1, b1, c1 = -3, 1, 5
# Position vectors of second line
x2, y2, z2 = -1, 2, 5
# Direction ratios of line to
# which second line is parallel
a2, b2, c2 = -1, 2, 5
# Determinant to check coplanarity
det_list = [[x2-x1, y2-y1, z2-z1],
[a1, b1, c1], [a2, b2, c2]]
# If determinant is zero
if(det(det_list) == 0):
print("Lines are coplanar")
# Otherwise
else:
print("Lines are non coplanar")
C#
// C# program implement
// the above approach
using System;
class GFG{
// Function to generate determinant
static int det(int[,] d)
{
int Sum = d[0, 0] * ((d[1, 1] * d[2, 2]) -
(d[2, 1] * d[1, 2]));
Sum -= d[0, 1] * ((d[1, 0] * d[2, 2]) -
(d[1, 2] * d[2, 0]));
Sum += d[0, 2] * ((d[0, 1] * d[1, 2]) -
(d[0, 2] * d[1, 1]));
// Return the sum
return Sum;
}
// Driver Code
public static void Main()
{
// Position vector of first line
int x1 = -3, y1 = 1, z1 = 5;
// Direction ratios of line to
// which first line is parallel
int a1 = -3, b1 = 1, c1 = 5;
// Position vectors of second line
int x2 = -1, y2 = 2, z2 = 5;
// Direction ratios of line to
// which second line is parallel
int a2 = -1, b2 = 2, c2 = 5;
// Determinant to check coplanarity
int[,] det_list = { {x2 - x1, y2 - y1, z2 - z1},
{a1, b1, c1}, {a2, b2, c2}};
// If determinant is zero
if (det(det_list) == 0)
Console.Write("Lines are coplanar");
// Otherwise
else
Console.Write("Lines are non coplanar");
}
}
// This code is contributed by sanjoy_62
Lines are coplanar
时间复杂度: O(1)
辅助空间: O(1)