booleanClockwise function
Takes a ring and return true or false whether or not the ring is clockwise
or counter-clockwise.
Takes a Feature<LineString> orLineString or a List<Position> to be
evaluated.
example:
var clockwiseRing = LineString(
coordinates: [
Position.of([0, 0]),
Position.of([1, 1]),
Position.of([1, 0]),
Position.of([0, 0])
],
);
var counterClockwiseRing = LineString(
coordinates: [
Position.of([0, 0]),
Position.of([1, 0]),
Position.of([1, 1]),
Position.of([0, 0])
],
);
booleanClockwise(clockwiseRing);
//=true
booleanClockwise(counterClockwiseRing);
//=false
Implementation
bool booleanClockwise(LineString line) {
var ring = getCoords(line) as List<Position>;
num sum = 0;
int i = 1;
Position prev;
Position? cur;
while (i < ring.length) {
prev = cur ?? ring[0];
cur = ring[i];
sum += (cur[0]! - prev[0]!) * (cur[1]! + prev[1]!);
i++;
}
return sum > 0;
}