My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
package com.mrinalwadhwa.primitives
{
import flash.display.Graphics;

import spark.primitives.supportClasses.FilledElement;



public class EllipticalArc extends FilledElement
{

//--------------------------------------------------------------------------
//
// Constructor
//
//--------------------------------------------------------------------------


/**
* Constructor.
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion Flex 4
*/
public function EllipticalArc()
{
super();
}


//--------------------------------------------------------------------------
//
// Properties
//
//--------------------------------------------------------------------------


//-----------------------
// startAngle
//-----------------------

private var _startAngle:Number;

/**
* The beginning angle of the arc. If not specified
* a default value of 0 is used.
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion Flex 4
*/
public function get startAngle():Number
{
if (!_startAngle)
{
return 0;
}
return _startAngle;
}

/**
* @private
*/
public function set startAngle(value:Number):void
{
if (_startAngle != value)
{
_startAngle = value;

invalidateSize();
invalidateDisplayList();
invalidateParentSizeAndDisplayList();
}
}



//-----------------------
// arc
//-----------------------

private var _arc:Number;

/**
* The angular extent of the arc. If not specified
* a default value of 0 is used.
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion Flex 4
*/
public function get arc():Number
{
if (!_arc)
{
return 0;
}
return _arc;
}

/**
* @private
*/
public function set arc(value:Number):void
{
if (_arc != value)
{
_arc = value;

invalidateSize();
invalidateDisplayList();
invalidateParentSizeAndDisplayList();
}
}



//-----------------------
// closureType
//-----------------------

private var _closureType:String;

[Inspectable(category="General",enumeration="open,chord,pie",defaultValue="open")]

/**
* The method in which to close the arc.
* <p>
* <li><b>open</b> will apply no closure.</li>
* <li><b>chord</b> will close the arc with a strait line to the start.</li>
* <li><b>pie</b> will draw a line from center to start and end to center forming a pie shape.</li>
* </p>
**/
public function get closureType():String
{
if (!_closureType)
{
return "open";
}
return _closureType;
}

public function set closureType(value:String):void
{
if (_closureType != value)
{
_closureType = value;

invalidateSize();
invalidateDisplayList();
invalidateParentSizeAndDisplayList();
}
}






//--------------------------------------------------------------------------
//
// Overridden methods
//
//--------------------------------------------------------------------------

/**
* @inheritDoc
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion Flex 4
*/
override protected function draw(g:Graphics):void
{
var newX:Number = (width / 2) + (width / 2) * Math.cos(startAngle * (Math.PI / 180)) + x;
var newY:Number = (height / 2) - (height / 2) * Math.sin(startAngle * (Math.PI / 180)) + y;

var ax:Number;
var ay:Number;

//draw the start line in the case of a pie type
if (closureType == "pie")
{
ax = newX - Math.cos(-(startAngle / 180) * Math.PI) * width / 2;
ay = newY - Math.sin(-(startAngle / 180) * Math.PI) * height / 2;

if (Math.abs(arc) < 360)
{
g.moveTo(ax, ay);
g.lineTo(newX, newY);
}
}

g.moveTo(newX, newY);
drawEllipticalArc(newX, newY, startAngle, arc, (width * 0.5), (height * 0.5), g);


//close the arc if required
if (Math.abs(arc) < 360)
{
if (closureType == "pie")
{
g.lineTo(ax, ay);
}

if (closureType == "chord")
{
g.lineTo(newX, newY);
}
}

}



/**
* @private
*/
protected static function drawEllipticalArc(x:Number, y:Number, startAngle:Number, arc:Number, radius:Number,
yRadius:Number, g:Graphics):void
{

var ax:Number;
var ay:Number;

// Circumvent drawing more than is needed
if (Math.abs(arc) > 360)
{
arc = 360;
}

// Draw in a maximum of 45 degree segments. First we calculate how many
// segments are needed for our arc.
var segs:Number = Math.ceil(Math.abs(arc) / 45);

// Now calculate the sweep of each segment
var segAngle:Number = arc / segs;

// The math requires radians rather than degrees. To convert from degrees
// use the formula (degrees/180)*Math.PI to get radians.
var theta:Number = -(segAngle / 180) * Math.PI;

// convert angle startAngle to radians
var angle:Number = -(startAngle / 180) * Math.PI;

// find our starting points (ax,ay) relative to the secified x,y
ax = x - Math.cos(angle) * radius;
ay = y - Math.sin(angle) * yRadius;

// Draw as 45 degree segments
if (segs > 0)
{
var oldX:Number = x;
var oldY:Number = y;
var cx:Number;
var cy:Number;
var x1:Number;
var y1:Number;

// Loop for drawing arc segments
for (var i:int = 0; i < segs; i++)
{

// increment our angle
angle += theta;

//find the angle halfway between the last angle and the new one,
//calculate our end point, our control point, and draw the arc segment

cx = ax + Math.cos(angle - (theta / 2)) * (radius / Math.cos(theta / 2));

cy = ay + Math.sin(angle - (theta / 2)) * (yRadius / Math.cos(theta / 2));

x1 = ax + Math.cos(angle) * radius;

y1 = ay + Math.sin(angle) * yRadius;


g.curveTo(cx, cy, x1, y1);

oldX = x1;
oldY = y1;
}
}
}

}
}

Change log

r2 by mrinal.wadhwa on Jul 20, 2009   Diff
Intial checkin of EllipticalArc.as and and
example

The class works properly at this point ...
it does not implement some of the
layouting related features that other
spark primitives like Rect and Ellipse do
.. but it seems to work ok with basic
layouting
Go to: 
Project members, sign in to write a code review

Older revisions

All revisions of this file

File info

Size: 7927 bytes, 293 lines
Powered by Google Project Hosting