How to resolve the algorithm Shoelace formula for polygonal area step by step in the FreeBASIC programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Shoelace formula for polygonal area step by step in the FreeBASIC programming language

Table of Contents

Problem Statement

Given the n + 1 vertices x[0], y[0] .. x[N], y[N] of a simple polygon described in a clockwise direction, then the polygon's area can be calculated by: (Where abs returns the absolute value) Write a function/method/routine to use the the Shoelace formula to calculate the area of the polygon described by the ordered points:

Show the answer here, on this page.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Shoelace formula for polygonal area step by step in the FreeBASIC programming language

Source code in the freebasic programming language

' version 18-08-2017
' compile with: fbc -s console

Type _point_
    As Double x, y
End Type

Function shoelace_formula(p() As _point_ ) As Double

    Dim As UInteger i
    Dim As Double sum

    For i = 1 To UBound(p) -1
        sum += p(i   ).x * p(i +1).y
        sum -= p(i +1).x * p(i   ).y
    Next
    sum += p(i).x * p(1).y
    sum -= p(1).x * p(i).y

    Return Abs(sum) / 2
End Function

' ------=< MAIN >=------

Dim As _point_ p_array(1 To ...) = {(3,4), (5,11), (12,8), (9,5), (5,6)}

Print "The area of the polygon ="; shoelace_formula(p_array())

' empty keyboard buffer
While Inkey <> "" : Wend
Print : Print "hit any key to end program"
Sleep
End

  

You may also check:How to resolve the algorithm Sorting algorithms/Patience sort step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Look-and-say sequence step by step in the OCaml programming language
You may also check:How to resolve the algorithm Find if a point is within a triangle step by step in the Nim programming language
You may also check:How to resolve the algorithm Strip whitespace from a string/Top and tail step by step in the zkl programming language
You may also check:How to resolve the algorithm Stream merge step by step in the Java programming language