You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
75 lines
2.8 KiB
75 lines
2.8 KiB
<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" > |
|
<head> |
|
<title>Approximation of an arc with bezier</title> |
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> |
|
<script type="text/javascript">djConfig = { isDebug: true };</script> |
|
<script type="text/javascript" src="../../dojo.js"></script> |
|
<script type="text/javascript">dojo.require("dojo.debug.console");</script> |
|
<!--<script type="text/javascript" src="../../src/gfx/common.js"></script>--> |
|
<!--<script type="text/javascript" src="../../src/gfx/path.js"></script>--> |
|
<!--<script type="text/javascript" src="../../src/gfx/vml.js"></script>--> |
|
<!--<script type="text/javascript" src="../../src/gfx/svg.js"></script>--> |
|
<script type="text/javascript"> |
|
dojo.require("dojo.gfx.*"); |
|
|
|
makeShapes = function(){ |
|
var surface = dojo.gfx.createSurface("test", 500, 300); |
|
var g = surface.createGroup(); |
|
|
|
// create a reference ellipse |
|
var rx = 200; |
|
var ry = 100; |
|
var startAngle = 30; |
|
var arcAngle = 90; |
|
var axisAngle = 30; |
|
var e = g.createEllipse({rx: rx, ry: ry}).setStroke({}); |
|
|
|
// calculate a bezier |
|
var alpha = dojo.math.degToRad(arcAngle) / 2; // half of our angle |
|
var cosa = Math.cos(alpha); |
|
var sina = Math.sin(alpha); |
|
// start point |
|
var p1 = {x: cosa, y: sina}; |
|
// 1st control point |
|
var p2 = {x: cosa + (4 / 3) * (1 - cosa), y: sina - (4 / 3) * cosa * (1 - cosa) / sina}; |
|
// 2nd control point (symmetric to the 1st one) |
|
var p3 = {x: p2.x, y: -p2.y}; |
|
// end point (symmetric to the start point) |
|
var p4 = {x: p1.x, y: -p1.y}; |
|
|
|
// rotate and scale poins as appropriate |
|
var s = dojo.gfx.matrix.normalize([dojo.gfx.matrix.scale(e.shape.rx, e.shape.ry), dojo.gfx.matrix.rotateg(startAngle + arcAngle / 2)]); |
|
p1 = dojo.gfx.matrix.multiplyPoint(s, p1); |
|
p2 = dojo.gfx.matrix.multiplyPoint(s, p2); |
|
p3 = dojo.gfx.matrix.multiplyPoint(s, p3); |
|
p4 = dojo.gfx.matrix.multiplyPoint(s, p4); |
|
// draw control trapezoid |
|
var t = g.createPath().setStroke({color: "blue"}); |
|
t.moveTo(p1.x, p1.y); |
|
t.lineTo(p2.x, p2.y); |
|
t.lineTo(p3.x, p3.y); |
|
t.lineTo(p4.x, p4.y); |
|
t.lineTo(p1.x, p1.y); |
|
t.moveTo((p1.x + p4.x) / 2, (p1.y + p4.y) / 2); |
|
t.lineTo((p2.x + p3.x) / 2, (p2.y + p3.y) / 2); |
|
t.moveTo((p1.x + p2.x) / 2, (p1.y + p2.y) / 2); |
|
t.lineTo((p3.x + p4.x) / 2, (p3.y + p4.y) / 2); |
|
// draw a bezier |
|
var b = g.createPath().setStroke({color: "red"}); |
|
b.moveTo(p1.x, p1.y); |
|
b.curveTo(p2.x, p2.y, p3.x, p3.y, p4.x, p4.y); |
|
// move everything in a middle |
|
g.setTransform([dojo.gfx.matrix.translate(250, 150), dojo.gfx.matrix.rotateg(axisAngle)]); |
|
}; |
|
|
|
dojo.addOnLoad(makeShapes); |
|
|
|
</script> |
|
</head> |
|
<body> |
|
<p>Approximation of an arc with bezier</p> |
|
<!--<p><button onclick="makeShapes();">Make</button></p>--> |
|
<div id="test" style="width: 500px; height: 300px;"></div> |
|
<p>That's all Folks!</p> |
|
</body> |
|
</html>
|
|
|