How to resolve the algorithm Shoelace formula for polygonal area step by step in the Nim 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 Nim 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 Nim programming language

Source code in the nim programming language

type
  Point = tuple
    x: float
    y: float

func shoelace(points: openArray[Point]): float =
  var leftSum, rightSum = 0.0
  for i in 0..<len(points):
    var j = (i + 1) mod len(points)
    leftSum  += points[i].x * points[j].y
    rightSum += points[j].x * points[i].y
  0.5 * abs(leftSum - rightSum)

var points = [(3.0, 4.0), (5.0, 11.0), (12.0, 8.0), (9.0, 5.0), (5.0, 6.0)]

echo shoelace(points)


  

You may also check:How to resolve the algorithm Keyboard input/Flush the keyboard buffer step by step in the Perl programming language
You may also check:How to resolve the algorithm Sierpinski carpet step by step in the Mathematica/Wolfram Language programming language
You may also check:How to resolve the algorithm Determine if a string is collapsible step by step in the D programming language
You may also check:How to resolve the algorithm 24 game/Solve step by step in the Sidef programming language
You may also check:How to resolve the algorithm Array concatenation step by step in the Emacs Lisp programming language