Skip to content

Commit

Permalink
Minor code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitaly Puzrin authored and kpym committed Dec 21, 2015
1 parent 8140cc4 commit 14efddb
Showing 1 changed file with 17 additions and 28 deletions.
45 changes: 17 additions & 28 deletions lib/a2c.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
// Convert an arc to a sequence of cubic bézier curves
//

'use strict';


var TAU = Math.PI * 2;

// causes an error in case of a long url in comments
/* eslint-disable max-len */

// spaces are used for grouping throughout this file
/* eslint-disable space-infix-ops */

// Calculate an angle between two vectors
Expand All @@ -20,30 +17,13 @@ function vector_angle(ux, uy, vx, vy) {
var dot = ux * vx + uy * vy;
var div = dot / (umag * vmag);

if (div > 1 || div < -1) {
// rounding errors, e.g. -1.0000000000000002 can screw up this
div = Math.max(div, -1);
div = Math.min(div, 1);
}
// rounding errors, e.g. -1.0000000000000002 can screw up this
if (div > 1.0) { div = 1.0; }
if (div < -1.0) { div = -1.0; }

return sign * Math.acos(div);
}

// Correction of out-of-range radii
//
function correct_radii(midx, midy, rx, ry) {
rx = Math.abs(rx);
ry = Math.abs(ry);

var Λ = (midx * midx) / (rx * rx) + (midy * midy) / (ry * ry);
if (Λ > 1) {
rx *= Math.sqrt(Λ);
ry *= Math.sqrt(Λ);
}

return [ rx, ry ];
}


// Convert from endpoint to center parameterization,
// see http://www.w3.org/TR/SVG11/implnote.html#ArcImplementationNotes
Expand Down Expand Up @@ -115,7 +95,7 @@ function get_arc_center(x1, y1, x2, y2, fa, fs, rx, ry, sin_φ, cos_φ) {

//
// Approximate one unit arc segment with bézier curves,
// see http://math.stackexchange.com/questions/873224/calculate-control-points-of-cubic-bezier-curve-approximating-a-part-of-a-circle
// see http://math.stackexchange.com/questions/873224
//
function approximate_unit_arc(θ1, Δθ) {
var α = 4/3 * Math.tan(Δθ/4);
Expand Down Expand Up @@ -147,9 +127,18 @@ module.exports = function a2c(x1, y1, x2, y2, fa, fs, rx, ry, φ) {
return [];
}

var radii = correct_radii(x1p, y1p, rx, ry);
rx = radii[0];
ry = radii[1];

// Compensate out-of-range radii
//
rx = Math.abs(rx);
ry = Math.abs(ry);

var Λ = (x1p * x1p) / (rx * rx) + (y1p * y1p) / (ry * ry);
if (Λ > 1) {
rx *= Math.sqrt(Λ);
ry *= Math.sqrt(Λ);
}


// Get center parameters (cx, cy, θ1, Δθ)
//
Expand Down

0 comments on commit 14efddb

Please sign in to comment.