#!/usr/bin/perl # PLANAR polygon AREA calculation in entered units # If vertices are travelled in wrong direction, the result # will be negative, and sign change will show the result. # Data pairs: Easting, Northing @poly = ( 613.69, 548.36, 669.00, 510.00, 593.27, 440.77, 398.07, 497.69, 425.46, 593.46, 517.44, 533.63 ); # test data.. should yield area=1.0000 #@poly = ( 0, 0, # 1, 0, # 1, 1, # 0, 1 ); # Step index by TWO (2) ! # $poly[$i+0] = y # $poly[$i+1] = x # Number of numbers in the poly array $N = 1+$#poly; # Separate the corner coordinate vector into X and Y components @x=(); @y=(); for ($i = 0; $i < $N; $i += 2) { $ii = $i/2; $x[$ii] = $poly[$i+0]; $y[$ii] = $poly[$i+1]; # $j = ($i + 2) % $N; # $x = $poly[$j+1] - $poly[$i+1]; # $y = $poly[$j+0] - $poly[$i+0]; # # $s = sqrt($x*$x + $y*$y); # printf( "v[%d] = { %12.3f, %12.3f }; s = %12.3f\n", $ii, $x, $y, $s ); } $N = $N / 2; $area = 0.0; $ji = 0.0; $ij = 0.0; for ($i = 0; $i < $N; ++$i) { $j = ($i + 1) % $N; $area += $x[$i] * $y[$j] - $x[$j] * $y[$i]; } $area *= 0.5; printf ( "Area = %.6f\n", abs($area) );