parea Function

public function parea(x, y, nb)

given a sequence of nb points (x(i),y(i)). parea 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 curve is not required to be simple.

  • Original version from the NSWC Library

Arguments

Type IntentOptional Attributes Name
real(kind=wp), intent(in) :: x(nb)
real(kind=wp), intent(in) :: y(nb)
integer, intent(in) :: nb

Return Value real(kind=wp)


Called by

proc~~parea~~CalledByGraph proc~parea aoc_utilities::parea proc~go~2 problem_18::go proc~go~2->proc~parea program~problem_18 problem_18 program~problem_18->proc~go~2

Source Code

    real(wp) function parea(x, y, nb)

    real(wp),intent(in) :: x(nb), y(nb)
    integer,intent(in) :: nb

    real(wp) :: a
    integer :: n, nm1, i

    n = nb
    if (x(1) == x(n) .and. y(1) == y(n)) n = n - 1

    if (n-3 < 0) then
        parea = 0.0_wp
    else if (n-3==0) then
        parea= 0.5_wp*((x(2) - x(1))*(y(3) - y(1)) - (x(3) - x(1))*(y(2) - y(1)))
    else
        nm1 = n - 1
        a = x(1)*(y(2) - y(n)) + x(n)*(y(1) - y(nm1))
        do i = 2, nm1
            a = a + x(i)*(y(i+1) - y(i-1))
        end do
        parea = 0.5_wp*a
    end if

    end function parea