author | Claus Gittinger <cg@exept.de> |
Tue, 25 Jun 2019 11:09:04 +0200 | |
changeset 5048 | 63f42cfbaf59 |
parent 2156 | 27b688e0dd31 |
permissions | -rw-r--r-- |
285 | 1 |
" |
2 |
COPYRIGHT (c) 1996 by Claus Gittinger |
|
3 |
All Rights Reserved |
|
4 |
||
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
1303 | 12 |
"{ Package: 'stx:libbasic2' }" |
13 |
||
285 | 14 |
Geometric subclass:#Curve |
15 |
instanceVariableNames:'startPoint middlePoint endPoint' |
|
16 |
classVariableNames:'' |
|
17 |
poolDictionaries:'' |
|
1303 | 18 |
category:'Graphics-Geometry-Objects' |
285 | 19 |
! |
20 |
||
21 |
!Curve class methodsFor:'documentation'! |
|
22 |
||
23 |
copyright |
|
24 |
" |
|
25 |
COPYRIGHT (c) 1996 by Claus Gittinger |
|
26 |
All Rights Reserved |
|
27 |
||
28 |
This software is furnished under a license and may be used |
|
29 |
only in accordance with the terms of that license and with the |
|
30 |
inclusion of the above copyright notice. This software may not |
|
31 |
be provided or otherwise made available to, or used by, any |
|
32 |
other person. No title to or ownership of the software is |
|
33 |
hereby transferred. |
|
34 |
" |
|
35 |
! |
|
36 |
||
37 |
documentation |
|
38 |
" |
|
39 |
A Curve is a conic section determined by three points |
|
40 |
that interpolates the first and the third and is tangent to the angle formed |
|
41 |
by the three points at the first and third points. |
|
42 |
||
43 |
[author:] |
|
44 |
Claus Gittinger |
|
45 |
||
46 |
[see also:] |
|
47 |
Rectangle Polygon EllipticalArc Circle Spline Point LineSegment |
|
331 | 48 |
ArrowedSpline Arrow |
285 | 49 |
GraphicsContext |
331 | 50 |
StrokingWrapper FillingWrapper |
285 | 51 |
" |
52 |
! |
|
53 |
||
54 |
examples |
|
55 |
" |
|
56 |
filled & unfilled: |
|
57 |
[exBegin] |
|
58 |
|v c| |
|
59 |
||
60 |
v := (View extent:100@100) openAndWait. |
|
61 |
||
62 |
c := Curve start:(20@20) middle:(80@80) end:(20@80). |
|
63 |
||
64 |
v paint:Color blue. |
|
65 |
c displayFilledOn:v. |
|
66 |
||
67 |
v paint:Color red. |
|
68 |
c displayStrokedOn:v. |
|
69 |
[exEnd] |
|
70 |
||
71 |
with a grid (for demonstration): |
|
72 |
[exBegin] |
|
73 |
|v c| |
|
74 |
||
75 |
v := (View extent:200@100) openAndWait. |
|
76 |
||
77 |
v lineStyle:#dashed. |
|
78 |
v displayLineFrom:(20@0) to:(20@200). |
|
79 |
v displayLineFrom:(180@0) to:(180@200). |
|
80 |
||
81 |
v displayLineFrom:(0@20) to:(200@20). |
|
82 |
v displayLineFrom:(0@80) to:(200@80). |
|
83 |
||
84 |
v lineStyle:#solid. |
|
85 |
c := Curve start:(20@20) middle:(180@80) end:(20@80). |
|
86 |
||
87 |
v paint:Color blue. |
|
88 |
c displayFilledOn:v. |
|
89 |
||
90 |
v paint:Color red. |
|
91 |
c displayStrokedOn:v. |
|
92 |
||
93 |
v paint:(Color black). |
|
94 |
v displayLineFrom:(20@20) to:(180@80). |
|
95 |
v displayLineFrom:(20@80) to:(180@80). |
|
96 |
||
97 |
[exEnd] |
|
98 |
" |
|
99 |
! ! |
|
100 |
||
101 |
!Curve class methodsFor:'instance creation'! |
|
102 |
||
103 |
from:startPoint to:endPoint through:middlePoint |
|
104 |
"return a new curve, passing through the three given points" |
|
105 |
||
106 |
^ self new start:startPoint middle:middlePoint end:endPoint |
|
107 |
||
108 |
"Created: 8.5.1996 / 21:16:39 / cg" |
|
109 |
! |
|
110 |
||
111 |
start:startPoint middle:middlePoint end:endPoint |
|
112 |
"return a new curve, passing through the three given points" |
|
113 |
||
114 |
^ self new start:startPoint middle:middlePoint end:endPoint |
|
115 |
||
116 |
"Created: 8.5.1996 / 21:19:30 / cg" |
|
346 | 117 |
! |
118 |
||
119 |
with:startPoint with:middlePoint with:endPoint |
|
120 |
"return a new curve, passing through the three given points" |
|
121 |
||
122 |
^ self new start:startPoint middle:middlePoint end:endPoint |
|
123 |
||
124 |
"Created: 17.5.1996 / 10:22:47 / cg" |
|
285 | 125 |
! ! |
126 |
||
127 |
!Curve methodsFor:'accessing'! |
|
128 |
||
129 |
end |
|
130 |
"return the endPoint" |
|
131 |
||
132 |
^ endPoint |
|
133 |
||
134 |
"Created: 8.5.1996 / 21:08:35 / cg" |
|
135 |
! |
|
136 |
||
137 |
start |
|
138 |
"return the startPoint" |
|
139 |
||
140 |
^ startPoint |
|
141 |
||
142 |
"Created: 8.5.1996 / 21:08:44 / cg" |
|
143 |
! |
|
144 |
||
145 |
start:p1 middle:p2 end:p3 |
|
146 |
"set the startPoint, middlePoint and the endPoint" |
|
147 |
||
148 |
startPoint := p1. |
|
149 |
middlePoint := p2. |
|
150 |
endPoint := p3. |
|
151 |
||
152 |
"Created: 8.5.1996 / 21:09:40 / cg" |
|
153 |
! ! |
|
154 |
||
289 | 155 |
!Curve methodsFor:'converting'! |
156 |
||
157 |
asPointArray |
|
158 |
"return an array containing my points." |
|
159 |
||
160 |
^ Array with:startPoint with:middlePoint with:endPoint |
|
161 |
||
162 |
"Created: 9.5.1996 / 01:07:07 / cg" |
|
163 |
! |
|
164 |
||
165 |
asPolygon |
|
166 |
"return a polygon, approximating the spline" |
|
167 |
||
168 |
^ Polygon vertices:(self computeLineSegments) |
|
169 |
||
170 |
"Created: 9.5.1996 / 01:08:12 / cg" |
|
171 |
! ! |
|
172 |
||
285 | 173 |
!Curve methodsFor:'displaying'! |
174 |
||
175 |
displayFilledOn:aGC |
|
176 |
"draw the receiver as a filled curve in a graphicsContext, aGC" |
|
177 |
||
289 | 178 |
aGC fillPolygon:self computeLineSegments |
285 | 179 |
|
180 |
"Created: 8.5.1996 / 21:24:15 / cg" |
|
289 | 181 |
"Modified: 9.5.1996 / 01:08:34 / cg" |
285 | 182 |
! |
183 |
||
184 |
displayStrokedOn:aGC |
|
185 |
"draw the receiver as a unfilled curve in a graphicsContext, aGC" |
|
186 |
||
289 | 187 |
aGC displayPolygon:self computeLineSegments |
285 | 188 |
|
289 | 189 |
"Modified: 9.5.1996 / 01:08:36 / cg" |
285 | 190 |
! ! |
191 |
||
192 |
!Curve methodsFor:'helpers'! |
|
193 |
||
289 | 194 |
computeLineSegments |
285 | 195 |
"compute the lines which approxiamte this curve" |
196 |
||
197 |
|lines pa pb |
|
198 |
numberOfSegments "{ Class: SmallInteger }"| |
|
199 |
||
200 |
lines := OrderedCollection new. |
|
201 |
||
202 |
"/ Compute the number of line segments used to approximate the curve. |
|
203 |
pa := middlePoint - startPoint. |
|
204 |
pb := endPoint - middlePoint. |
|
205 |
numberOfSegments := 5 max:pa x abs + pa y abs + pb x abs + pb y abs // 20. |
|
206 |
||
207 |
"/ Add all of the points necessary for the path. |
|
208 |
lines add:startPoint. |
|
209 |
1 to:numberOfSegments do:[:seg | |
|
210 |
lines add: |
|
211 |
((pa * seg // numberOfSegments + startPoint) * (numberOfSegments - seg) |
|
212 |
+ ((pb * (seg - 1) // numberOfSegments + middlePoint) * (seg - 1))) |
|
213 |
// (numberOfSegments - 1) |
|
214 |
]. |
|
215 |
lines add:endPoint. |
|
216 |
||
217 |
^ lines |
|
218 |
||
289 | 219 |
"Created: 9.5.1996 / 01:08:21 / cg" |
285 | 220 |
! ! |
221 |
||
222 |
!Curve methodsFor:'queries'! |
|
223 |
||
224 |
bounds |
|
225 |
"return the smallest enclosing rectangle" |
|
226 |
||
227 |
|min max| |
|
228 |
||
229 |
min := (startPoint min: endPoint) min: middlePoint. |
|
230 |
max := (startPoint max: endPoint) max: middlePoint. |
|
231 |
^ min corner:max |
|
232 |
||
233 |
" |
|
234 |
(Curve from:10@10 to:50@100 through:50@50) bounds |
|
235 |
(Curve from:10@10 to:50@100 through:100@50) bounds |
|
236 |
" |
|
237 |
||
238 |
"Modified: 8.5.1996 / 21:18:50 / cg" |
|
2156 | 239 |
! ! |
240 |
||
241 |
!Curve methodsFor:'testing'! |
|
374
a77f804c605e
added #canBeFilled for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
346
diff
changeset
|
242 |
|
a77f804c605e
added #canBeFilled for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
346
diff
changeset
|
243 |
canBeFilled |
a77f804c605e
added #canBeFilled for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
346
diff
changeset
|
244 |
"return true, if the receiver can be drawn as a filled geometric. |
a77f804c605e
added #canBeFilled for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
346
diff
changeset
|
245 |
Always true here." |
a77f804c605e
added #canBeFilled for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
346
diff
changeset
|
246 |
|
a77f804c605e
added #canBeFilled for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
346
diff
changeset
|
247 |
^ true |
a77f804c605e
added #canBeFilled for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
346
diff
changeset
|
248 |
|
285 | 249 |
! ! |
250 |
||
251 |
!Curve class methodsFor:'documentation'! |
|
252 |
||
253 |
version |
|
2156 | 254 |
^ '$Header: /cvs/stx/stx/libbasic2/Curve.st,v 1.8 2009-06-06 10:12:21 cg Exp $' |
285 | 255 |
! ! |