2150 |
mathias |
1 |
<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" >
|
|
|
2 |
<head>
|
|
|
3 |
<title>Approximation of an arc with bezier</title>
|
|
|
4 |
<style type="text/css">
|
|
|
5 |
@import "../../../dojo/resources/dojo.css";
|
|
|
6 |
@import "../../../dijit/tests/css/dijitTests.css";
|
|
|
7 |
</style>
|
|
|
8 |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
|
|
9 |
<!--
|
|
|
10 |
The next line should include Microsoft's Silverligth.js, if you plan to use the silverlight backend
|
|
|
11 |
<script type="text/javascript" src="Silverlight.js"></script>
|
|
|
12 |
-->
|
|
|
13 |
<script type="text/javascript" src="../../../dojo/dojo.js" djConfig="isDebug: true"></script>
|
|
|
14 |
<script type="text/javascript" src="../_base.js"></script>
|
|
|
15 |
<script type="text/javascript" src="../shape.js"></script>
|
|
|
16 |
<script type="text/javascript" src="../path.js"></script>
|
|
|
17 |
<script type="text/javascript" src="../arc.js"></script>
|
|
|
18 |
<!--<script type="text/javascript" src="../vml.js"></script>-->
|
|
|
19 |
<!--<script type="text/javascript" src="../svg.js"></script>-->
|
|
|
20 |
<!--<script type="text/javascript" src="../canvas.js"></script>-->
|
|
|
21 |
<!--<script type="text/javascript" src="../silverlight.js"></script>-->
|
|
|
22 |
<script type="text/javascript">
|
|
|
23 |
dojo.require("dojox.gfx");
|
|
|
24 |
|
|
|
25 |
makeShapes = function(){
|
|
|
26 |
var surface = dojox.gfx.createSurface("test", 500, 300);
|
|
|
27 |
var g = surface.createGroup();
|
|
|
28 |
|
|
|
29 |
// create a reference ellipse
|
|
|
30 |
var rx = 200;
|
|
|
31 |
var ry = 100;
|
|
|
32 |
var startAngle = -30;
|
|
|
33 |
var arcAngle = -90;
|
|
|
34 |
var axisAngle = -30;
|
|
|
35 |
var e = g.createEllipse({rx: rx, ry: ry}).setStroke({});
|
|
|
36 |
|
|
|
37 |
// calculate a bezier
|
|
|
38 |
var alpha = dojox.gfx.matrix._degToRad(arcAngle) / 2; // half of our angle
|
|
|
39 |
var cosa = Math.cos(alpha);
|
|
|
40 |
var sina = Math.sin(alpha);
|
|
|
41 |
// start point
|
|
|
42 |
var p1 = {x: cosa, y: sina};
|
|
|
43 |
// 1st control point
|
|
|
44 |
var p2 = {x: cosa + (4 / 3) * (1 - cosa), y: sina - (4 / 3) * cosa * (1 - cosa) / sina};
|
|
|
45 |
// 2nd control point (symmetric to the 1st one)
|
|
|
46 |
var p3 = {x: p2.x, y: -p2.y};
|
|
|
47 |
// end point (symmetric to the start point)
|
|
|
48 |
var p4 = {x: p1.x, y: -p1.y};
|
|
|
49 |
|
|
|
50 |
// rotate and scale poins as appropriate
|
|
|
51 |
var s = dojox.gfx.matrix.normalize([dojox.gfx.matrix.scale(e.shape.rx, e.shape.ry), dojox.gfx.matrix.rotateg(startAngle + arcAngle / 2)]);
|
|
|
52 |
p1 = dojox.gfx.matrix.multiplyPoint(s, p1);
|
|
|
53 |
p2 = dojox.gfx.matrix.multiplyPoint(s, p2);
|
|
|
54 |
p3 = dojox.gfx.matrix.multiplyPoint(s, p3);
|
|
|
55 |
p4 = dojox.gfx.matrix.multiplyPoint(s, p4);
|
|
|
56 |
// draw control trapezoid
|
|
|
57 |
var t = g.createPath().setStroke({color: "blue"});
|
|
|
58 |
t.moveTo(p1.x, p1.y);
|
|
|
59 |
t.lineTo(p2.x, p2.y);
|
|
|
60 |
t.lineTo(p3.x, p3.y);
|
|
|
61 |
t.lineTo(p4.x, p4.y);
|
|
|
62 |
t.lineTo(p1.x, p1.y);
|
|
|
63 |
t.moveTo((p1.x + p4.x) / 2, (p1.y + p4.y) / 2);
|
|
|
64 |
t.lineTo((p2.x + p3.x) / 2, (p2.y + p3.y) / 2);
|
|
|
65 |
t.moveTo((p1.x + p2.x) / 2, (p1.y + p2.y) / 2);
|
|
|
66 |
t.lineTo((p3.x + p4.x) / 2, (p3.y + p4.y) / 2);
|
|
|
67 |
// draw a bezier
|
|
|
68 |
var b = g.createPath().setStroke({color: "red"});
|
|
|
69 |
b.moveTo(p1.x, p1.y);
|
|
|
70 |
b.curveTo(p2.x, p2.y, p3.x, p3.y, p4.x, p4.y);
|
|
|
71 |
// move everything in a middle
|
|
|
72 |
g.setTransform([dojox.gfx.matrix.translate(250, 150), dojox.gfx.matrix.rotateg(axisAngle)]);
|
|
|
73 |
};
|
|
|
74 |
|
|
|
75 |
dojo.addOnLoad(makeShapes);
|
|
|
76 |
|
|
|
77 |
</script>
|
|
|
78 |
</head>
|
|
|
79 |
<body>
|
|
|
80 |
<h1>Approximation of an arc with bezier</h1>
|
|
|
81 |
<!--<p><button onclick="makeShapes();">Make</button></p>-->
|
|
|
82 |
<div id="test" style="width: 500px; height: 300px;"></div>
|
|
|
83 |
<p>That's all Folks!</p>
|
|
|
84 |
</body>
|
|
|
85 |
</html>
|