topical media & game development
lib-present-math-s5-files-ASCIIsvg.js / js
/* ASCIIsvg.js
==============
JavaScript routines to dynamically generate Scalable Vector Graphics
using a mathematical xy-coordinate system (y increases upwards) and
very intuitive JavaScript commands (no programming experience required).
ASCIIsvg.js is good for learning math and illustrating online math texts.
Works with Internet Explorer+Adobe SVGviewer and SVG enabled Mozilla/Firefox.
Ver 1.2.6 Aug 1, 2005 (c) Peter Jipsen http://www.chapman.edu/~jipsen
Latest version at http://www.chapman.edu/~jipsen/svg/ASCIIsvg.js
If you use it on a webpage, please send the URL to jipsen@chapman.edu
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at
your option) any later version.
This program 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
General Public License (at http://www.gnu.org/copyleft/gpl.html)
for more details.*/
var checkIfSVGavailable = true;
var notifyIfNoSVG = true;
var xunitlength = 20; // pixels
var yunitlength = 20; // pixels
var origin = [0,0]; // in pixels (default is bottom left corner)
var border = 25;
var strokewidth = "1"; // pixel
var strokedasharray; // e.g. 4,4 pixel
var stroke = "black"; // default line color
var fill = "none"; // default fill color
var fontstyle = "italic"; // default shape for text labels
var fontfamily = "times"; // default font
var fontsize = "20"; // default size
var fontweight = "normal";// default size
var fontstroke = "none"; // default font outline color
var fontfill = "none"; // default font color
var markerstrokewidth = "1";
var markerstroke = "black";
var markerfill = "yellow";
var marker = "none";
var arrowfill = stroke;
var dotradius = 4;
var ticklength = 4;
var axesstroke = "black";
var gridstroke = "grey";
var pointerpos = null;
var coordinates = null;
var above = "above";
var below = "below";
var left = "left";
var right = "right";
var aboveleft = "aboveleft";
var aboveright = "aboveright";
var belowleft = "belowleft";
var belowright = "belowright";
var cpi = "\u03C0", ctheta = "\u03B8";
var pi = Math.PI, ln = Math.log, e = Math.E;
var arcsin = Math.asin, arccos = Math.acos, arctan = Math.atan;
var sec = function(x) { return 1/Math.cos(x) };
var csc = function(x) { return 1/Math.sin(x) };
var cot = function(x) { return 1/Math.tan(x) };
var xMin=-5;
var xMax=5;
var yMin=-5;
var yMax=5;
var isIE = document.createElementNS==null;
var picture, svgpicture, doc, width, height, a, b, c, d, i, n, p, t, x, y;
var arcsec = function(x) { return arccos(1/x) };
var arccsc = function(x) { return arcsin(1/x) };
var arccot = function(x) { return arctan(1/x) };
var sinh = function(x) { return (Math.exp(x)-Math.exp(-x))/2 };
var cosh = function(x) { return (Math.exp(x)+Math.exp(-x))/2 };
var tanh =
function(x) { return (Math.exp(x)-Math.exp(-x))/(Math.exp(x)+Math.exp(-x)) };
var sech = function(x) { return 1/cosh(x) };
var csch = function(x) { return 1/sinh(x) };
var coth = function(x) { return 1/tanh(x) };
var arcsinh = function(x) { return ln(x+Math.sqrt(x*x+1)) };
var arccosh = function(x) { return ln(x+Math.sqrt(x*x-1)) };
var arctanh = function(x) { return ln((1+x)/(1-x))/2 };
var sech = function(x) { return 1/cosh(x) };
var csch = function(x) { return 1/sinh(x) };
var coth = function(x) { return 1/tanh(x) };
var arcsech = function(x) { return arccosh(1/x) };
var arccsch = function(x) { return arcsinh(1/x) };
var arccoth = function(x) { return arctanh(1/x) };
var sign = function(x) { return (x==0?0:(x<0?-1:1)) };
function factorial(x,n) {
if (n==null) n=1;
for (var i=x-n; i>0; i-=n) x*=i;
return (x<0?NaN:(x==0?1:x));
}
function C(x,k) {
var res=1;
for (var i=0; i<k; i++) res*=(x-i)/(k-i);
return res;
}
function chop(x,n) {
if (n==null) n=0;
return Math.floor(x*Math.pow(10,n))/Math.pow(10,n);
}
function ran(a,b,n) {
if (n==null) n=0;
return chop((b+Math.pow(10,-n)-a)*Math.random()+a,n);
}
function myCreateElementXHTML(t) {
if (isIE) return document.createElement(t);
else return document.createElementNS("http://www.w3.org/1999/xhtml",t);
}
function isSVGavailable() {
var nd = myCreateElementXHTML("center");
nd.appendChild(document.createTextNode("To view the "));
var an = myCreateElementXHTML("a");
an.appendChild(document.createTextNode("ASCIIsvg"));
an.setAttribute("href","http://www.chapman.edu/~jipsen/asciisvg.html");
nd.appendChild(an);
nd.appendChild(document.createTextNode(" images use Internet Explorer 6+"));
an = myCreateElementXHTML("a");
an.appendChild(document.createTextNode("Adobe SVGviewer 3.02"));
an.setAttribute("href","http://www.adobe.com/svg");
nd.appendChild(an);
nd.appendChild(document.createTextNode(" or "));
an = myCreateElementXHTML("a");
an.appendChild(document.createTextNode("SVG enabled Mozilla/Firefox"));
an.setAttribute("href",
"http://www.chapman.edu/~jipsen/svg/svgenabledmozillafirefox.html");
nd.appendChild(an);
if (navigator.appName.slice(0,8)=="Netscape")
if (window['SVGElement']) return null;
else return nd;
else if (navigator.appName.slice(0,9)=="Microsoft")
try {
var oSVG=eval("new ActiveXObject('Adobe.SVGCtl.3');");
return null;
} catch (e) {
return nd;
}
else return nd;
}
function less(x,y) { return x < y } // used for scripts in XML files
// since IE does not handle CDATA well
function setText(st,id) {
var node = document.getElementById(id);
if (node!=null)
if (node.childNodes.length!=0) node.childNodes[0].nodeValue = st;
else node.appendChild(document.createTextNode(st));
}
function myCreateElementSVG(t) {
if (isIE) return doc.createElement(t);
else return doc.createElementNS("http://www.w3.org/2000/svg",t);
}
function getX() {
return (doc.getElementById("pointerpos").getAttribute("cx")-origin[0])/xunitlength;
}
function getY() {
return (height-origin[1]-doc.getElementById("pointerpos").getAttribute("cy"))/yunitlength;
}
function mousemove_listener(evt) {
if (svgpicture.getAttribute("xbase")!=null)
pointerpos.cx.baseVal.value = evt.clientX-svgpicture.getAttribute("xbase");
if (svgpicture.getAttribute("ybase")!=null)
pointerpos.cy.baseVal.value = evt.clientY-svgpicture.getAttribute("ybase");
}
function top_listener(evt) {
svgpicture.setAttribute("ybase",evt.clientY);
}
function bottom_listener(evt) {
svgpicture.setAttribute("ybase",evt.clientY-height+1);
}
function left_listener(evt) {
svgpicture.setAttribute("xbase",evt.clientX);
}
function right_listener(evt) {
svgpicture.setAttribute("xbase",evt.clientX-width+1);
}
function switchTo(id) {
picture = document.getElementById(id);
width = picture.getAttribute("width")-0;
height = picture.getAttribute("height")-0;
strokewidth = "1" // pixel
stroke = "black"; // default line color
fill = "none"; // default fill color
marker = "none";
if ((picture.nodeName == "EMBED" || picture.nodeName == "embed") && isIE) {
svgpicture = picture.getSVGDocument().getElementById("root");
doc = picture.getSVGDocument();
} else {
svgpicture = picture;
doc = document;
}
xunitlength = svgpicture.getAttribute("xunitlength")-0;
yunitlength = svgpicture.getAttribute("yunitlength")-0;
origin = [svgpicture.getAttribute("ox")-0,svgpicture.getAttribute("oy")-0];
}
function drawPictures() {
var src, id, dsvg, onmov, nd;
var pictures = document.getElementsByTagName("embed");
var len = pictures.length;
if (checkIfSVGavailable) {
nd = isSVGavailable();
if (nd != null && notifyIfNoSVG && len>0) {
var ASbody = document.getElementsByTagName("body")[0];
ASbody.insertBefore(nd,ASbody.childNodes[0]);
}
}
if (nd == null) {
for (var index = 0; index < len; index++) {
picture = (isIE ? pictures[index] : pictures[0]);
src = picture.getAttribute("script");
if (src==null) src = "";
if (src=="") {
if (document.getElementById("picture"+(index+1)+"input")==null) {
node = picture.previousSibling;
while (node != null && node.nodeName != "TEXTAREA" &&
node.nodeName != "textarea") node = node.previousSibling;
if (node.value.indexOf("hideCode()")!=-1) {
node.style.display = "none";
node.nextSibling.nextSibling.style.display = "none";
}
node.setAttribute("id","picture"+(index+1)+"input");
}
src = document.getElementById("picture"+(index+1)+"input").value;
}
src = src.replace(/plot\(\x20*([^\"\[].*?) /g,"plot\(\"$1\",");
src = src.replace(/plot\(\x20*([^\"\[].*)\)/g,"plot(\"$1\")");
src = src.replace(/([0-9])([a-zA-Z])/g,"$1*$2");
src = src.replace(/\)([\(0-9a-zA-Z])/g,"\)*$1");
// eval(src.replace(/\s\s/g,";")); //for XML version
id = picture.getAttribute("id");
dsvg = picture.getAttribute("src");
onmov = picture.getAttribute("onmousemove");
if (id == null || id == "") {
id = "picture"+(index+1);
picture.setAttribute("id",id);
}
try {
with (Math) eval(src);
} catch(err) {alert(err+"\n"+src)}
if (isIE) src = src.replace(/([^\r])\n/g,"$1\r");
setText("<embed width=\""+width+"\" height=\""+height+"\" src=\""+
dsvg+"\" "+(onmov!=null?"onmousemove=\""+onmov+"\"":"")+
(isIE?"\r":"\n")+"script=\'"+src+"\'>",id+"script");
// setText(src.replace(/\s\s/g,"\r"),id+"script"); //for XML version
}
}
}
function updatePicture(obj) {
var src = document.getElementById("picture"+(obj+1)+"input").value;
// picture = document.getElementById("picture"+(obj+1));//id);
switchTo("picture"+(obj+1));
src = src.replace(/plot\(\x20*([^\"\[].*?) /g,"plot\(\"$1\",");
src = src.replace(/plot\(\x20*([^\"\[].*)\)/g,"plot(\"$1\")");
src = src.replace(/([0-9])([a-zA-Z])/g,"$1*$2");
src = src.replace(/\)([\(0-9a-zA-Z])/g,"\)*$1");
with (Math) eval(src);
}
function showHideCode(obj) {
var node = obj.previousSibling;
while (node != null && node.nodeName != "BUTTON" &&
node.nodeName != "button") node = node.previousSibling;
if (node.style.display == "none") node.style.display = "";
else node.style.display = "none";
while (node != null && node.nodeName != "TEXTAREA" &&
node.nodeName != "textarea") node = node.previousSibling;
if (node.style.display == "none") node.style.display = "";
else node.style.display = "none";
}
function hideCode() { //do nothing
}
function setBorder(x) { border = x }
function initPicture(xmin,xmax,ymin,ymax) {
strokewidth = "1"; // pixel
strokedasharray = null;
stroke = "black"; // default line color
fill = "none"; // default fill color
fontstyle = "italic"; // default shape for text labels
fontfamily = "times"; // default font
fontsize = "20"; // default size
fontweight = "normal";
marker = "none";
xMin = xmin;
xMax = xmax;
if (typeof xmin != "number" || typeof xmax != "number" || xmin >= xmax)
alert("initPicture requires at least two numbers: xmin < xmax");
else if (ymax != null && (typeof ymin != "number" ||
typeof ymax != "number" || ymin >= ymax))
alert("initPicture(xmin,xmax,ymin,ymax) requires numbers ymin < ymax");
else {
if (ymin!=null) yMin = ymin;
if (ymax!=null) yMax = ymax;
if (width==null) width = picture.getAttribute("width");
else picture.setAttribute("width",width);
if (height==null) height = picture.getAttribute("height");
else picture.setAttribute("height",height);
if (xmax!=null) {
xunitlength = (width-2*border)/(xmax-xmin);
yunitlength = xunitlength;
if (ymin==null) {
origin = [-xmin*xunitlength+border,height/2];
yMin = -(height-2*border)/(2*yunitlength);
yMax = -yMin;
} else {
if (ymax!=null) yunitlength = (height-2*border)/(ymax-ymin);
else yMax = (height-2*border)/yunitlength + ymin;
origin = [-xmin*xunitlength+border,-ymin*yunitlength+border];
}
}
if (picture.nodeName == "EMBED" || picture.nodeName == "embed") {
if (isIE) {
svgpicture = picture.getSVGDocument().getElementById("root");
while (svgpicture.childNodes.length()>5)
svgpicture.removeChild(svgpicture.lastChild);
svgpicture.setAttribute("width",width);
svgpicture.setAttribute("height",height);
doc = picture.getSVGDocument();
} else {
var qnode = document.createElementNS("http://www.w3.org/2000/svg","svg");
qnode.setAttribute("id",picture.getAttribute("id"));
qnode.setAttribute("width",picture.getAttribute("width"));
qnode.setAttribute("height",picture.getAttribute("height"));
if (picture.parentNode!=null)
picture.parentNode.replaceChild(qnode,picture);
else
svgpicture.parentNode.replaceChild(qnode,svgpicture);
svgpicture = qnode;
doc = document;
pointerpos = doc.getElementById("pointerpos");
if (pointerpos==null) {
pointerpos = myCreateElementSVG("circle");
pointerpos.setAttribute("id","pointerpos");
pointerpos.setAttribute("cx",0);
pointerpos.setAttribute("cy",0);
pointerpos.setAttribute("r",0.5);
pointerpos.setAttribute("fill","red");
svgpicture.appendChild(pointerpos);
}
alert('Click OK');
}
} else {
svgpicture = picture;
doc = document;
}
svgpicture.setAttribute("xunitlength",xunitlength);
svgpicture.setAttribute("yunitlength",yunitlength);
svgpicture.setAttribute("ox",origin[0]);
svgpicture.setAttribute("oy",origin[1]);
var node = myCreateElementSVG("rect");
node.setAttribute("x","0");
node.setAttribute("y","0");
node.setAttribute("width",width);
node.setAttribute("height",height);
node.setAttribute("style","stroke-width:1;fill:white");
svgpicture.appendChild(node);
if (!isIE && picture.getAttribute("onmousemove")!=null) {
svgpicture.addEventListener("mousemove", mousemove_listener, true);
var st = picture.getAttribute("onmousemove");
svgpicture.addEventListener("mousemove", eval(st.slice(0,st.indexOf("("))), true);
node = myCreateElementSVG("polyline");
node.setAttribute("points","0,0 "+width+",0");
node.setAttribute("style","stroke:white; stroke-width:3");
node.addEventListener("mousemove", top_listener, true);
svgpicture.appendChild(node);
node = myCreateElementSVG("polyline");
node.setAttribute("points","0,"+height+" "+width+","+height);
node.setAttribute("style","stroke:white; stroke-width:3");
node.addEventListener("mousemove", bottom_listener, true);
svgpicture.appendChild(node);
node = myCreateElementSVG("polyline");
node.setAttribute("points","0,0 0,"+height);
node.setAttribute("style","stroke:white; stroke-width:3");
node.addEventListener("mousemove", left_listener, true);
svgpicture.appendChild(node);
node = myCreateElementSVG("polyline");
node.setAttribute("points",(width-1)+",0 "+(width-1)+","+height);
node.setAttribute("style","stroke:white; stroke-width:3");
node.addEventListener("mousemove", right_listener, true);
svgpicture.appendChild(node);
}
border = 25;
}
}
function line(p,q,id) { // segment connecting points p,q (coordinates in units)
var node;
if (id!=null) node = doc.getElementById(id);
if (node==null) {
node = myCreateElementSVG("path");
node.setAttribute("id", id);
svgpicture.appendChild(node);
}
node.setAttribute("d","M"+(p[0]*xunitlength+origin[0])+","+
(height-p[1]*yunitlength-origin[1])+" "+
(q[0]*xunitlength+origin[0])+","+(height-q[1]*yunitlength-origin[1]));
node.setAttribute("stroke-width", strokewidth);
if (strokedasharray!=null)
node.setAttribute("stroke-dasharray", strokedasharray);
node.setAttribute("stroke", stroke);
node.setAttribute("fill", fill);
if (marker=="dot" || marker=="arrowdot") {
ASdot(p,4,markerstroke,markerfill);
if (marker=="arrowdot") arrowhead(p,q);
ASdot(q,4,markerstroke,markerfill);
} else if (marker=="arrow") arrowhead(p,q);
}
function path(plist,id,c) {
if (c==null) c="";
var node, st, i;
if (id!=null) node = doc.getElementById(id);
if (node==null) {
node = myCreateElementSVG("path");
node.setAttribute("id", id);
svgpicture.appendChild(node);
}
if (typeof plist == "string") st = plist;
else {
st = "M";
st += (plist[0][0]*xunitlength+origin[0])+","+
(height-plist[0][1]*yunitlength-origin[1])+" "+c;
for (i=1; i<plist.length; i++)
st += (plist[i][0]*xunitlength+origin[0])+","+
(height-plist[i][1]*yunitlength-origin[1])+" ";
}
node.setAttribute("d", st);
node.setAttribute("stroke-width", strokewidth);
if (strokedasharray!=null)
node.setAttribute("stroke-dasharray", strokedasharray);
node.setAttribute("stroke", stroke);
node.setAttribute("fill", fill);
if (marker=="dot" || marker=="arrowdot")
for (i=0; i<plist.length; i++)
if (c!="C" || i!=1 && i!=2)
ASdot(plist[i],4,markerstroke,markerfill);
}
function curve(plist,id) {
path(plist,id,"T");
}
function circle(center,radius,id) { // coordinates in units
var node;
if (id!=null) node = doc.getElementById(id);
if (node==null) {
node = myCreateElementSVG("circle");
node.setAttribute("id", id);
svgpicture.appendChild(node);
}
node.setAttribute("cx",center[0]*xunitlength+origin[0]);
node.setAttribute("cy",height-center[1]*yunitlength-origin[1]);
node.setAttribute("r",radius*xunitlength);
node.setAttribute("stroke-width", strokewidth);
node.setAttribute("stroke", stroke);
node.setAttribute("fill", fill);
}
function arc(start,end,radius,id) { // coordinates in units
var node;
if (id!=null) node = doc.getElementById(id);
if (node==null) {
node = myCreateElementSVG("path");
node.setAttribute("id", id);
svgpicture.appendChild(node);
}
node.setAttribute("d","M"+(start[0]*xunitlength+origin[0])+","+
(height-start[1]*yunitlength-origin[1])+" A"+radius*xunitlength+","+
radius*xunitlength+" 0 0,0 "+(end[0]*xunitlength+origin[0])+","+
(height-end[1]*yunitlength-origin[1]));
node.setAttribute("stroke-width", strokewidth);
node.setAttribute("stroke", stroke);
node.setAttribute("fill", fill);
}
function ellipse(center,rx,ry,id) { // coordinates in units
var node;
if (id!=null) node = doc.getElementById(id);
if (node==null) {
node = myCreateElementSVG("ellipse");
node.setAttribute("id", id);
svgpicture.appendChild(node);
}
node.setAttribute("cx",center[0]*xunitlength+origin[0]);
node.setAttribute("cy",height-center[1]*yunitlength-origin[1]);
node.setAttribute("rx",rx*xunitlength);
node.setAttribute("ry",ry*yunitlength);
node.setAttribute("stroke-width", strokewidth);
node.setAttribute("stroke", stroke);
node.setAttribute("fill", fill);
}
function rect(p,q,id,rx,ry) { // opposite corners in units, rounded by radii
var node;
if (id!=null) node = doc.getElementById(id);
if (node==null) {
node = myCreateElementSVG("rect");
node.setAttribute("id", id);
svgpicture.appendChild(node);
}
node.setAttribute("x",p[0]*xunitlength+origin[0]);
node.setAttribute("y",height-q[1]*yunitlength-origin[1]);
node.setAttribute("width",(q[0]-p[0])*xunitlength);
node.setAttribute("height",(q[1]-p[1])*yunitlength);
if (rx!=null) node.setAttribute("rx",rx*xunitlength);
if (ry!=null) node.setAttribute("ry",ry*yunitlength);
node.setAttribute("stroke-width", strokewidth);
node.setAttribute("stroke", stroke);
node.setAttribute("fill", fill);
}
function text(p,st,pos,id) {
var textanchor = "middle";
var dx = 0; var dy = 5;
if (pos!=null) {
if (pos.slice(0,5)=="above") dy = -10;
if (pos.slice(0,5)=="below") dy = 20;
if (pos.slice(0,5)=="right" || pos.slice(5,10)=="right") {
textanchor = "start";
dx = 8;
}
if (pos.slice(0,4)=="left" || pos.slice(5,9)=="left") {
textanchor = "end";
dx = -8;
}
}
var node;
if (id!=null) node = doc.getElementById(id);
if (node==null) {
node = myCreateElementSVG("text");
node.setAttribute("id", id);
svgpicture.appendChild(node);
node.appendChild(doc.createTextNode(st));
}
node.lastChild.nodeValue = st;
node.setAttribute("x",p[0]*xunitlength+origin[0]+dx);
node.setAttribute("y",height-p[1]*yunitlength-origin[1]+dy);
node.setAttribute("font-style",fontstyle);
node.setAttribute("font-family",fontfamily);
node.setAttribute("font-size",fontsize);
node.setAttribute("font-weight",fontweight);
node.setAttribute("text-anchor",textanchor);
if (fontstroke!="none") node.setAttribute("stroke",fontstroke);
if (fontfill!="none") node.setAttribute("fill",fontfill);
return p;
}
function ASdot(center,radius,s,f) { // coordinates in units, radius in pixel
if (s==null) s = stroke; if (f==null) f = fill;
var node = myCreateElementSVG("circle");
node.setAttribute("cx",center[0]*xunitlength+origin[0]);
node.setAttribute("cy",height-center[1]*yunitlength-origin[1]);
node.setAttribute("r",radius);
node.setAttribute("stroke-width", strokewidth);
node.setAttribute("stroke", s);
node.setAttribute("fill", f);
svgpicture.appendChild(node);
}
function dot(center, typ, label, pos, id) {
var node;
var cx = center[0]*xunitlength+origin[0];
var cy = height-center[1]*yunitlength-origin[1];
if (id!=null) node = doc.getElementById(id);
if (typ=="+" || typ=="-" || typ=="|") {
if (node==null) {
node = myCreateElementSVG("path");
node.setAttribute("id", id);
svgpicture.appendChild(node);
}
if (typ=="+") {
node.setAttribute("d",
" M "+(cx-ticklength)+" "+cy+" L "+(cx+ticklength)+" "+cy+
" M "+cx+" "+(cy-ticklength)+" L "+cx+" "+(cy+ticklength));
node.setAttribute("stroke-width", .5);
node.setAttribute("stroke", axesstroke);
} else {
if (typ=="-") node.setAttribute("d",
" M "+(cx-ticklength)+" "+cy+" L "+(cx+ticklength)+" "+cy);
else node.setAttribute("d",
" M "+cx+" "+(cy-ticklength)+" L "+cx+" "+(cy+ticklength));
node.setAttribute("stroke-width", strokewidth);
node.setAttribute("stroke", stroke);
}
} else {
if (node==null) {
node = myCreateElementSVG("circle");
node.setAttribute("id", id);
svgpicture.appendChild(node);
}
node.setAttribute("cx",cx);
node.setAttribute("cy",cy);
node.setAttribute("r",dotradius);
node.setAttribute("stroke-width", strokewidth);
node.setAttribute("stroke", stroke);
node.setAttribute("fill", (typ=="open"?"white":stroke));
}
if (label!=null)
text(center,label,(pos==null?"below":pos),(id==null?id:id+"label"))
}
function arrowhead(p,q) { // draw arrowhead at q (in units)
var up;
var v = [p[0]*xunitlength+origin[0],height-p[1]*yunitlength-origin[1]];
var w = [q[0]*xunitlength+origin[0],height-q[1]*yunitlength-origin[1]];
var u = [w[0]-v[0],w[1]-v[1]];
var d = Math.sqrt(u[0]*u[0]+u[1]*u[1]);
if (d > 0.00000001) {
u = [u[0]/d, u[1]/d];
up = [-u[1],u[0]];
var node = myCreateElementSVG("path");
node.setAttribute("d","M "+(w[0]-15*u[0]-4*up[0])+" "+
(w[1]-15*u[1]-4*up[1])+" L "+(w[0]-3*u[0])+" "+(w[1]-3*u[1])+" L "+
(w[0]-15*u[0]+4*up[0])+" "+(w[1]-15*u[1]+4*up[1])+" z");
node.setAttribute("stroke-width", markerstrokewidth);
node.setAttribute("stroke", stroke); /*was markerstroke*/
node.setAttribute("fill", stroke); /*was arrowfill*/
svgpicture.appendChild(node);
}
}
function chopZ(st) {
var k = st.indexOf(".");
if (k==-1) return st;
for (var i=st.length-1; i>k && st.charAt(i)=="0"; i--);
if (i==k) i--;
return st.slice(0,i+1);
}
function grid(dx,dy) { // for backward compatibility
axes(dx,dy,null,dx,dy)
}
function axes(dx,dy,labels,gdx,gdy) {
var x, y, ldx, ldy, pnode, st;
if (typeof dx=="string") { labels = dx; dx = null; }
if (typeof dy=="string") { gdx = dy; dy = null; }
dx = (dx==null?xunitlength:dx*xunitlength);
dy = (dy==null?yunitlength:dy*yunitlength);
if (gdx!=null) {
gdx = (typeof gdx=="string"?dx:gdx*xunitlength);
gdy = (gdy==null?dy:gdy*yunitlength);
pnode = myCreateElementSVG("path");
st="";
for (x = origin[0]; x<width; x = x+gdx)
st += " M"+x+",0"+" "+x+","+height;
for (x = origin[0]-gdx; x>0; x = x-gdx)
st += " M"+x+",0"+" "+x+","+height;
for (y = height-origin[1]; y<height; y = y+gdy)
st += " M0,"+y+" "+width+","+y;
for (y = height-origin[1]-gdy; y>0; y = y-gdy)
st += " M0,"+y+" "+width+","+y;
pnode.setAttribute("d",st);
pnode.setAttribute("stroke-width", .5);
pnode.setAttribute("stroke", gridstroke);
pnode.setAttribute("fill", fill);
svgpicture.appendChild(pnode);
}
pnode = myCreateElementSVG("path");
st="M0,"+(height-origin[1])+" "+width+","+
(height-origin[1])+" M"+origin[0]+",0 "+origin[0]+","+height;
for (x = origin[0]+dx; x<width; x = x+dx)
st += " M"+x+","+(height-origin[1]+ticklength)+" "+x+","+
(height-origin[1]-ticklength);
for (x = origin[0]-dx; x>0; x = x-dx)
st += " M"+x+","+(height-origin[1]+ticklength)+" "+x+","+
(height-origin[1]-ticklength);
for (y = height-origin[1]+dy; y<height; y = y+dy)
st += " M"+(origin[0]+ticklength)+","+y+" "+(origin[0]-ticklength)+","+y;
for (y = height-origin[1]-dy; y>0; y = y-dy)
st += " M"+(origin[0]+ticklength)+","+y+" "+(origin[0]-ticklength)+","+y;
if (labels!=null) with (Math) {
ldx = dx/xunitlength;
ldy = dy/yunitlength;
var ddx = floor(1.1-log(ldx)/log(10));
var ddy = floor(1.1-log(ldy)/log(10));
for (x = ldx; x<=xMax; x = x+ldx)
text([x,0],chopZ(x.toFixed(ddx)),"below");
for (x = -ldx; xMin<=x; x = x-ldx)
text([x,0],chopZ(x.toFixed(ddx)),"below");
for (y = ldy; y<=yMax; y = y+ldy)
text([0,y],chopZ(y.toFixed(ddy)),"left");
for (y = -ldy; yMin<=y; y = y-ldy)
text([0,y],chopZ(y.toFixed(ddy)),"left");
}
pnode.setAttribute("d",st);
pnode.setAttribute("stroke-width", .5);
pnode.setAttribute("stroke", axesstroke);
pnode.setAttribute("fill", fill);
svgpicture.appendChild(pnode);
}
function mathjs(st) {
//translate a math formula to js function notation
// a^b --> pow(a,b)
// na --> n*a
// (...)d --> (...)*d
// n! --> factorial(n)
// sin^-1 --> arcsin etc.
//while ^ in string, find term on left and right
//slice and concat new formula string
st = st.replace(/\s/g,"");
if (st.indexOf("^-1")!=-1) {
st = st.replace(/sin^-1/g,"arcsin");
st = st.replace(/cos^-1/g,"arccos");
st = st.replace(/tan^-1/g,"arctan");
st = st.replace(/sec^-1/g,"arcsec");
st = st.replace(/csc^-1/g,"arccsc");
st = st.replace(/cot^-1/g,"arccot");
st = st.replace(/sinh^-1/g,"arcsinh");
st = st.replace(/cosh^-1/g,"arccosh");
st = st.replace(/tanh^-1/g,"arctanh");
st = st.replace(/sech^-1/g,"arcsech");
st = st.replace(/csch^-1/g,"arccsch");
st = st.replace(/coth^-1/g,"arccoth");
}
st = st.replace(/^e
(C) Æliens
27/08/2009
You may not copy or print any of this material without explicit permission of the author or the publisher.
In case of other copyright issues, contact the author.