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