簡體   English   中英

創建具有特定半徑的多邊形

[英]Create polygon with a certain radius

我有一個半徑一定的多邊形。 這樣的東西。 現在我需要

  • 將其插入mysql數據庫。
  • 查找點位於多邊形內部還是外部。 (它包括獲取頂點嗎?)

如何做到這一點?

分類點要求完全定義多邊形。 通常,您將需要按順序圍繞多邊形的頂點,或者需要一些完全定義多邊形的約束(例如:常規,以原點為中心,在+ x軸上有一個頂點以及給定數量的邊)。 如果多邊形是自相交的,則在這種情況下,您還需要定義“內部”和“外部”的含義(有多個非等價的定義)。

編輯:如果您用谷歌“ php多邊形”,您會發現許多用於多邊形點測試的代碼(例如, 在這里 ,盡管我不保證該代碼的正確性)。

<?php
function point_in_reg_poly($sides, $radius, $p_x, $p_y) {
    $centerAngle = 2*pi() / $sides;
    $internalRadius = $radius * sin($centerAngle / 2);

    $angle = atan2($p_x, $p_y);
    $length = sqrt($p_x*$p_x + $p_y*$p_y);

    if($length > $radius)
        return false;

    //normalize angle to angle from center of nearest segment
    $angle = fmod($angle + 2*pi(), $centerAngle) - $centerAngle/2;

    return $internalRadius > $length * cos($angle);
} ?>

這個作品。 但實際上,您無法發現語法錯誤!

查找點是否在多邊形內,將在更新中完成

//this assumes that the orientation of your polygon is
//http://en.wikipedia.org/wiki/File:Pentagon.svg

$pass=1;

function filter($init, $final, $center)
{

    if(($final['a']['x']-$init['x'])*($center['y']-$init['y'])-($final['a']['y'] - $init['y'])*($center['x']-$init['x']) > 0)
        return $final['a'];
    else
        return $final['b']; 
}


function getNextPoint($init, $center, $distance, $slope)
{
    global $pass;

    $final['a']['x'] = $init['x']+$distance/sqrt(1+tan($slope)*tan($slope));
    $final['a']['y'] = $init['y']+(tan($slope)*$distance)/sqrt(1+tan($slope)*tan($slope));

    $final['b']['x'] = $init['x']-$distance/sqrt(1+tan($slope)*tan($slope));
    $final['b']['y'] = $init['y']-(tan($slope)*$distance)/sqrt(1+tan($slope)*tan($slope));


    echo "<br/><br/>";  
    echo "Pass: $pass <br/>";
    echo "Slope: ".$slope."<br/>";  


    if($pass == 1){ 
        $point = $final['b'];
        $distance = $distance*2*sin(pi()/5);
        $slope = 0;
    }
    else{   
        $point = filter($init, $final, $center);
        $slope = $slope+pi()/2.5;
    }       
    echo "Position: ";
    print_r($point);
    echo "<br/>";

    echo "Distance : ".distance($init['x'], $init['y'], $point['x'], $point['y']);


    if($pass == 7){ 
        return $point;  
    }
    else{
        //echo "x: ".($point['x'])." y: ".($point['y'])." <br/>";
        $pass++;

        getNextPoint($point, $center, $distance, $slope);
    }

    //echo "x: ".($point['x'])." y: ".($point['y'])." <br/>";
}

function polygon($vertices=5, $centerX=10, $centerY=10, $radius=5)
{

    $internalangle = ($vertices-2)*pi()/$vertices;

    $slope = pi()+($internalangle)/2;

    $init['x'] = 10;
    $init['y'] = 10;

    getNextPoint($init, $init, 5, $slope);
}



polygon();

/*
function getx($slope, $x1, $y1, $y)
{
    return (($y-$y1)/$slope+$x1);
}

function gety($slope, $x1, $y1, $x)
{
    return ($slope*($x-$x1)+$y1);
}

*/

function distance($initx, $inity, $finalx, $finaly)
{

    return sqrt(($initx-$finalx)*($initx-$finalx)+($inity-$finaly)*($inity-$finaly));

}

function getslope($final, $init)
{
    return atan(($final['y']-$init['y'])/($final['x']-$init['x']))*180/pi();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM