-
-
Notifications
You must be signed in to change notification settings - Fork 26
feat: add TypeScript Polygon class with comprehensive geometric operations #140
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
Dexus
wants to merge
7
commits into
main
Choose a base branch
from
feature/polygon-class
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4f2ab4e
feat: add TypeScript Polygon class with comprehensive geometric opera…
Dexus b7874fd
refactor(HullPolygon): integrate with new Polygon class
Dexus 2ce79a4
refactor(geometryutil): integrate with new Polygon class
Dexus 1b616e2
refactor(deepnest): integrate Polygon class with main logic
Dexus fde4bd2
refactor(background): integrate Polygon class with background worker
Dexus 691aca5
refactor(svgparser): output Polygon instances instead of point arrays
Dexus 476e776
enhance(polygon): properly utilize Vector class for geometric operations
Dexus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,10 @@ | ||
| // based on https://d3js.org/d3-polygon/ Version 1.0.2. | ||
|
|
||
| import { Point } from "./point.js"; | ||
| import { Polygon } from "./polygon.js"; | ||
|
|
||
| // Type definition for a polygon (array of points) | ||
| type Polygon = Point[]; | ||
| // Type definition for a polygon (array of points) - keeping for backwards compatibility | ||
| type PolygonArray = Point[]; | ||
|
|
||
| // Type for points with index used in hull calculation | ||
| interface IndexedPoint { | ||
|
|
@@ -19,7 +20,11 @@ export class HullPolygon { | |
| /** | ||
| * Returns the signed area of the specified polygon. | ||
| */ | ||
| public static area(polygon: Polygon): number { | ||
| public static area(polygon: Polygon | PolygonArray): number { | ||
| if (polygon instanceof Polygon) { | ||
| return polygon.area(); | ||
| } | ||
|
|
||
| let i = -1; | ||
| const n = polygon.length; | ||
| let a: Point; | ||
|
|
@@ -38,7 +43,11 @@ export class HullPolygon { | |
| /** | ||
| * Returns the centroid of the specified polygon. | ||
| */ | ||
| public static centroid(polygon: Polygon): Point { | ||
| public static centroid(polygon: Polygon | PolygonArray): Point { | ||
| if (polygon instanceof Polygon) { | ||
| return polygon.centroid(); | ||
| } | ||
|
|
||
| let i = -1; | ||
| const n = polygon.length; | ||
| let x = 0; | ||
|
|
@@ -62,11 +71,12 @@ export class HullPolygon { | |
|
|
||
| /** | ||
| * Returns the convex hull of the specified points. | ||
| * The returned hull is represented as an array of points | ||
| * The returned hull is represented as a new Polygon | ||
| * arranged in counterclockwise order. | ||
| */ | ||
| public static hull(points: Polygon): Polygon | null { | ||
| const n = points.length; | ||
| public static hull(points: Polygon | PolygonArray): Polygon | null { | ||
| const pointArray = points instanceof Polygon ? points.points : points; | ||
| const n = pointArray.length; | ||
| if (n < 3) return null; | ||
|
|
||
| let i: number; | ||
|
|
@@ -75,8 +85,8 @@ export class HullPolygon { | |
|
|
||
| for (i = 0; i < n; ++i) { | ||
| sortedPoints[i] = { | ||
| x: points[i].x, | ||
| y: points[i].y, | ||
| x: pointArray[i].x, | ||
| y: pointArray[i].y, | ||
| index: i, | ||
| }; | ||
| } | ||
|
|
@@ -99,26 +109,31 @@ export class HullPolygon { | |
| const skipRight = | ||
| lowerIndexes[lowerIndexes.length - 1] === | ||
| upperIndexes[upperIndexes.length - 1]; | ||
| const hull: Polygon = []; | ||
| const hullPoints: Point[] = []; | ||
|
|
||
| // Add upper hull in right-to-left order. | ||
| // Then add lower hull in left-to-right order. | ||
| for (i = upperIndexes.length - 1; i >= 0; --i) | ||
| hull.push(points[sortedPoints[upperIndexes[i]].index]); | ||
| hullPoints.push(pointArray[sortedPoints[upperIndexes[i]].index]); | ||
| for ( | ||
| i = skipLeft ? 1 : 0; | ||
| i < lowerIndexes.length - (skipRight ? 1 : 0); | ||
| ++i | ||
| ) | ||
| hull.push(points[sortedPoints[lowerIndexes[i]].index]); | ||
| hullPoints.push(pointArray[sortedPoints[lowerIndexes[i]].index]); | ||
|
|
||
| return hull; | ||
| return new Polygon(hullPoints); | ||
| } | ||
|
|
||
| /** | ||
| * Returns true if and only if the specified point is inside the specified polygon. | ||
| */ | ||
| public static contains(polygon: Polygon, point: Point): boolean { | ||
| public static contains(polygon: Polygon | PolygonArray, point: Point): boolean { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I haven’t looked at the uses of this function but it seems like the member function on Polygon should just be called directly. |
||
| if (polygon instanceof Polygon) { | ||
| const result = polygon.contains(point); | ||
| return result === true; // convert null to false | ||
| } | ||
|
|
||
| const n = polygon.length; | ||
| let p = polygon[n - 1]; | ||
| const x = point.x; | ||
|
|
@@ -145,7 +160,11 @@ export class HullPolygon { | |
| /** | ||
| * Returns the length of the perimeter of the specified polygon. | ||
| */ | ||
| public static length(polygon: Polygon): number { | ||
| public static length(polygon: Polygon | PolygonArray): number { | ||
| if (polygon instanceof Polygon) { | ||
| return polygon.perimeter(); | ||
| } | ||
|
|
||
| let i = -1; | ||
| const n = polygon.length; | ||
| let b = polygon[n - 1]; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it make sense to represent various properties in the type system? For example, we could have HullPolygon be a subclass of Polygon, and AxisAlignedRectangle as a subclass of HullPolygon. This would make some of this code simpler or easier to follow, hull() can return HullPolygon and so on. We can still override methods as needed for precision or speed, since eg area of AxisAlignedRectangle is very simple.