2015-12-27 58 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]);