hdu3692 三维计算几何,射线与平面交,点的旋转

本文详细介绍了如何使用C++实现点绕正轴旋转和射线和平面交点计算的算法。通过定义点类并实现相应操作,简化了数学计算过程,提高了代码的可读性和效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值