Skip to content

Commit

Permalink
Fixed newlines and very minor shortening of arc math
Browse files Browse the repository at this point in the history
  • Loading branch information
sebmarkbage committed Mar 19, 2010
1 parent 2110141 commit e0d392f
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 60 deletions.
72 changes: 35 additions & 37 deletions Source/ART.Path.js
Expand Up @@ -40,41 +40,39 @@ var parse = function(path){

};

var circle = Math.PI * 2, north = circle / 2, west = north / 2, east = -west, south = 0;
var circle = Math.PI * 2, north = circle / 2, west = north / 2, east = -west, south = 0;

var calculateArc = function(rx, ry, rotation, large, clockwise, x, y, tX, tY){
var xp = -x / 2, yp = -y / 2,
rxry = rx * rx * ry * ry, ryxp = ry * ry * xp * xp, rxyp = rx * rx * yp * yp,
a = rxry - rxyp - ryxp;

if (a < 0){
a = Math.sqrt(1 - a / rxry);
rx *= a; ry *= a;
a = 0;
} else {
a = Math.sqrt(a / (rxyp + ryxp));
if (large == clockwise) a = -a;
}

var cx = a * rx * yp / ry - xp, cy = -a * ry * xp / rx - yp,
sa = Math.atan2(cx, -cy), ea = Math.atan2(-x + cx, y - cy);

if (!+clockwise){ var t = sa; sa = ea; ea = t; }
if (ea < sa){ ea += circle; }

cx += tX; cy += tY;

return {
circle: [cx - rx, cy - ry, cx + rx, cy + ry],
boundsX: [
ea > circle + west || (sa < west && ea > west) ? cx - rx : tX,
ea > circle + east || (sa < east && ea > east) ? cx + rx : tX
],
boundsY: [
ea > north ? cy - ry : tY,
ea > circle + south || (sa < south && ea > south) ? cy + ry : tY
]
};
var cx = x / 2, cy = y / 2,
rxry = rx * rx * ry * ry, ryxp = ry * ry * cx * cx, rxyp = rx * rx * cy * cy,
a = rxry - rxyp - ryxp;

if (a < 0){
a = Math.sqrt(1 - a / rxry);
rx *= a; ry *= a;
} else {
a = Math.sqrt(a / (rxyp + ryxp));
if (large == clockwise) a = -a;
cx += -a * cy * rx / ry; cy += a * x / 2 / rx * ry;
}

var sa = Math.atan2(cx, -cy), ea = Math.atan2(-x + cx, y - cy);
if (!+clockwise){ var t = sa; sa = ea; ea = t; }
if (ea < sa) ea += circle;

cx += tX; cy += tY;

return {
circle: [cx - rx, cy - ry, cx + rx, cy + ry],
boundsX: [
ea > circle + west || (sa < west && ea > west) ? cx - rx : tX,
ea > circle + east || (sa < east && ea > east) ? cx + rx : tX
],
boundsY: [
ea > north ? cy - ry : tY,
ea > circle + south || (sa < south && ea > south) ? cy + ry : tY
]
};
};

var measureAndTransform = function(parts, precision){
Expand Down Expand Up @@ -144,10 +142,10 @@ var measureAndTransform = function(parts, precision){
}

v.push(X, Y);
r = calculateArc.apply(null, v);

boundsX.push.apply(boundsX, r.boundsX);
boundsY.push.apply(boundsY, r.boundsY);
r = calculateArc.apply(null, v);

boundsX.push.apply(boundsX, r.boundsX);
boundsY.push.apply(boundsY, r.boundsY);

path += (v[4] == 1 ? 'wa' : 'at') + r.circle.map(np) + ',' + ux(X) + ',' + uy(Y) + ',' + ux(X = px) + ',' + uy(Y = py);
break;
Expand Down
46 changes: 23 additions & 23 deletions Source/ART.Shapes.js
Expand Up @@ -104,33 +104,33 @@ ART.Wedge = new Class({

draw: function(innerRadius, outerRadius, startAngle, endAngle){
var path = new ART.Path;

var circle = Math.PI * 2,
radiansPerDegree = Math.PI / 180,
sa = startAngle * radiansPerDegree % circle || 0,
ea = endAngle * radiansPerDegree % circle || 0,
ir = Math.min(innerRadius || 0, outerRadius || 0),
or = Math.max(innerRadius || 0, outerRadius || 0),
a = sa > ea ? circle - sa + ea : ea - sa;

if (a >= circle){

var circle = Math.PI * 2,
radiansPerDegree = Math.PI / 180,
sa = startAngle * radiansPerDegree % circle || 0,
ea = endAngle * radiansPerDegree % circle || 0,
ir = Math.min(innerRadius || 0, outerRadius || 0),
or = Math.max(innerRadius || 0, outerRadius || 0),
a = sa > ea ? circle - sa + ea : ea - sa;

if (a >= circle){

path.move(0, or).arc(or * 2, 0, or).arc(-or * 2, 0, or);
if (ir) path.move(or - ir, 0).counterArc(ir * 2, 0, ir).counterArc(-ir * 2, 0, ir);

} else {

var ss = Math.sin(sa), es = Math.sin(ea),
sc = Math.cos(sa), ec = Math.cos(ea),
ds = es - ss, dc = ec - sc, dr = ir - or,
large = a > Math.PI;

path.move(or + or * ss, or - or * sc).arc(or * ds, or * -dc, or, or, large).line(dr * es, dr * -ec);
if (ir) path.counterArc(ir * -ds, ir * dc, ir, ir, large);
if (ir) path.move(or - ir, 0).counterArc(ir * 2, 0, ir).counterArc(-ir * 2, 0, ir);

} else {

var ss = Math.sin(sa), es = Math.sin(ea),
sc = Math.cos(sa), ec = Math.cos(ea),
ds = es - ss, dc = ec - sc, dr = ir - or,
large = a > Math.PI;

path.move(or + or * ss, or - or * sc).arc(or * ds, or * -dc, or, or, large).line(dr * es, dr * -ec);
if (ir) path.counterArc(ir * -ds, ir * dc, ir, ir, large);

}

path.close();

path.close();
return this.parent(path);
}

Expand Down

0 comments on commit e0d392f

Please sign in to comment.