0

PHP with GD I built a quadrangle on the Cartesian plane. I measured the 4 sides, result = 100 each (the corners are at 90 degrees). Area calculation formula of Gauss. https://en.wikipedia.org/wiki/Shoelace_formula Area doverebbe be 10,000, but my result is 15,000? Where is the mistake? Thanks for your help.

    $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);
sandra
  • 29
  • 2

1 Answers1

0

You need to end shoelace formula with initial cordinates.

$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]);
Ergo
  • 44
  • 2