51Nod-1265 四点共面

51Nod 1265 : http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1265

1265 四点共面

基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题

 

给出三维空间上的四个点(点与点的位置均不相同),判断这4个点是否在同一个平面内(4点共线也算共面)。如果共面,输出"Yes",否则输出"No"。

Input

第1行:一个数T,表示输入的测试数量(1 <= T <= 1000)

第2 - 4T + 1行:每行4行表示一组数据,每行3个数,x, y, z, 表示该点的位置坐标(-1000 <= x, y, z <= 1000)。

Output

输出共T行,如果共面输出"Yes",否则输出"No"。

Input示例

1

1 2 0

2 3 0

4 0 0

0 0 0

Output示例

Yes

题解:

确定空间中的四个点(三维)是否共面

对于四个点, 以一个点为原点,对于其他三个点有A,B, C三个向量, 求出 A X B (cross product), 就是以A B构成的平面的一个法向量(如果 AB共线,则法向量为0), 在求其与C之间的点积, 如果为0, 则表示两个向量为0。 所以四点共面。

#include <iostream> 
#include <cstdio> 
#include <cstring> 
#include <algorithm>
#include <map> 
#include <cmath> 
using namespace std;

struct Node{
	double x, y, z; 
}; 

int main(){
//	freopen("in.txt", "r", stdin); 

	int test_num; 
	Node a[5];  
	double ans; 
	scanf("%d", &test_num); 
	while(test_num--){
		for(int i=0; i<4; ++i){
			scanf("%lf %lf %lf", &a[i].x, &a[i].y, &a[i].z); 
		} 
		// the cross product of (a,b) 
		a[4].x = -(a[1].z - a[0].z)*(a[2].y - a[0].y)+ (a[1].y - a[0].y)*(a[2].z - a[0].z); 
		a[4].y = (a[1].z - a[0].z)*(a[2].x-a[0].x) - (a[1].x - a[0].x)*(a[2].z - a[0].z); 
		a[4].z = (a[1].x -a[0].x)*(a[2].y - a[0].y)  - (a[1].y-a[0].y)*(a[2].x - a[0].x ); 
		// the dot product of cp(a,b) and c 
		ans = (a[3].x - a[0].x)*(a[4].x) + (a[3].y-a[0].y)*a[4].y + (a[3].z-a[0].z)*a[4].z; 
		if( fabs(ans) <= 1e-9){
			printf("Yes
");
		}else{
			printf("No
");
		}
	} 
	return 0; 
}

  

原文地址:https://www.cnblogs.com/zhang-yd/p/6022276.html