morphic.js
morphic.js copied to clipboard
Rectangle.intersect can give negative rectangles - worth a comment
This is correct, but I think it deserves a comment, because, say, it matters if one wants to implement a proper isEmpty routine (which I didn't, and costed me several hours of debugging :-)
new Rectangle(10,10,20,20).intersect(new Rectangle(15,25,19,25))
gives a rectangle with the corner above the origin:
origin: Point
x: 15
y: 25
corner: Point
x: 19
y: 20
(the proper "is empty" test indeed is already in morphic.js
!dirty.extent().gt(new Point(0, 0))
, just not in a dedicated routine)
Also note how a negative rectangle can be expanded to be positive (which again is correct but worth a comment, as the intuition might otherwise suggest that many operations including expansion should be "void" on an empty rectangle):
new Rectangle(10,10,20,20).intersect(new Rectangle(15,25,19,25)).expandBy(3).area() -> 10
Also - similar topic: 2 empty rectangles can be merged into a bigger non-empty rectangle
new Rectangle(0,0,0,0).merge(new Rectangle(10,10,10,10))
gives the rectangle at origin 0,0 and corner at 10,10