hdu3692 三维计算几何,射线与平面交,点的旋转
1.点绕正轴(x,y,z轴)旋转整理成模板了,代码很短。
2.射线和平面交,定义一个点乘运算符,写起来代码比较短,思路比较清楚。
#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>using namespace std;const double eps = 1e-8;struct Point { double x, y, z; Point(double x, double y, double z): x(x), y(y), z(z){} Point(){} Point operator-(const Point &t) const { return Point(x-t.x, y-t.y, z-t.z); } Point operator+(const Point &t) const { return Point(x + t.x, y + t.y, z + t.z); } Point rotz(double t) { // 绕z旋转 return Point(x*cos(t)-y*sin(t), x*sin(t)+y*cos(t), z); } Point rotx(double t) { //绕x旋转 return Point(x, y*cos(t)-z*sin(t), y*sin(t)+z*cos(t)); } Point operator*(const Point &t) const { //叉乘 return Point(y*t.z-z*t.y, z*t.x-x*t.z, x*t.y-y*t.x); } Point operator*(const double &t) const { return Point(x*t, y*t, z*t); } double operator^(const Point &t) const { //点乘 return x*t.x+y*t.y+z*t.z; } void in() { scanf("%lf%lf%lf", &x, &y, &z); } bool operator<(const Point &t) const { return y +eps< t.y || (fabs(y-t.y) < eps && x + eps < t.x); } void out() { printf("%lf %lf %lf~~~~\n", x, y, z); }}p[103], st[103];double a, b, c, d;int n, cnt;//***********旋转void rot() { if(a*a+b*b < eps) return; double t1 = acos(b/sqrt(a*a+b*b)); double t2 = acos(c/sqrt(a*a+b*b+c*c)); for(int i = 0; i < n; i++) p[i] = (p[i].rotz(t1)).rotx(t2);}//************凸包double cross(Point a, Point b) { return a.x*b.y-a.y*b.x;}double convex(Point *p, int n) { sort(p, p+n); int m = 0; int i; for(i = 0; i < n; i++) { while(m > 1 && cross(st[m-1]-st[m-2],p[i]-st[m-2]) < eps) m--; st[m++] = p[i]; } int t = m; for(i = n-2; i >= 0; i--) { while(m > t && cross(st[m-1]-st[m-2], p[i]-st[m-2]) < eps) m--; st[m++] = p[i]; } double ans = 0; for(i = 1; i < m-1; i++) ans += cross(st[i]-st[0], st[i+1]-st[0]); return fabs(ans)*0.5;}int main() { int i; while(~scanf("%lf%lf%lf%lf", &a, &b, &c, &d)) { if(a==0 && b==0 && c==0 && d==0) break; scanf("%d", &n); for(i = 0; i <= n; i++) p[i].in(); //*****射线与平面交 Point f(a, b, c); cnt = 0; for(i = 0; i < n; i++) { Point v = p[i]-p[n]; if(fabs(f^v) < eps) continue; double t = (d-(f^p[n]))/(f^v); if(t > -eps) p[cnt++] = v*t+p[n]; } if(!cnt) puts("0.00"); else if(cnt < n) puts("Infi"); else { rot(); printf("%.2f\n", convex(p, n)); } } return 0;}