计算任何给定四面体4点卷 [英] Calculate Volume of any Tetrahedron given 4 points

查看:156
本文介绍了计算任何给定四面体4点卷的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要计算的任何四面体给出4分的音量。 我正在写一个程序,即需要找到四面体的体积,而我所知道的是: 的4点所有的X,Y,Z的位置。该函数必须返回的体积。 我stucked在这10个小时......我不能想任何事情。 谢谢!

I need to calculate the volume of "any" tetrahedron given 4 points. I'm writing a program, that needs to find the volume of the tetrahedron, and what I know is: all X, Y, Z location of the 4 points. The function have to return the volume. I'm stucked in this for 10 hours... I couldn't think in ANY thing. Thanks!

推荐答案

下面是code,PHP中,用来计算给出4分的任何四面体的体积:

Here is the code, in PHP, that calculates the Volume of any Tetrahedron given 4 points:

class Math{
public static function determinant(array $vals){
    $s = sizeof($vals);
    $sum = 0.0;
    for( $i=0; $i < $s ; $i++ ){
        $mult = 1.0;
        for($j=0; $j < $s ; $j++ ){
            $mult *= $vals[$j][ ($i+$j)%$s ];
        }
        $sum += $mult;
    }
    for( $i=0; $i < $s ; $i++ ){
        $mult = 1;
        for($j=0; $j < $s ; $j++ ){
            $mult *= $vals[$j][ ($i-$j < 0? $s - ($j-$i) :($i-$j)) ];
        }
        $sum -= $mult;
    }
    return $sum;
}

public static function subtract(array $a, array $b){
    for($i = 0; $i < sizeof($a); $i++)
        $a[$i] -= $b[$i];

    return $a;
}
}
// TEST CASE
$a = array(0,0,0);
$d = array(2,0,0);
$c = array(0,2,0);
$b = array(0,0,2);

echo abs(Math::determinant(array(
Math::subtract($a, $b),
Math::subtract($b, $c),
Math::subtract($c, $d),
)))/6;

这篇关于计算任何给定四面体4点卷的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆