2015-12-27 54 views
0

與GD的PHP我在笛卡爾飛機上建立了一個四邊形。 我測量了4邊,結果= 100(角落在90度)。 高斯面積計算公式。 https://en.wikipedia.org/wiki/Shoelace_formula 面積doverebbe是10,000,但我的結果是15,000? 錯誤在哪裏? 感謝您的幫助。計算區域笛卡爾飛機

$Image = imagecreate(400,400); 
    imagecolorallocate($Image,0,0,0); 
    $White = imagecolorallocate($Image,255,255,255); 

    $p[0] = 100; $p[1] = 200;$p[2] = 200; 
    $p[3] = 200;$p[4] = 200; $p[5] = 300;$p[6] = 100; $p[7] = 300; 

    $XY = array(
    $p[0], $p[1], // section of the line $p[2], $p[3] 
    $p[2], $p[3], // section of the line $p[4], $p[5] 
    $p[4], $p[5], // section of the line $p[6], $p[7] 
    $p[6], $p[7] // section of the line $p[0], $p[1] 
      ); 

    imagepolygon($Image, $XY, 4, $White);   

    // segment 
    $SegmentLengthA = sqrt(bcpow($p[0]-$p[2],2) + bcpow($p[1]-$p[3],2)); 
    imagestring ($Image , 5 , 10 , 5 , "LunghSegmA = $SegmentLengthA" , $White); 

    $SegmentLengthB = sqrt(bcpow($p[2]-$p[4],2) + bcpow($p[3]-$p[5],2)); 
    imagestring ($Image , 5 , 10 , 20 , "LunghSegmB = $SegmentLengthB" , $White); 


    $SegmentLengthC = sqrt(bcpow($p[4]-$p[6],2) + bcpow($p[5]-$p[7],2)); 
    imagestring ($Image , 5 , 10 , 35 , "LunghSegmC = $SegmentLengthC" , $White); 


    $SegmentLengthD = sqrt(bcpow($p[6]-$p[0],2) + bcpow($p[7]-$p[1],2)); 
    imagestring ($Image , 5 , 10 , 50 , "LunghSegmC = $SegmentLengthC" , $White); 

    ////// perimeter 
    $Perimeter = $SegmentLengthA + $SegmentLengthB + $SegmentLengthC + $SegmentLengthD; 
    imagestring ($Image , 5 , 10 , 65 , "Perimeter = $Perimeter" , $White); 

    // area 
    $L1 = ($p[0] * $p[3]) + ($p[2] * $p[5]) + ($p[4] * $p[7]); 
    $L2 = ($p[1] * $p[2]) + ($p[3] * $p[4]) + ($p[5] * $p[6]); 
    $Area = abs($L1 - $L2)/2; 
    imagestring ($Image , 5 , 10 , 80 , "Area = $Area" , $White); 

    imagepng($Image); 

回答

0

您需要結束與初始座標的鞋帶公式。

$L1 = ($p[0] * $p[3]) + ($p[2] * $p[5]) + ($p[4] * $p[7]) + ($p[6] * $p[1]); 
$L2 = ($p[1] * $p[2]) + ($p[3] * $p[4]) + ($p[5] * $p[6]) + ($p[7] * $p[0]);