282
|
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 |
|
|
13 |
|
|
14 |
|
284
|
15 |
Geometric subclass:#Curve
|
|
16 |
instanceVariableNames:'startPoint middlePoint endPoint'
|
282
|
17 |
classVariableNames:''
|
|
18 |
poolDictionaries:''
|
|
19 |
category:'Graphics-Geometry'
|
|
20 |
!
|
|
21 |
|
284
|
22 |
!Curve class methodsFor:'documentation'!
|
282
|
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 |
!
|
|
39 |
|
|
40 |
documentation
|
|
41 |
"
|
284
|
42 |
A Curve is a conic section determined by three points
|
|
43 |
that interpolates the first and the third and is tangent to the angle formed
|
|
44 |
by the three points at the first and third points.
|
282
|
45 |
|
|
46 |
[author:]
|
|
47 |
Claus Gittinger
|
|
48 |
|
|
49 |
[see also:]
|
284
|
50 |
Rectangle Polygon EllipticalArc Circle Spline Point LineSegment
|
282
|
51 |
GraphicsContext
|
|
52 |
"
|
|
53 |
|
|
54 |
!
|
|
55 |
|
284
|
56 |
examples
|
|
57 |
"
|
|
58 |
filled & unfilled:
|
|
59 |
[exBegin]
|
|
60 |
|v c|
|
|
61 |
|
|
62 |
v := (View extent:100@100) openAndWait.
|
|
63 |
|
|
64 |
c := Curve start:(20@20) middle:(80@80) end:(20@80).
|
|
65 |
|
|
66 |
v paint:Color blue.
|
|
67 |
c displayFilledOn:v.
|
|
68 |
|
|
69 |
v paint:Color red.
|
|
70 |
c displayStrokedOn:v.
|
|
71 |
[exEnd]
|
|
72 |
|
|
73 |
with a grid (for demonstration):
|
|
74 |
[exBegin]
|
|
75 |
|v c|
|
|
76 |
|
|
77 |
v := (View extent:200@100) openAndWait.
|
282
|
78 |
|
284
|
79 |
v lineStyle:#dashed.
|
|
80 |
v displayLineFrom:(20@0) to:(20@200).
|
|
81 |
v displayLineFrom:(180@0) to:(180@200).
|
|
82 |
|
|
83 |
v displayLineFrom:(0@20) to:(200@20).
|
|
84 |
v displayLineFrom:(0@80) to:(200@80).
|
|
85 |
|
|
86 |
v lineStyle:#solid.
|
|
87 |
c := Curve start:(20@20) middle:(180@80) end:(20@80).
|
282
|
88 |
|
284
|
89 |
v paint:Color blue.
|
|
90 |
c displayFilledOn:v.
|
|
91 |
|
|
92 |
v paint:Color red.
|
|
93 |
c displayStrokedOn:v.
|
|
94 |
|
|
95 |
v paint:(Color black).
|
|
96 |
v displayLineFrom:(20@20) to:(180@80).
|
|
97 |
v displayLineFrom:(20@80) to:(180@80).
|
|
98 |
|
|
99 |
[exEnd]
|
|
100 |
"
|
282
|
101 |
! !
|
|
102 |
|
284
|
103 |
!Curve class methodsFor:'instance creation'!
|
|
104 |
|
|
105 |
from:startPoint to:endPoint through:middlePoint
|
|
106 |
"return a new curve, passing through the three given points"
|
|
107 |
|
|
108 |
^ self new start:startPoint middle:middlePoint end:endPoint
|
|
109 |
|
|
110 |
"Created: 8.5.1996 / 21:16:39 / cg"
|
|
111 |
!
|
|
112 |
|
|
113 |
start:startPoint middle:middlePoint end:endPoint
|
|
114 |
"return a new curve, passing through the three given points"
|
|
115 |
|
|
116 |
^ self new start:startPoint middle:middlePoint end:endPoint
|
|
117 |
|
|
118 |
"Created: 8.5.1996 / 21:19:30 / cg"
|
|
119 |
! !
|
|
120 |
|
|
121 |
!Curve methodsFor:'accessing'!
|
282
|
122 |
|
|
123 |
end
|
|
124 |
"return the endPoint"
|
|
125 |
|
|
126 |
^ endPoint
|
|
127 |
|
284
|
128 |
"Created: 8.5.1996 / 21:08:35 / cg"
|
282
|
129 |
!
|
|
130 |
|
|
131 |
start
|
|
132 |
"return the startPoint"
|
|
133 |
|
|
134 |
^ startPoint
|
|
135 |
|
284
|
136 |
"Created: 8.5.1996 / 21:08:44 / cg"
|
282
|
137 |
!
|
|
138 |
|
284
|
139 |
start:p1 middle:p2 end:p3
|
|
140 |
"set the startPoint, middlePoint and the endPoint"
|
282
|
141 |
|
|
142 |
startPoint := p1.
|
284
|
143 |
middlePoint := p2.
|
|
144 |
endPoint := p3.
|
282
|
145 |
|
284
|
146 |
"Created: 8.5.1996 / 21:09:40 / cg"
|
282
|
147 |
! !
|
|
148 |
|
284
|
149 |
!Curve methodsFor:'displaying'!
|
282
|
150 |
|
|
151 |
displayFilledOn:aGC
|
284
|
152 |
"draw the receiver as a filled curve in a graphicsContext, aGC"
|
282
|
153 |
|
284
|
154 |
aGC fillPolygon:self computeLines
|
282
|
155 |
|
284
|
156 |
"Created: 8.5.1996 / 21:24:15 / cg"
|
|
157 |
"Modified: 8.5.1996 / 21:25:33 / cg"
|
282
|
158 |
!
|
|
159 |
|
|
160 |
displayStrokedOn:aGC
|
284
|
161 |
"draw the receiver as a unfilled curve in a graphicsContext, aGC"
|
282
|
162 |
|
284
|
163 |
aGC displayPolygon:self computeLines
|
282
|
164 |
|
284
|
165 |
"Modified: 8.5.1996 / 21:25:35 / cg"
|
282
|
166 |
! !
|
|
167 |
|
284
|
168 |
!Curve methodsFor:'helpers'!
|
|
169 |
|
|
170 |
computeLines
|
|
171 |
"compute the lines which approxiamte this curve"
|
|
172 |
|
|
173 |
|lines pa pb
|
|
174 |
numberOfSegments "{ Class: SmallInteger }"|
|
|
175 |
|
|
176 |
lines := OrderedCollection new.
|
|
177 |
|
|
178 |
"/ Compute the number of line segments used to approximate the curve.
|
|
179 |
pa := middlePoint - startPoint.
|
|
180 |
pb := endPoint - middlePoint.
|
|
181 |
numberOfSegments := 5 max:pa x abs + pa y abs + pb x abs + pb y abs // 20.
|
|
182 |
|
|
183 |
"/ Add all of the points necessary for the path.
|
|
184 |
lines add:startPoint.
|
|
185 |
1 to:numberOfSegments do:[:seg |
|
|
186 |
lines add:
|
|
187 |
((pa * seg // numberOfSegments + startPoint) * (numberOfSegments - seg)
|
|
188 |
+ ((pb * (seg - 1) // numberOfSegments + middlePoint) * (seg - 1)))
|
|
189 |
// (numberOfSegments - 1)
|
|
190 |
].
|
|
191 |
lines add:endPoint.
|
|
192 |
|
|
193 |
^ lines
|
|
194 |
|
|
195 |
"Created: 8.5.1996 / 21:23:29 / cg"
|
|
196 |
! !
|
|
197 |
|
|
198 |
!Curve methodsFor:'queries'!
|
282
|
199 |
|
|
200 |
bounds
|
|
201 |
"return the smallest enclosing rectangle"
|
|
202 |
|
284
|
203 |
|min max|
|
282
|
204 |
|
284
|
205 |
min := (startPoint min: endPoint) min: middlePoint.
|
|
206 |
max := (startPoint max: endPoint) max: middlePoint.
|
|
207 |
^ min corner:max
|
282
|
208 |
|
|
209 |
"
|
284
|
210 |
(Curve from:10@10 to:50@100 through:50@50) bounds
|
|
211 |
(Curve from:10@10 to:50@100 through:100@50) bounds
|
282
|
212 |
"
|
|
213 |
|
284
|
214 |
"Modified: 8.5.1996 / 21:18:50 / cg"
|
282
|
215 |
! !
|
|
216 |
|
284
|
217 |
!Curve class methodsFor:'documentation'!
|
282
|
218 |
|
|
219 |
version
|
284
|
220 |
^ '$Header: /cvs/stx/stx/libbasic2/LineSegment.st,v 1.2 1996-05-08 19:32:21 cg Exp $'
|
282
|
221 |
! !
|