Skip to content

Commit 3eb6fb9

Browse files
committed
Add #fitExtent: to GPolygon
1 parent c6c1a27 commit 3eb6fb9

File tree

2 files changed

+77
-1
lines changed

2 files changed

+77
-1
lines changed

src/Geometry-Tests/GPolygonTest.class.st

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,68 @@ self
4141
equals: (Rectangle left: -10 right:40 top: 10 bottom: -30)
4242
]
4343

44+
{ #category : #tests }
45+
GPolygonTest >> testFitInExtent [
46+
| polygon |
47+
polygon := GPolygon
48+
newVertices:
49+
{(10 @ 10).
50+
(-10 @ 10).
51+
(-10 @ -10)}.
52+
polygon fitInExtent: 20 @ 20.
53+
self
54+
assert: polygon vertices
55+
equals:
56+
{(10 @ 10).
57+
(-10 @ 10).
58+
(-10 @ -10)}.
59+
polygon fitInExtent: 40 @ 40.
60+
self
61+
assert: polygon vertices
62+
equals:
63+
{(20 @ 20).
64+
(-20 @ 20).
65+
(-20 @ -20)}.
66+
polygon fitInExtent: 10 @ 10.
67+
self
68+
assert: polygon vertices
69+
equals:
70+
{(5 @ 5).
71+
(-5 @ 5).
72+
(-5 @ -5)}
73+
]
74+
75+
{ #category : #tests }
76+
GPolygonTest >> testFitInExtentNonProportionnal [
77+
| polygon |
78+
polygon := GPolygon
79+
newVertices:
80+
{(10 @ 4).
81+
(-4 @ 10).
82+
(-10 @ -8)}.
83+
polygon fitInExtent: 20 @ 20.
84+
self
85+
assert: polygon vertices
86+
equals:
87+
{(10 @ 4).
88+
(-4 @ 10).
89+
(-10 @ -8)}.
90+
polygon fitInExtent: 40 @ 40.
91+
self
92+
assert: polygon vertices
93+
equals:
94+
{(20 @ 8).
95+
(-8 @ 20).
96+
(-20 @ -16)}.
97+
polygon fitInExtent: 10 @ 10.
98+
self
99+
assert: polygon vertices
100+
equals:
101+
{(5 @ 2).
102+
(-2 @ 5).
103+
(-5 @ -4)}
104+
]
105+
44106
{ #category : #tests }
45107
GPolygonTest >> testIncludesPoint [
46108
self

src/Geometry/GPolygon.class.st

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,20 @@ GPolygon >> encompasingRectangle [
8585
^Rectangle left: leftMost x right: rightMost x top: top y bottom: bottom y
8686
]
8787

88+
{ #category : #converting }
89+
GPolygon >> fitInExtent: extent [
90+
"I take as parameter a point and will fit the polygon in a rectangle whose dimensions is defined by the extent.
91+
92+
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."
93+
94+
| max verticesInSquareOfSizeOne |
95+
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"
96+
"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."
97+
verticesInSquareOfSizeOne := self vertices collect: [ :point | point / (max * 2) ].
98+
99+
vertices := (verticesInSquareOfSizeOne collect: [ :point | point * extent ])
100+
]
101+
88102
{ #category : #testing }
89103
GPolygon >> includesPoint: point [
90104

@@ -103,7 +117,7 @@ GPolygon >> intersectLineSegment: aLineSegment [
103117
^ aLineSegment intersectPolygon: self
104118
]
105119

106-
{ #category : #private }
120+
{ #category : #converting }
107121
GPolygon >> moveBy: aPoint [
108122
"Move a polygon by a delta defined by aPoint"
109123

0 commit comments

Comments
 (0)