HP Forums
(49g 50g) Shoelace algorithm - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Software Libraries (/forum-10.html)
+--- Forum: General Software Library (/forum-13.html)
+--- Thread: (49g 50g) Shoelace algorithm (/thread-11259.html)

Pages: 1 2


RE: (49g 50g) Shoelace algorithm - Albert Chan - 02-28-2019 02:43 PM

(08-25-2018 02:02 PM)Albert Chan Wrote:  Let xr = rotated-left x, yr = rotated-left y ...

2*A = sum((x*yr - xr*y))
= sum(xr*yr - xr*y + x*yr - x*y)
= sum((xr + x)(yr - y))

Just noticed this re-arranged shoelace area formula can proof itself easily:

A = ½ sum((xr + x)(yr - y)) = -½ sum((yr + y)(xr - x))

|A| = \( \large \left| \left[\sum_{i=1}^{n-1}\frac{(y_{i+1}+y_{i})}{2}(x_{i+1}-x_{i}) \right] - \frac{(y_{n}+y_{1})}{2} ( x_{n}-x_{1}) \right| \)

First term is summing all the little trapezoid areas. Last term is area of the base.

Update: polygon area and perimeter implemented in Mathematica

area[v_] := Apply[area, Transpose[v]]; (* v = list of vertices *)
area[x_, y_] := Abs[Dot[RotateLeft[x]+x, RotateLeft[y]-y]] / 2

perimeter[x_,y_] := Plus @@ Abs[MapThread[Complex, {RotateLeft[x]-x, RotateLeft[y]-y}]]


RE: (49g 50g) Shoelace algorithm - John Keith - 07-13-2019 01:20 PM

Another interesting (and short!) version by user telemachos in this thread.


RE: (49g 50g) Shoelace algorithm - John Keith - 08-24-2019 01:19 PM

At Albert Chan's request, I am posting an HP-48G / HP 50 program for the perimeter of a polygon. Stack diagram is the same as for Thomas's programs for polygon area and centroid:

xs ys -> p

Code:

\<< R\->C DUP HEAD + 2.
  \<< - ABS
  \>> DOSUBS \GSLIST
\>>