Skip to content

Commit

Permalink
take into account orientation of coordinate system
Browse files Browse the repository at this point in the history
  • Loading branch information
defo10 committed Sep 14, 2022
1 parent 0eed582 commit 00a06bb
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,7 @@
## 1.1.1

- Change documentation to take into account orientation of coordinate system.

## 1.1.0

- Use accessor functions to point coordinates instead of specified classes.
Expand Down
7 changes: 6 additions & 1 deletion lib/src/convex_hull_base.dart
Expand Up @@ -51,7 +51,10 @@ enum Direction {
/// [x] and [y] are accessor functions which specify what are the coordinates.
///
/// The returned convex hull starts with the leftmost point and traverses
/// counter-clockwise.
/// counter-clockwise for standard orientation (positive x is right, positive
/// y is up), and clockwise for left-handed orientation (positive x is right,
/// positive y is *down*). Left-handed orientation is common in many computer
/// graphics applications.
///
/// Convex hull only works for lists of at least 3 points. If there are less,
/// the function returns the points without changing anything and logs an info
Expand All @@ -68,6 +71,8 @@ Iterable<T> convexHull<T>(Iterable<T> points,
final sortedPoints = points.map((e) => Point2d(x(e), y(e), e)).toList()
..sort();

// wlog we assume a standard orientation, thus receive the lower half first,
// then the upper half. For left-handed orientation, the names would switch.
final lowerHalf = _halfHull(sortedPoints);
final upperHalf = _halfHull(sortedPoints.reversed);

Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
@@ -1,6 +1,6 @@
name: convex_hull
description: Calculates the convex hull of a given set of points. Monotone chain is used as algorithm.
version: 1.1.0
version: 1.1.1
homepage: https://github.com/defo10/convex-hull

environment:
Expand Down
12 changes: 12 additions & 0 deletions test/convex_hull_test.dart
Expand Up @@ -49,4 +49,16 @@ void main() {
x: (e) => e.xCoordinate, y: (e) => e.yCoordinate).map((e) => e.info);
expect(hull, equals(["1", "2"]));
});

test("goes clockwise starting from left", () {
final points = <Example>[
Example(0, 0, "a"),
Example(60, 0, "b"),
Example(60, -60, "c"),
];

final hull = convexHull<Example>(points,
x: (e) => e.xCoordinate, y: (e) => e.yCoordinate).map((e) => e.info);
expect(hull, equals(["a", "c", "b"]));
});
}

0 comments on commit 00a06bb

Please sign in to comment.