feat: add TypeScript Polygon class with comprehensive geometric operations#140
Draft
feat: add TypeScript Polygon class with comprehensive geometric operations#140
Conversation
Member
Dexus
commented
Jul 9, 2025
- Created new Polygon class in main/util/polygon.ts as modern replacement for array-based polygons
- Uses existing Point class for type-safe coordinate handling
- Implements all commonly used polygon operations from geometryutil.js:
- Geometric calculations: area(), bounds(), centroid(), perimeter()
- Spatial queries: contains() with point-in-polygon testing
- Transformations: translate(), rotate(), scale() with immutable and in-place variants
- Utility methods: isRectangle(), reverse(), winding direction control
- Provides conversion methods for legacy array format and Clipper integration
- Includes static factory methods for creating rectangles and circles
- Maintains compatibility with existing offsetx/offsety and children properties
- Supports both immutable operations (return new instances) and in-place modifications
…tions - Created new Polygon class in main/util/polygon.ts as modern replacement for array-based polygons - Uses existing Point class for type-safe coordinate handling - Implements all commonly used polygon operations from geometryutil.js: - Geometric calculations: area(), bounds(), centroid(), perimeter() - Spatial queries: contains() with point-in-polygon testing - Transformations: translate(), rotate(), scale() with immutable and in-place variants - Utility methods: isRectangle(), reverse(), winding direction control - Provides conversion methods for legacy array format and Clipper integration - Includes static factory methods for creating rectangles and circles - Maintains compatibility with existing offsetx/offsety and children properties - Supports both immutable operations (return new instances) and in-place modifications
- Updated HullPolygon.ts to accept both new Polygon class and legacy Point[] arrays - Added backwards compatibility with PolygonArray type alias - Modified all static methods to handle both polygon types: - area(): delegates to Polygon.area() when possible - centroid(): delegates to Polygon.centroid() when possible - hull(): returns new Polygon instance instead of Point[] - contains(): uses Polygon.contains() with null-to-false conversion - length(): delegates to Polygon.perimeter() when possible - Maintains full backwards compatibility while enabling new Polygon class usage 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
- Updated key polygon functions in geometryutil.js to use new Polygon class: - getPolygonBounds(): delegates to polygon.bounds() when available - pointInPolygon(): delegates to polygon.contains() when available - polygonArea(): delegates to polygon.area() when available - isRectangle(): delegates to polygon.isRectangle() when available - rotatePolygon(): delegates to polygon.rotate() when available - Added global window export to polygon.ts for JavaScript compatibility - Maintains full backwards compatibility with array-based polygon format - Functions automatically detect Polygon class instances and use optimized methods 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
- Added Polygon class import to deepnest.js - Updated getHull() function to handle both Polygon instances and arrays - Added instanceof checks to use Polygon methods when available - Maintains backwards compatibility with existing array-based polygon format - HullPolygon.hull() results are converted to arrays for consistency with existing code 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
- Added Polygon class import to background.js - Updated getHull() function to handle Polygon instances: - Uses HullPolygon.hull() directly for Polygon objects - Converts results to arrays for compatibility with existing code - Updated rotatePolygon() function to handle Polygon instances: - Uses Polygon.rotate() method when available - Preserves exact property from original points - Maintains children handling for nested polygons - Maintains full backwards compatibility with array-based polygon format 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
- Added Polygon class import to svgparser.js
- Updated polygonify() method to return Polygon instances:
- Converts array of {x, y} points to Point instances
- Creates and returns new Polygon from Point array
- Maintains all existing SVG element support (rect, circle, ellipse, path, polygon, polyline)
- Preserves existing coincident point removal logic
- SVG parsing now directly produces Polygon objects ready for nesting operations
🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <noreply@anthropic.com>
- Re-added Vector import and implemented proper vector-based operations - Added getEdgeNormal() method to get perpendicular edge vectors - Added getEdgeVector() method to get edge direction vectors - isRectangle() method already uses Vector through Point.to() for dot products - Enhanced geometric capabilities with proper vector mathematics - Fixed unused import issue identified in code review 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
willmurnane
reviewed
Jul 12, 2025
| * Returns the signed area of the specified polygon. | ||
| */ | ||
| public static area(polygon: Polygon): number { | ||
| public static area(polygon: Polygon | PolygonArray): number { |
Collaborator
There was a problem hiding this comment.
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.
willmurnane
reviewed
Jul 12, 2025
| * 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.
I haven’t looked at the uses of this function but it seems like the member function on Polygon should just be called directly.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.