球点位置数学

时间:2015-12-14 18:56:08

标签: php math geometry

请原谅我没有提供任何代码。我的互联网已关闭,所以我通过手机进行此操作。我正在尝试编写一个库来操纵半径为1的球体上的点。

我给了它8点lat,lon(代码为rad)
+ -45deg,+ - 45deg
+ -45deg,+ - 135deg

要求笛卡尔坐标并获得+ -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>';

0 个答案:

没有答案