空间四点体积

https://ac.nowcoder.com/acm/contest/11212/E

#include<bits/stdc++.h>

using namespace std;

// define a point class to store a point
class Point
{
public:
    double x;
    double y;
    double z;
    Point(double x, double y, double z);
};
// constructor
Point::Point(double x, double y, double z)
{
    this->x = x;
    this->y = y;
    this->z = z;
}

// the function based on the given equation
double cal_volume(Point a, Point b, Point c, Point d)
{
    // a-d
    Point p_ad(a.x-d.x, a.y-d.y, a.z-d.z);
    // b-d
    Point p_bd(b.x-d.x, b.y- d.y, b.z-d.z);
    // c-d
    Point p_cd(c.x-d.x, c.y-d.y, c.z-d.z);
    // (b-d)x(c-d)
    Point p_bd_cd(p_bd.y * p_cd.z - p_bd.z * p_cd.y, p_bd.z * p_cd.x - p_bd.x * p_cd.z, p_bd.x * p_cd.y - p_bd.y * p_cd.x);
    // final result
    double res = abs(p_ad.x * p_bd_cd.x + p_ad.y * p_bd_cd.y + p_ad.z * p_bd_cd.z) / 6.0;
    return res;
}

int main()
{
    // test
    double x,y,z;
    cin>>x>>y>>z;
    Point p1(x, y, z);
    cin>>x>>y>>z;
    Point p2(x, y, z);
    cin>>x>>y>>z;
    Point p3(x, y, z);
    cin>>x>>y>>z;
    Point p4(x, y, z);
    double v = cal_volume(p1, p2, p3, p4);
    printf("%.7lf\n",v);
}

 

空间四点体积

上一篇:docker常用命令


下一篇:Cyclegan