Php 球点定位数学

Php 球点定位数学,php,math,geometry,Php,Math,Geometry,请原谅我没有提供代码。我的网络断开了,所以我在手机上做这件事。我正在尝试编写一个库来操纵半径为1的球体上的点 我给它打了8分lat,lon(代码单位为rad) +-45度,+-45度 +-45度,+-135度 求笛卡尔坐标,得到+-0.5,+-0.5,+-sqrt(2) 我希望所有的值都是sqrt(2)。我的输入或输出错误吗 编辑: 终于上网了这里是我的代码。如果方向角为0(正北),则移动函数有效,但对于任何其他方向,移动函数的数字变化似乎比我预期的要大 class PARTICLE {

请原谅我没有提供代码。我的网络断开了,所以我在手机上做这件事。我正在尝试编写一个库来操纵半径为1的球体上的点

我给它打了8分lat,lon(代码单位为rad)
+-45度,+-45度
+-45度,+-135度

求笛卡尔坐标,得到+-0.5,+-0.5,+-sqrt(2)

我希望所有的值都是sqrt(2)。我的输入或输出错误吗

编辑: 终于上网了这里是我的代码。如果方向角为0(正北),则移动函数有效,但对于任何其他方向,移动函数的数字变化似乎比我预期的要大

class PARTICLE {
    private $lat;       //north and south   - in radians
    private $lon;       //east and west     - in radians


    public function __construct($lat=0,$lon=0) {
        $this->lat=$lat;
        $this->lon=$lon;
    }


    /* **********************************************************************************************************
    *                                               Get Functions                                               *
    ********************************************************************************************************** */

    public function getLat() {
        return $this->lat;
    }
    public function getLon() {
        return $this->lon;
    }
    public function getXYZ() {
        return array (
            'x' => cos($this->lat) * cos($this->lon),
            'y' => cos($this->lat) * sin($this->lon),
            'z' => sin($this->lat)
        );
    }
    /* **********************************************************************************************************
    *                                            Manipulate Functions                                           *
    ********************************************************************************************************** */
    public function setLat($lat) {
        $this->lat=$lat;
    }
    public function setLon($lon) {
        $this->lon=$lon;
    }
    public function move($bearing,$distance) {
        //http://www.movable-type.co.uk/scripts/latlong.html
        $lat = asin(sin($this->lat)*cos($distance)+cos($this->lat)*sin($distance)*cos($bearing));
        $lon =$this->lon+atan2(sin($bearing)*sin($bearing)*cos($this->lat),cos($distance)-sin($this->lat)*sin($lat));
        $this->lat=$lat;
        $this->lon=fmod($lon+pi(),2*pi())-pi(); 
    }


    /* **********************************************************************************************************
    *                                             Static Functions                                              *
    ********************************************************************************************************** */

    public static function getDistance($point1,$point2) {       //haversine formula for great circuler arc distance
        $lat1=$point1->getLat();
        $lat2=$point2->getLat();
        $halfDeltaLat = ($lat2-$lat1)/2;
        $halfDeltaLon=($point2->getLon()-$point1->getLon())/2;

        $a=sin($halfDeltaLat)*sin($halfDeltaLat)+cos($lat1)*cos($lat2)*sin($halfDeltaLon)*sin($halfDeltaLon);
        return 2*atan2(sqrt($a),sqrt(1-$a));
    }

    public static function getInitialBearing($start,$end) {
        $y=sin($end->getLon()-$start->getLon()) * cos($end->getLat());
        $x=cos($start->getLat())*sin($end->getLat())-sin($start->getLat())*cos($end->getLat())*cos($end->getLon()-$start->getLon());
        return atan2($y, $x);
    }

    public static function getFinalBearing($start,$end) {
        //get bearing from end to start
        $theta=self::getInitialBearing($end,$start);

        //reverse bearing direction
        return ($theta+pi())%(2*pi());  
    }

    public static function flip($point) {
        $lat=0-$point->getLat();
        $lon=$point->getLon()+pi();
        return new self($lat,$lon);

    }

    public static function getRandom() {
        $lat=rand(0-pi()*1000000,pi()*1000000)/1000000;
        $lon=rand(0-pi()*1000000,pi()*1000000)/1000000;
        return new self($lat,$lon);
    }
}
测试代码:

$point=new PARTICLE(0.5,0.5);
echo $point->getLat().' , '.$point->getLon().'<br>';
$point->move(pi()/2,0.1);   //move off at 45 deg for 0.1rad
echo $point->getLat().' , '.$point->getLon().'<br>';
$point=新粒子(0.5,0.5);
echo$point->getLat().,'.$point->getLon().'
'; $point->move(pi()/2,0.1)//以45度移动0.1拉德 echo$point->getLat().,'.$point->getLon().'
';
为了获得更多有用的答案,请发布代码。我会在上网后发布。在手机上复制有点困难是的,我觉得输出不对。坐标应该距离原点1个单位,但是x=-0.5,y=-0.5,z=-sqrt(2)距离原点1.581个单位。假设您的意思是
1/sqrt(2)
,而不是
sqrt(2)
,那么这些输出对于给定的输入是正确的。如果您希望所有三个坐标相等(在这种情况下,它们必须分别为
1/sqrt(3)
),那么您的输入是错误的:您希望纬度为
atan(1/sqrt(2))
,大约为35.26度。谢谢@Mark Dickinson。您是对的,我的输入不正确,用“atan(1/sqrt(2))”替换后,我得到的所有3个值与我错误地期望的第一个值相同。