-
-
Notifications
You must be signed in to change notification settings - Fork 17
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
base: main
Are you sure you want to change the base?
Conversation
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>
| * Returns the signed area of the specified polygon. | ||
| */ | ||
| public static area(polygon: Polygon): number { | ||
| public static area(polygon: Polygon | PolygonArray): number { |
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.
| * 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 { |
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.
I haven’t looked at the uses of this function but it seems like the member function on Polygon should just be called directly.