~eda-qa/dhlib/main

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
/* <license>
 * This file is part of the dis-Emi-A HaXe Library. Copyright (c) edA-qa mort-ora-y
 * For full copyright and license information please refer to doc/license.txt.
 * </license> 
 */
package flashx;

#if neko
import neash.display.Graphics;
#elseif flash
import flash.display.Graphics;
#end

import mathx.Point2;

class GraphicsUtil
{
	/**
	 * Draws an approximate arc of a circle.  The arc segment may be
	 * in either direction and of any size (though no less than a dot will
	 * be drawn and no more than a circle)
	 *
	 * @param	graphics	[in,out] where to draw
	 * @param	cx,cy	[in] center of the elliptic arc
	 * @param	rx,ry	[in] radius of the ellipse
	 * @param	ast,ae	[in] start and end arc position
	 * @param	wedge	[in] true to draw a wedge, false for an arc
	 *
	 * @DEPREACTED use facility in GraphicsX (code copied there and modified)
	 */
	static public function drawArc( graphics : Graphics,
		cx : Float, cy : Float, 
		rx : Float, ry : Float,
		ast : Float, ae : Float,
		wedge : Bool )
	{
		var angDiff = ae - ast;
		if( angDiff == 0 )
			angDiff = Math.PI/1000;	//a minimal curve in all cases (a dot)
		if( angDiff > (2*Math.PI) )
			angDiff = 2*Math.PI;	//do not overdraw
		
		//only about 8 segments are needed to do a full convincing circle
		//so here calculate an amount based on the curve size, and use
		//10 just for extra prettiness :)
		var segments = Math.ceil( Math.abs(angDiff) / (2*Math.PI) * 10 );
		
		//for tangents to the curves
		var tan = Math.tan( angDiff / segments / 2 );	
	
		var sx = Math.cos( ast ) * rx + cx;
		var sy = Math.sin( ast ) * ry + cy;
		if( wedge )
		{
			graphics.moveTo( cx, cy );
			graphics.lineTo( sx, sy );
		}
		else
			graphics.moveTo( sx, sy );
		
		//use 8 segments... (optimize based on length of curve...)
		var angDelta = angDiff / segments ;
		for( ndx in 1...segments+1 )
		{
			var ang = ast + angDelta * ndx;
			
   			var endx = Math.cos( ang ) * rx;
   			var endy = Math.sin( ang ) * ry;
   			
   			//control point (tangents to both endpoints)
   			var ctlx = endx + rx * tan * Math.cos( ang - Math.PI/2 );
   			var ctly = endy + ry * tan * Math.sin( ang - Math.PI/2 );
   			graphics.curveTo( ctlx + cx, ctly + cy, endx + cx, endy + cy);
		}		
		
		if( wedge )
			graphics.lineTo( cx, cy );
	}
}