Shoelace formula for polygonal area: Difference between revisions
Content added Content deleted
Not a robot (talk | contribs) (Add APL) |
Not a robot (talk | contribs) (Add Cowgol) |
||
Line 335: | Line 335: | ||
<pre>30</pre> |
<pre>30</pre> |
||
=={{header|Cowgol}}== |
|||
<lang cowgol>include "cowgol.coh"; |
|||
typedef Coord is uint16; # floating point types are not supported |
|||
record Point is |
|||
x: Coord; |
|||
y: Coord; |
|||
end record; |
|||
sub shoelace(p: [Point], length: intptr): (area: Coord) is |
|||
var left: Coord := 0; |
|||
var right: Coord := 0; |
|||
var y0 := p.y; |
|||
var x0 := p.x; |
|||
while length > 1 loop |
|||
var xp := p.x; |
|||
var yp := p.y; |
|||
p := @next p; |
|||
left := left + xp * p.y; |
|||
right := right + yp * p.x; |
|||
length := length - 1; |
|||
end loop; |
|||
left := left + y0 * p.x; |
|||
right := right + x0 * p.y; |
|||
if left < right then |
|||
area := right - left; |
|||
else |
|||
area := left - right; |
|||
end if; |
|||
area := area / 2; |
|||
end sub; |
|||
var polygon: Point[] := {{3,4},{5,11},{12,8},{9,5},{5,6}}; |
|||
print_i16(shoelace(&polygon[0], @sizeof polygon)); |
|||
print_nl();</lang> |
|||
{{out}} |
|||
<pre>30</pre> |
|||
=={{header|D}}== |
=={{header|D}}== |
||
<lang D>import std.stdio; |
<lang D>import std.stdio; |