Add helper functions to serialize and deserialize paths

This commit is contained in:
Shaunak Kishore
2015-09-27 15:58:34 -04:00
parent 4fd741f204
commit 43c5c03626
5 changed files with 184 additions and 57 deletions

View File

@ -5,49 +5,6 @@ var MIN_CORNER_ANGLE = 0.1*Math.PI;
var MIN_CORNER_TANGENT_DISTANCE = 4;
var REVERSAL_PENALTY = 0.5;
// Helper methods for use with angles, which are floats in [-pi, pi).
var Angle = {
subtract: function(angle1, angle2) {
var result = angle1 - angle2;
if (result < -Math.PI) {
result += 2*Math.PI;
}
if (result >= Math.PI) {
result -= 2*Math.PI;
}
return result;
},
penalty: function(diff) {
return diff*diff;
},
};
// Helper methods for use with "points", which are pairs of integers.
var Point = {
angle: function(point) {
return Math.atan2(point[1], point[0]);
},
clone: function(point) {
return [point[0], point[1]];
},
distance2: function(point1, point2) {
var diff = Point.subtract(point1, point2);
return Math.pow(diff[0], 2) + Math.pow(diff[1], 2);
},
equal: function(point1, point2) {
return point1[0] === point2[0] && point1[1] === point2[1];
},
key: function(point) {
return point.join(',');
},
subtract: function(point1, point2) {
return [point1[0] - point2[0], point1[1] - point2[1]];
},
valid: function(point) {
return point[0] !== undefined && point[1] !== undefined;
},
};
// Takes a non-empty list of SVG commands that may contain multiple contours.
// Returns a list of lists of path segment objects that each form one contour.
// Each path segment has three keys: start, end, and control.