C++程序:检查三维平面上的点是否共面
给定点 (x1, y1, z1), (x2, y2, z2), (x3, y3, z3) 和 (x4, y4, z4),程序必须检查给定的点是否共面。如果点位于同一平面上,则称这些点为共面点;如果它们位于不同的平面上,则这些点不是共面点。
下图包含四个点,所有点都位于同一平面上(即xy平面),这意味着这些点是共面的。
下图包含四个点,它们位于不同的平面上,这表明这些点不是共面的。
示例
Input-: x1 = 2, y1 = 3, z1 = 1, x2 = 1, y2 = 9, z2 = 3, x3 = 3, y3 = 1, z3 = 5, x4 = 23, y4 = 21, z4 = 9 Output-: they are not coplanar Input-: x1 = 3, y1 = 2, z1 = -5, x2 = -1, y2 = 4, z2 = -3, x3 = -3, y3 = 8, z3 = -5, x4 = -3, y4 = 2, z4 = 1 Output-: they are coplanar
下面程序中使用的方法如下:
- 将点输入变量 (x1, y1, z1), (x2, y2, z2), (x3, y3, z3) 和 (x4, y4, z4) 中。
- 找到平面的方程,并检查它们是否满足条件。
- 显示这些点是否共面。
算法
START Step 1-> declare function to check whether points in 3-D are coplanar or not void check_coplanar(int x1,int y1,int z1,int x2,int y2,int z2, int x3, int y3, int z3, int x, int y, int z) declare variable as int a1 = x2 - x1 declare variable as int b1 = y2 - y1 declare variable as int c1 = z2 - z1 declare variable as int a2 = x3 - x1 declare variable as int b2 = y3 - y1 declare variable as int c2 = z3 - z1 declare variable as int a = b1 * c2 - b2 * c1 declare variable as int b = a2 * c1 - a1 * c2 declare variable as int c = a1 * b2 - b1 * a2 declare variable as int d = (- a * x1 - b * y1 - c * z1) check IF(a * x + b * y + c * z + d = 0) print coplanar End Else print not coplanar End Step 2-> In main() declare and set variable int x1 = 2 , y1 = 3, z1 = 1, x2 = 1, y2 = 9, z2 = 3, x3 = 3, y3 = 1, z3 = 5, x4 = 23, y4 = 21, z4 = 9 call check_coplanar(x1, y1, z1, x2, y2, z2, x3,y3, z3, x4, y4, z4) STOP
示例
#include<bits/stdc++.h> using namespace std ; //calculate points in a plane are coplanar or not void check_coplanar(int x1,int y1,int z1,int x2,int y2,int z2, int x3, int y3, int z3, int x, int y, int z) { int a1 = x2 - x1 ; int b1 = y2 - y1 ; int c1 = z2 - z1 ; int a2 = x3 - x1 ; int b2 = y3 - y1 ; int c2 = z3 - z1 ; int a = b1 * c2 - b2 * c1 ; int b = a2 * c1 - a1 * c2 ; int c = a1 * b2 - b1 * a2 ; int d = (- a * x1 - b * y1 - c * z1) ; if(a * x + b * y + c * z + d == 0) cout << "they are coplanar" << endl; else cout << "they are not coplanar" << endl; } int main() { int x1 = 2; int y1 = 3 ; int z1 = 1 ; int x2 = 1 ; int y2 = 9 ; int z2 = 3 ; int x3 = 3 ; int y3 = 1 ; int z3 = 5 ; int x4 = 23 ; int y4 = 21 ; int z4 = 9 ; check_coplanar(x1, y1, z1, x2, y2, z2, x3,y3, z3, x4, y4, z4) ; return 0; }
输出
如果运行上述代码,它将生成以下输出:
they are not coplanar
广告