These are the handlers used. It is useful to have this page open alongside the discussion pages.
1 -- Module SetProperties
2 -- ====================
3 setprop pLoc fLoc
4 -- Move the polygon to the given location, i.e. set its defining origin to fLoc.
5 -- fLoc is in display coordinates (i.e. Y pointing down and origin at top left of window)
6 if the number of parts of the target > 0 then
7 repeat with iPart = 1 to the number of controls of the target
8 set the pLoc of control iPart of the target to fLoc
9 end repeat
10 lock messages
11 set the pLoc of the target to fLoc
12 unlock messages
13 else
14 lock messages
15 put the target
16 set the pLoc of the target to fLoc
17 -- Recompute the display points from the design
18 put the pAngle of the target into lAngle
19 put the pRThetas of the target into lRThetas
20 repeat with i=1 to the number of lines of lRThetas
21 if line i of lRThetas is not empty then add lAngle to item 2 of line i of lRThetas
22 end repeat
23 set the points of the target to DisplayPoints(PolarToCartesian(lRThetas),fLoc)
24 unlock messages
25 end if
26 end pLoc
27 setprop pAngle fAngle
28 -- Turn the polygon or group of polygons to the given angle, using its defining origin as its centre.
29 if the number of parts of the target > 0 then
30 repeat with iPart = 1 to the number of controls of the target
31 set the pAngle of control iPart of the target to fAngle
32 end repeat
33 lock messages
34 set the pAngle of the target to fAngle
35 unlock messages
36 else
37 lock messages
38 set the pAngle of the target to fAngle
39 -- Recompute the display points from the design
40 put the pRThetas of the target into lRThetas
41 repeat with i=1 to the number of lines of lRThetas
42 if line i of lRThetas is not empty then add fAngle to item 2 of line i of lRThetas
43 end repeat
44 set the points of the target to DisplayPoints(PolarToCartesian(lRThetas),the pLoc of the target)
45 unlock messages
46 end if
47 end pAngle
48 setprop pPoints fPoints
49 -- Set the points of the polygon.
50 -- Normally used once, for designing the polygon's shape.
51 lock messages
52 set the pPoints of the target to fPoints
53 set the pRThetas of the target to CartesianToPolar(fPoints)
54 unlock messages
55 end pPoints
56 setprop pRThetas fRThetas
57 -- Set the points of the polygon.
58 -- Normally used once, for designing the polygon's shape.
59 lock messages
60 set the pRThetas of the target to fRThetas
61 set the pPoints of the target to PolarToCartesian(fRThetas)
62 unlock messages
63 end pRThetas
64 function DisplayPoints fPoints,fLoc
65 -- Return the points translated to fLoc, converted to display-Y and rounded.
66 -- This is the only routine that returns integers.
67 put item 1 of fLoc into x; put item 2 of fLoc into y
68 repeat with i=1 to the number of lines of fPoints
69 put line i of fPoints into lPoint
70 if lPoint is empty then next repeat
71 put round(x + item 1 of lPoint),round(y - item 2 of lPoint) into line i of lDisplayPoints
72 end repeat
73 return lDisplayPoints
74 end DisplayPoints
75 on PreparePolygon
76 -- Auxiliary routine to help set up one set of coordinates from the other one,
77 -- after the polygon has been designed. Not really necessary if pPoints or
78 -- pRThetas is used instead.
79 -- Call only once, after designing the polygon.
80 put the pRThetas of the target into lRThetas
81 if lRThetas is empty then -- assume the user designed the polygon in cartesian coordinates
82 if the pPoints of the target is empty then
83 answer "Graphic "&(the short name of the target)&" has not been designed" as sheet
84 exit PreparePolygon
85 end if
86 set the pRThetas of the target to CartesianToPolar(the pPoints of the target)
87 else -- assume the user designed the polygon in polar coordinates
88 set the pPoints of the target to PolarToCartesian(lRThetas)
89 end if
90 set the points of the target to DisplayPoints(the pPoints of the target,the pLoc of the target)
91 end PreparePolygon
92 on PrepareParts
93 -- Call only once for each group of graphics!
94 -- set the pRThetas or pPoints of all parts
95 repeat with iPart = 1 to the number of controls of the target
96 put the pRThetas of control iPart of the target into lRThetas
97 if lRThetas is empty then
98 if the pPoints of control iPart of the target is empty then
99 answer "Graphic "&(the short name of part iPart of the target)&" has not been designed" as sheet
100 exit PrepareParts
101 end if
102 set the pRThetas of control iPart of the target to CartesianToPolar(the pPoints of control iPart of the target)
103 else
104 set the pPoints of control iPart of the target to PolarToCartesian(lRThetas)
105 end if
106 set the pDisplayPoints of control iPart of the target to DisplayPoints(the pPoints of control iPart of the target)
107 end repeat
108 end PrepareParts
109 -- Module Action
110 -- =============
111 on MoveRotate fTarget
112 -- Use this in the MouseDown script of a polygon or group of polygons if you want
113 -- to move and rotate it interactively
114 put the mouseloc into lmouse
115 put item 1 of the pLoc of fTarget into X; put item 2 of the pLoc of fTarget into Y
116 if the optionkey is up then -- move the graphic or group
117 -- compute offset of point where the mouse went down:
118 put item 1 of lmouse - X into dx; put item 2 of lmouse - Y into dy
119 repeat while the mouse is down
120 put the mouseloc into lmouse
121 set the pLoc of fTarget to (item 1 of lmouse - dx,item 2 of lmouse - dy)
122 end repeat
123 else -- rotate the graphic or group
124 -- compute angle offset of point where mouse went down:
125 put (item 1 of lmouse - X),(Y - item 2 of lmouse) into dV
126 put item 2 of PolarRTheta(dV) - the pAngle of fTarget into dtheta
127 repeat while the mouse is down
128 put the mouseloc into lmouse
129 put (item 1 of lmouse - X),(Y - item 2 of lmouse) into dV
130 set the pAngle of fTarget to (item 2 of PolarRTheta(dV)) - dtheta
131 end repeat
132 end if
133 end MoveRotate
134 -- Module Conversion
135 -- =================
136 function CartesianToPolar fPoints
137 -- Convert a set of cartesian x,y pairs (one per line) to a set of r,theta values (one per line)
138 repeat with i=1 to the number of lines of fPoints
139 put line i of fPoints into lP
140 if lP is empty then next repeat
141 put item 1 of lP into x; put item 2 of lP into y
142 put PolarRTheta(lP) into line i of lRThetas
143 end repeat
144 return lRThetas
145 end CartesianToPolar
146 function PolarToCartesian fRThetas
147 -- Convert a set of r-theta values (one per line) to a set of points (one per line)
148 repeat with i=1 to the number of lines of fRThetas
149 put line i of fRThetas into lRTheta
150 if lRTheta is empty then next repeat
151 put CartesianXY(lRTheta) into line i of lPoints
152 end repeat
153 return lPoints
154 end PolarToCartesian
155 -- Module Polar-Cartesian conversions
156 -- ==================================
157 -- The angle theta returned is always between 0 and 2pi: 0<=theta<2pi
158 function PolarRTheta fPoint
159 -- Return r and theta as items 1 and 2
160 put item 1 of fPoint into x; put item 2 of fPoint into y
161 put sqrt(x*x+y*y) into r
162 switch
163 case x>0
164 if y>=0 then return r&","&atan(y/x) else return r&","&(2*pi+atan(y/x))
165 break
166 case x<0
167 return r&","&(pi-atan(-y/x))
168 break
169 case x=0
170 if y>0 then return r&","&(pi/2) else return r&","&(3*pi/2)
171 break
172 end switch
173 end PolarRTheta
174 function CartesianXY fRTheta
175 --Return x and y as items 1 and 2
176 put item 1 of fRTheta into r; put item 2 of fRTheta into theta
177 return r*cos(theta) &","& r*sin(theta)
178 end CartesianXY
179 -- Functions for parts of coordinates
180 --
181 -- They are not used in this stack; they are given for illustration only.
182 function PolarR x,y
183 -- Return the radius of a point
184 return sqrt(x*x+y*y)
185 end PolarR
186 function PolarTheta x,y
187 -- Return the angle of a point
188 switch
189 case x>0
190 if y>=0 then return atan(y/x) else return (2*pi+atan(y/x))
191 break
192 case x<0
193 return (pi-atan(-y/x))
194 break
195 case x=0
196 if y>0 then return pi/2 else return 3*pi/2
197 break
198 end switch
199 end PolarTheta
200 function CartesianX r,theta
201 --Return the abscissa of a point
202 return r*cos(theta)
203 end CartesianX
204 function CartesianY r,theta
205 --Return the ordinate of a point
206 return r*sin(theta)
207 end CartesianY
Copyright R. Cailliau (robert at robertcailliau dot eu)
next planned revision: 2009-11