|  |  | 
 |  |  | // Function: svgedit.math.transformPoint | 
 |  |  | // A (hopefully) quicker function to transform a point by a matrix | 
 |  |  | // (this function avoids any DOM calls and just does the math) | 
 |  |  | //  | 
 |  |  | // | 
 |  |  | // Parameters: | 
 |  |  | // x - Float representing the x coordinate | 
 |  |  | // y - Float representing the y coordinate | 
 |  |  | 
 |  |  |  | 
 |  |  |  | 
 |  |  | // Function: svgedit.math.isIdentity | 
 |  |  | // Helper function to check if the matrix performs no actual transform  | 
 |  |  | // Helper function to check if the matrix performs no actual transform | 
 |  |  | // (i.e. exists for identity purposes) | 
 |  |  | // | 
 |  |  | // Parameters:  | 
 |  |  | // Parameters: | 
 |  |  | // m - The matrix object to check | 
 |  |  | // | 
 |  |  | // Returns: | 
 |  |  | 
 |  |  | // Function: svgedit.math.matrixMultiply | 
 |  |  | // This function tries to return a SVGMatrix that is the multiplication m1*m2. | 
 |  |  | // We also round to zero when it's near zero | 
 |  |  | //  | 
 |  |  | // | 
 |  |  | // Parameters: | 
 |  |  | // >= 2 Matrix objects to multiply | 
 |  |  | // | 
 |  |  | // Returns:  | 
 |  |  | // Returns: | 
 |  |  | // The matrix object resulting from the calculation | 
 |  |  | svgedit.math.matrixMultiply = function() { | 
 |  |  |   var args = arguments, i = args.length, m = args[i-1]; | 
 |  |  |    | 
 |  |  |  | 
 |  |  |   while(i-- > 1) { | 
 |  |  |     var m1 = args[i-1]; | 
 |  |  |     m = m1.multiply(m); | 
 |  |  | 
 |  |  |   if (Math.abs(m.d) < NEAR_ZERO) m.d = 0; | 
 |  |  |   if (Math.abs(m.e) < NEAR_ZERO) m.e = 0; | 
 |  |  |   if (Math.abs(m.f) < NEAR_ZERO) m.f = 0; | 
 |  |  |    | 
 |  |  |  | 
 |  |  |   return m; | 
 |  |  | }; | 
 |  |  |  | 
 |  |  | // Function: svgedit.math.hasMatrixTransform | 
 |  |  | // See if the given transformlist includes a non-indentity matrix transform | 
 |  |  | // | 
 |  |  | // Parameters:  | 
 |  |  | // Parameters: | 
 |  |  | // tlist - The transformlist to check | 
 |  |  | // | 
 |  |  | // Returns:  | 
 |  |  | // Returns: | 
 |  |  | // Boolean on whether or not a matrix transform was found | 
 |  |  | svgedit.math.hasMatrixTransform = function(tlist) { | 
 |  |  |   if(!tlist) return false; | 
 |  |  | 
 |  |  | // w - Float with the box width | 
 |  |  | // h - Float with the box height | 
 |  |  | // m - Matrix object to transform the box by | 
 |  |  | //  | 
 |  |  | // | 
 |  |  | // Returns: | 
 |  |  | // An object with the following values: | 
 |  |  | // * tl - The top left coordinate (x,y object) | 
 |  |  | 
 |  |  |   miny = Math.min(miny, botright.y); | 
 |  |  |   maxy = Math.max(maxy, botright.y); | 
 |  |  |  | 
 |  |  |   return {tl:topleft, tr:topright, bl:botleft, br:botright,  | 
 |  |  |   return {tl:topleft, tr:topright, bl:botleft, br:botright, | 
 |  |  |       aabox: {x:minx, y:miny, width:(maxx-minx), height:(maxy-miny)} }; | 
 |  |  | }; | 
 |  |  |  | 
 |  |  | 
 |  |  | // (this is the equivalent of SVGTransformList.consolidate() but unlike | 
 |  |  | //  that method, this one does not modify the actual SVGTransformList) | 
 |  |  | // This function is very liberal with its min,max arguments | 
 |  |  | //  | 
 |  |  | // | 
 |  |  | // Parameters: | 
 |  |  | // tlist - The transformlist object | 
 |  |  | // min - Optional integer indicating start transform position | 
 |  |  | 
 |  |  |   var m = svg.createSVGMatrix(); | 
 |  |  |   for (var i = min; i <= max; ++i) { | 
 |  |  |     // if our indices are out of range, just use a harmless identity matrix | 
 |  |  |     var mtom = (i >= 0 && i < tlist.numberOfItems ?  | 
 |  |  |     var mtom = (i >= 0 && i < tlist.numberOfItems ? | 
 |  |  |             tlist.getItem(i).matrix : | 
 |  |  |             svg.createSVGMatrix()); | 
 |  |  |     m = svgedit.math.matrixMultiply(m, mtom); | 
 |  |  | 
 |  |  | // | 
 |  |  | // Parameters: | 
 |  |  | // elem - The DOM element to check | 
 |  |  | //  | 
 |  |  | // | 
 |  |  | // Returns: | 
 |  |  | // The matrix object associated with the element's transformlist | 
 |  |  | svgedit.math.getMatrix = function(elem) { | 
 |  |  | 
 |  |  |  | 
 |  |  |  | 
 |  |  | // Function: svgedit.math.snapToAngle | 
 |  |  | // Returns a 45 degree angle coordinate associated with the two given  | 
 |  |  | // Returns a 45 degree angle coordinate associated with the two given | 
 |  |  | // coordinates | 
 |  |  | //  | 
 |  |  | // | 
 |  |  | // Parameters: | 
 |  |  | // x1 - First coordinate's x value | 
 |  |  | // x2 - Second coordinate's x value | 
 |  |  | // y1 - First coordinate's y value | 
 |  |  | // y2 - Second coordinate's y value | 
 |  |  | // | 
 |  |  | // Returns:  | 
 |  |  | // Returns: | 
 |  |  | // Object with the following values: | 
 |  |  | // x - The angle-snapped x value | 
 |  |  | // y - The angle-snapped y value | 
 |  |  | 
 |  |  |   var angle = Math.atan2(dy,dx); | 
 |  |  |   var dist = Math.sqrt(dx * dx + dy * dy); | 
 |  |  |   var snapangle= Math.round(angle/snap)*snap; | 
 |  |  |   var x = x1 + dist*Math.cos(snapangle);   | 
 |  |  |   var x = x1 + dist*Math.cos(snapangle); | 
 |  |  |   var y = y1 + dist*Math.sin(snapangle); | 
 |  |  |   //console.log(x1,y1,x2,y2,x,y,angle) | 
 |  |  |   return {x:x, y:y, a:snapangle}; | 
 |  |  | 
 |  |  | // Boolean that's true if rectangles intersect | 
 |  |  | svgedit.math.rectsIntersect = function(r1, r2) { | 
 |  |  |   if (!r1 || !r2) return false; | 
 |  |  |   return r2.x < (r1.x+r1.width) &&  | 
 |  |  |   return r2.x < (r1.x+r1.width) && | 
 |  |  |     (r2.x+r2.width) > r1.x && | 
 |  |  |     r2.y < (r1.y+r1.height) && | 
 |  |  |     (r2.y+r2.height) > r1.y; | 
 |  |  | }; | 
 |  |  |  | 
 |  |  |  | 
 |  |  | })(); | 
 |  |  | })(); |