2316
|
1 |
"{ Package: 'stx:libbasic2' }"
|
|
2 |
|
|
3 |
Geometric subclass:#CurveFitter
|
|
4 |
instanceVariableNames:'points'
|
|
5 |
classVariableNames:''
|
|
6 |
poolDictionaries:''
|
|
7 |
category:'Graphics-Geometry-Objects'
|
|
8 |
!
|
|
9 |
|
|
10 |
!CurveFitter class methodsFor:'documentation'!
|
|
11 |
|
|
12 |
documentation
|
|
13 |
"
|
|
14 |
I represent a conic section determined by three points p1, p2 and p3.
|
|
15 |
I interpolate p1 and p3 and am tangent to line p1, p2 at p1 and line p3, p2 at p3.
|
|
16 |
|
|
17 |
[see also:]
|
|
18 |
Polygon LineSegment Circle EllipticalArc
|
|
19 |
Rectangle Curve Arrow Spline ArrowedSpline
|
|
20 |
|
|
21 |
[author:]
|
|
22 |
?
|
|
23 |
adapted to ST/X by Claus Gittinger
|
|
24 |
"
|
|
25 |
!
|
|
26 |
|
|
27 |
examples
|
|
28 |
"
|
|
29 |
[exBegin]
|
|
30 |
|v s|
|
|
31 |
|
|
32 |
v := (View extent:100@100) openAndWait.
|
|
33 |
|
|
34 |
v displayLineFrom:(0@20) to:(100@20).
|
|
35 |
v displayLineFrom:(0@80) to:(100@80).
|
|
36 |
v displayLineFrom:(0@60) to:(100@60).
|
|
37 |
v displayLineFrom:(20@0) to:(20@100).
|
|
38 |
v displayLineFrom:(80@0) to:(80@100).
|
|
39 |
|
|
40 |
s := CurveFitter new points:
|
|
41 |
(Array with:(20@20)
|
|
42 |
with:(80@60)
|
|
43 |
with:(20@80)).
|
|
44 |
|
|
45 |
'
|
|
46 |
v paint:Color blue.
|
|
47 |
s displayFilledOn:v.
|
|
48 |
'.
|
|
49 |
|
|
50 |
v paint:Color red.
|
|
51 |
s displayStrokedOn:v.
|
|
52 |
[exEnd]
|
|
53 |
|
|
54 |
"
|
|
55 |
! !
|
|
56 |
|
|
57 |
!CurveFitter methodsFor:'accessing'!
|
|
58 |
|
|
59 |
points
|
|
60 |
"the collection of points which are used to approximate the curve"
|
|
61 |
|
|
62 |
^ points
|
|
63 |
!
|
|
64 |
|
|
65 |
points:aCollectionOfPoints
|
|
66 |
"set the collection of points which are used to approximate the curve"
|
|
67 |
|
|
68 |
points := aCollectionOfPoints.
|
|
69 |
! !
|
|
70 |
|
|
71 |
!CurveFitter methodsFor:'displaying'!
|
|
72 |
|
|
73 |
displayStrokedOn:aDisplayMedium
|
|
74 |
| pa pb k s p1 p2 p3 |
|
|
75 |
|
|
76 |
points size < 3 ifTrue: [ self error: 'Curve must have three points' ].
|
|
77 |
|
|
78 |
p1 := points at:1.
|
|
79 |
p2 := points at:2.
|
|
80 |
p3 := points at:3.
|
|
81 |
|
|
82 |
s := Polygon new.
|
|
83 |
s add: p1.
|
|
84 |
pa := p2 - p1.
|
|
85 |
pb := p3 - p2.
|
|
86 |
k := 5 max: pa x abs + pa y abs + pb x abs + pb y abs // 20.
|
|
87 |
|
|
88 |
"k is a guess as to how many line segments to use to approximate the curve."
|
|
89 |
1 to: k do:[:i |
|
|
90 |
s add: pa * i // k + p1 * (k - i) + (pb * (i - 1) // k + p2 * (i - 1)) // (k - 1)
|
|
91 |
].
|
|
92 |
s add: p3.
|
|
93 |
s displayOn:aDisplayMedium
|
|
94 |
! !
|
|
95 |
|
|
96 |
!CurveFitter class methodsFor:'documentation'!
|
|
97 |
|
|
98 |
version_CVS
|
|
99 |
^ '$Header: /cvs/stx/stx/libbasic2/CurveFitter.st,v 1.1 2009-10-26 21:14:55 cg Exp $'
|
|
100 |
! !
|