Skip to content

Commit

Permalink
Add #fitExtent: to GPolygon
Browse files Browse the repository at this point in the history
  • Loading branch information
jecisc committed Dec 10, 2018
1 parent c6c1a27 commit 3eb6fb9
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 1 deletion.
62 changes: 62 additions & 0 deletions src/Geometry-Tests/GPolygonTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,68 @@ self
equals: (Rectangle left: -10 right:40 top: 10 bottom: -30)
]

{ #category : #tests }
GPolygonTest >> testFitInExtent [
| polygon |
polygon := GPolygon
newVertices:
{(10 @ 10).
(-10 @ 10).
(-10 @ -10)}.
polygon fitInExtent: 20 @ 20.
self
assert: polygon vertices
equals:
{(10 @ 10).
(-10 @ 10).
(-10 @ -10)}.
polygon fitInExtent: 40 @ 40.
self
assert: polygon vertices
equals:
{(20 @ 20).
(-20 @ 20).
(-20 @ -20)}.
polygon fitInExtent: 10 @ 10.
self
assert: polygon vertices
equals:
{(5 @ 5).
(-5 @ 5).
(-5 @ -5)}
]

{ #category : #tests }
GPolygonTest >> testFitInExtentNonProportionnal [
| polygon |
polygon := GPolygon
newVertices:
{(10 @ 4).
(-4 @ 10).
(-10 @ -8)}.
polygon fitInExtent: 20 @ 20.
self
assert: polygon vertices
equals:
{(10 @ 4).
(-4 @ 10).
(-10 @ -8)}.
polygon fitInExtent: 40 @ 40.
self
assert: polygon vertices
equals:
{(20 @ 8).
(-8 @ 20).
(-20 @ -16)}.
polygon fitInExtent: 10 @ 10.
self
assert: polygon vertices
equals:
{(5 @ 2).
(-2 @ 5).
(-5 @ -4)}
]

{ #category : #tests }
GPolygonTest >> testIncludesPoint [
self
Expand Down
16 changes: 15 additions & 1 deletion src/Geometry/GPolygon.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,20 @@ GPolygon >> encompasingRectangle [
^Rectangle left: leftMost x right: rightMost x top: top y bottom: bottom y
]

{ #category : #converting }
GPolygon >> fitInExtent: extent [
"I take as parameter a point and will fit the polygon in a rectangle whose dimensions is defined by the extent.
To do that, I will first fit my vertices in a square of lenght of 1, then I will multiply my vertices by the extent required."

| max verticesInSquareOfSizeOne |
max := (self vertices collect: #x) max max: ((self vertices collect: #y )) max. "We do not take `self vertices max` because we want to preserve proportions"
"Fit the polygon in a square of size 1 by doing a division of it's vertices by 2 times the value of the furthest point from the center of the polygon."
verticesInSquareOfSizeOne := self vertices collect: [ :point | point / (max * 2) ].

vertices := (verticesInSquareOfSizeOne collect: [ :point | point * extent ])
]

{ #category : #testing }
GPolygon >> includesPoint: point [

Expand All @@ -103,7 +117,7 @@ GPolygon >> intersectLineSegment: aLineSegment [
^ aLineSegment intersectPolygon: self
]

{ #category : #private }
{ #category : #converting }
GPolygon >> moveBy: aPoint [
"Move a polygon by a delta defined by aPoint"

Expand Down

0 comments on commit 3eb6fb9

Please sign in to comment.