polyarea_shoelace(3f) - [M_math:geometry] compute area bounded by a simple closed polygon using the shoelace algorithm
Synopsis
Description
Options
Returns
Example
function polyarea_shoelace(x, y)
class(*), intent(in) :: x(:) class(*), intent(in) :: y(:) doubleprecision :: polyarea_shoelace
Given a sequence of points (X(I),Y(I)), polyarea_shoelace(3f) computes the area bounded by the closed polygonal curve which passes through the points in the order that they are indexed. The final point of the curve is assumed to be the first point given. Therefore, it need not be listed at the end of X and Y. The polygon should be simple (e.g. It may not cross over itself).If the vertices are given in counterclockwise order, the area will be positive. If the vertices are given in clockwise order, the area will be negative.
The X and Y arrays are assumed to be of the same size.
x x coordinates of the points that define the simple polygon. May be any standard scalar numeric value type supported by M_anything::anyscalar_to_double(3f). y y coordinates of the points that define the simple polygon. May be any standard scalar numeric value type supported by M_anything::anyscalar_to_double(3f).
polyarea_shoelace the area of the simple polygon
Sample program:
! (0,10) ########### (10,10) ! ## # ! # # # ! # # # ! # # # ! # # ! # # # ! # # # ! # # # ! ## # ! (0,0)########### (10,0)Results:program demo_polyarea_shoelace use M_math, only : polyarea_shoelace implicit none ! A B C D E F real,allocatable :: x(:) real,allocatable :: y(:)
x=[ 0.0, 10.0, 0.0, 10.0, 0.0, 0.0] !*! hourglass crosses itself. unexpected value y=[10.0, 10.0, 0.0, 0.0, 10.0, 10.0] write(*,*)polyarea_shoelace=,polyarea_shoelace(x,y)
x=[ 0.0, 10.0, 0.0, 0.0, 10.0, 0.0, 0.0] !*! crosses itself. maybe not what you expect y=[10.0, 10.0, 0.0, 10.0, 0.0, 0.0, 10.0] write(*,*)polyarea_shoelace=,polyarea_shoelace(x,y)
x=[ 0.0, 0.0, 10.0, 10.0, 0.0 ] ! square clockwise
y=[ 0.0, 10.0, 10.0, 0.0, 0.0 ] write(*,*)polyarea_shoelace=,polyarea_shoelace(x,y)
x=[ 0.0, 0.0, 10.0, 10.0, 0.0 ] ! square counterclockwise y=[10.0, 0.0, 0.0, 10.0, 10.0 ] write(*,*)polyarea_shoelace=,polyarea_shoelace(x,y)
end program demo_polyarea_shoelace
polyarea_shoelace= 0.0000000000000000 polyarea_shoelace= -100.00000000000000 polyarea_shoelace= -100.00000000000000 polyarea_shoelace= 100.00000000000000
Nemo Release 3.1 | polyarea_shoelace (3) | February 23, 2025 |