📄 helper_functions.js
字号:
/** * @fileoverview * * ECMAScript <a href="http://www.carto.net/papers/svg/resources/helper_functions.html">helper functions</a>, main purpose is to serve in SVG mapping or other SVG based web applications * * This ECMA script library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library (http://www.carto.net/papers/svg/resources/lesser_gpl.txt); if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * Please report bugs and send improvements to neumann@karto.baug.ethz.ch * If you use these scripts, please link to the original (http://www.carto.net/papers/svg/resources/helper_functions.html) * somewhere in the source-code-comment or the "about" of your project and give credits, thanks! * * See <a href="js_docs_out/overview-summary-helper_functions.js.html">documentation</a>. * * @author Andreas Neumann a.neumann@carto.net * @copyright LGPL 2.1 <a href="http://www.gnu.org/copyleft/lesser.txt">Gnu LGPL 2.1</a> * @credits Bruce Rindahl, numerous people on svgdevelopers@yahoogroups.com *///global variables necessary to create elements in these namespaces, do not delete them!!!!/** * This variable is a shortcut to the full URL of the SVG namespace * @final * @type String */var svgNS = "http://www.w3.org/2000/svg";/** * This variable is a shortcut to the full URL of the XLink namespace * @final * @type String */var xlinkNS = "http://www.w3.org/1999/xlink";/** * This variable is a shortcut to the full URL of the attrib namespace * @final * @type String */var cartoNS = "http://www.carto.net/attrib";/** * This variable is a alias to the full URL of the attrib namespace * @final * @type String */var attribNS = "http://www.carto.net/attrib";/** * This variable is a alias to the full URL of the Batik extension namespace * @final * @type String */var batikNS = "http://xml.apache.org/batik/ext";/** * Returns the polar direction from a given vector * @param {Number} xdiff the x-part of the vector * @param {Number} ydiff the y-part of the vector * @return direction the direction in radians * @type Number * @version 1.0 (2007-04-30) * @see #toPolarDist * @see #toRectX * @see #toRectY */function toPolarDir(xdiff,ydiff) { var direction = (Math.atan2(ydiff,xdiff)); return(direction);}/** * Returns the polar distance from a given vector * @param {Number} xdiff the x-part of the vector * @param {Number} ydiff the y-part of the vector * @return distance the distance * @type Number * @version 1.0 (2007-04-30) * @see #toPolarDir * @see #toRectX * @see #toRectY */function toPolarDist(xdiff,ydiff) { var distance = Math.sqrt(xdiff * xdiff + ydiff * ydiff); return(distance);}/** * Returns the x-part of a vector from a given direction and distance * @param {Number} direction the direction (in radians) * @param {Number} distance the distance * @return x the x-part of the vector * @type Number * @version 1.0 (2007-04-30) * @see #toPolarDist * @see #toPolarDir * @see #toRectY */function toRectX(direction,distance) { var x = distance * Math.cos(direction); return(x);}/** * Returns the y-part of the vector from a given direction and distance * @param {Number} direction the direction (in radians) * @param {Number} distance the distance * @return y the y-part of the vector * @type Number * @version 1.0 (2007-04-30) * @see #toPolarDist * @see #toPolarDir * @see #toRectX */function toRectY(direction,distance) { y = distance * Math.sin(direction); return(y);}/** * Converts degrees to radians * @param {Number} deg the degree value * @return rad the radians value * @type Number * @version 1.0 (2007-04-30) * @see #RadToDeg */function DegToRad(deg) { return (deg / 180.0 * Math.PI);}/** * Converts radians to degrees * @param {Number} rad the radians value * @return deg the degree value * @type Number * @version 1.0 (2007-04-30) * @see #DegToRad */function RadToDeg(rad) { return (rad / Math.PI * 180.0);}/** * Converts decimal degrees to degrees, minutes, seconds * @param {Number} dd the decimal degree value * @return degrees the degree values in the following notation: {deg:degrees,min:minutes,sec:seconds} * @type literal * @version 1.0 (2007-04-30) * @see #dms2dd */function dd2dms(dd) { var minutes = (Math.abs(dd) - Math.floor(Math.abs(dd))) * 60; var seconds = (minutes - Math.floor(minutes)) * 60; var minutes = Math.floor(minutes); if (dd >= 0) { var degrees = Math.floor(dd); } else { var degrees = Math.ceil(dd); } return {deg:degrees,min:minutes,sec:seconds};}/** * Converts degrees, minutes and seconds to decimal degrees * @param {Number} deg the degree value * @param {Number} min the minute value * @param {Number} sec the second value * @return deg the decimal degree values * @type Number * @version 1.0 (2007-04-30) * @see #dd2dms */function dms2dd(deg,min,sec) { if (deg < 0) { return deg - (min / 60) - (sec / 3600); } else { return deg + (min / 60) + (sec / 3600); }}/** * log function, missing in the standard Math object * @param {Number} x the value where the log function should be applied to * @param {Number} b the base value for the log function * @return logResult the result of the log function * @type Number * @version 1.0 (2007-04-30) */function log(x,b) { if(b==null) b=Math.E; return Math.log(x)/Math.log(b);}/** * interpolates a value (e.g. elevation) bilinearly based on the position within a cell with 4 corner values * @param {Number} za the value at the upper left corner of the cell * @param {Number} zb the value at the upper right corner of the cell * @param {Number} zc the value at the lower right corner of the cell * @param {Number} zd the value at the lower left corner of the cell * @param {Number} xpos the x position of the point where a new value should be interpolated * @param {Number} ypos the y position of the point where a new value should be interpolated * @param {Number} ax the x position of the lower left corner of the cell * @param {Number} ay the y position of the lower left corner of the cell * @param {Number} cellsize the size of the cell * @return interpol_value the result of the bilinear interpolation function * @type Number * @version 1.0 (2007-04-30) */function intBilinear(za,zb,zc,zd,xpos,ypos,ax,ay,cellsize) { //bilinear interpolation function var e = (xpos - ax) / cellsize; var f = (ypos - ay) / cellsize; //calculation of weights var wa = (1 - e) * (1 - f); var wb = e * (1 - f); var wc = e * f; var wd = f * (1 - e); var interpol_value = wa * zc + wb * zd + wc * za + wd * zb; return interpol_value; }/** * tests if a given point is left or right of a given line * @param {Number} pointx the x position of the given point * @param {Number} pointy the y position of the given point * @param {Number} linex1 the x position of line's start point * @param {Number} liney1 the y position of line's start point * @param {Number} linex2 the x position of line's end point * @param {Number} liney2 the y position of line's end point * @return leftof the result of the leftOfTest, 1 means leftOf, 0 means rightOf * @type Number (integer, 0|1) * @version 1.0 (2007-04-30) */function leftOfTest(pointx,pointy,linex1,liney1,linex2,liney2) { var result = (liney1 - pointy) * (linex2 - linex1) - (linex1 - pointx) * (liney2 - liney1); if (result < 0) { var leftof = 1; //case left of } else { var leftof = 0; //case left of } return leftof;}/** * calculates the distance between a given point and a given line * @param {Number} pointx the x position of the given point * @param {Number} pointy the y position of the given point * @param {Number} linex1 the x position of line's start point * @param {Number} liney1 the y position of line's start point * @param {Number} linex2 the x position of line's end point * @param {Number} liney2 the y position of line's end point * @return distance the result of the leftOfTest, 1 means leftOf, 0 means rightOf * @type Number * @version 1.0 (2007-04-30) */function distFromLine(xpoint,ypoint,linex1,liney1,linex2,liney2) { var dx = linex2 - linex1; var dy = liney2 - liney1; var distance = (dy * (xpoint - linex1) - dx * (ypoint - liney1)) / Math.sqrt(Math.pow(dx,2) + Math.pow(dy,2)); return distance;}/** * calculates the angle between two vectors (lines) * @param {Number} ax the x part of vector a * @param {Number} ay the y part of vector a * @param {Number} bx the x part of vector b * @param {Number} by the y part of vector b * @return angle the angle in radians * @type Number * @version 1.0 (2007-04-30) * @credits <a href="http://www.mathe-online.at/mathint/vect2/i.html#Winkel">Mathe Online (Winkel)</a> */function angleBetwTwoLines(ax,ay,bx,by) { var angle = Math.acos((ax * bx + ay * by) / (Math.sqrt(Math.pow(ax,2) + Math.pow(ay,2)) * Math.sqrt(Math.pow(bx,2) + Math.pow(by,2)))); return angle;}/** * calculates the bisector vector for two given vectors * @param {Number} ax the x part of vector a * @param {Number} ay the y part of vector a * @param {Number} bx the x part of vector b * @param {Number} by the y part of vector b * @return c the resulting vector as an Array, c[0] is the x part of the vector, c[1] is the y part * @type Array * @version 1.0 (2007-04-30) * @credits <a href="http://www.mathe-online.at/mathint/vect1/i.html#Winkelsymmetrale">Mathe Online (Winkelsymmetrale)</a> * see #calcBisectorAngle * */function calcBisectorVector(ax,ay,bx,by) { var betraga = Math.sqrt(Math.pow(ax,2) + Math.pow(ay,2)); var betragb = Math.sqrt(Math.pow(bx,2) + Math.pow(by,2)); var c = new Array(); c[0] = ax / betraga + bx / betragb; c[1] = ay / betraga + by / betragb; return c;}/** * calculates the bisector angle for two given vectors * @param {Number} ax the x part of vector a * @param {Number} ay the y part of vector a * @param {Number} bx the x part of vector b * @param {Number} by the y part of vector b * @return angle the bisector angle in radians * @type Number * @version 1.0 (2007-04-30) * @credits <a href="http://www.mathe-online.at/mathint/vect1/i.html#Winkelsymmetrale">Mathe Online (Winkelsymmetrale)</a> * see #calcBisectorVector * */function calcBisectorAngle(ax,ay,bx,by) { var betraga = Math.sqrt(Math.pow(ax,2) + Math.pow(ay,2)); var betragb = Math.sqrt(Math.pow(bx,2) + Math.pow(by,2)); var c1 = ax / betraga + bx / betragb; var c2 = ay / betraga + by / betragb; var angle = toPolarDir(c1,c2); return angle;}/** * calculates the intersection point of two given lines * @param {Number} line1x1 the x the start point of line 1 * @param {Number} line1y1 the y the start point of line 1 * @param {Number} line1x2 the x the end point of line 1 * @param {Number} line1y2 the y the end point of line 1 * @return interSectPoint the intersection point, interSectPoint.x contains x-part, interSectPoint.y the y-part of the resulting coordinate * @type Object * @version 1.0 (2007-04-30) * @credits <a href="http://astronomy.swin.edu.au/~pbourke/geometry/lineline2d/">P. Bourke</a> */function intersect2lines(line1x1,line1y1,line1x2,line1y2,line2x1,line2y1,line2x2,line2y2) { var interSectPoint = new Object(); var denominator = (line2y2 - line2y1)*(line1x2 - line1x1) - (line2x2 - line2x1)*(line1y2 - line1y1); if (denominator == 0) { alert("lines are parallel"); } else { var ua = ((line2x2 - line2x1)*(line1y1 - line2y1) - (line2y2 - line2y1)*(line1x1 - line2x1)) / denominator; var ub = ((line1x2 - line1x1)*(line1y1 - line2y1) - (line1y2 - line1y1)*(line1x1 - line2x1)) / denominator; } interSectPoint["x"] = line1x1 + ua * (line1x2 - line1x1); interSectPoint["y"] = line1y1 + ua * (line1y2 - line1y1); return interSectPoint;}/** * reformats a given number to a string by adding separators at every third digit * @param {String|Number} inputNumber the input number, can be of type number or string * @param {String} separator the separator, e.g. ' or , * @return newString the intersection point, interSectPoint.x contains x-part, interSectPoint.y the y-part of the resulting coordinate * @type String * @version 1.0 (2007-04-30) */function formatNumberString(inputNumber,separator) { //check if of type string, if number, convert it to string if (typeof(inputNumber) == "Number") { var myTempString = inputNumber.toString(); } else { var myTempString = inputNumber; } var newString=""; //if it contains a comma, it will be split var splitResults = myTempString.split("."); var myCounter = splitResults[0].length; if (myCounter > 3) { while(myCounter > 0) { if (myCounter > 3) { newString = separator + splitResults[0].substr(myCounter - 3,3) + newString; } else { newString = splitResults[0].substr(0,myCounter) + newString; } myCounter -= 3; } } else { newString = splitResults[0]; } //concatenate if it contains a comma if (splitResults[1]) { newString = newString + "." + splitResults[1]; } return newString;}/** * writes a status text message out to a SVG text element's first child * @param {String} statusText the text message to be displayed * @version 1.0 (2007-04-30) */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -