var w,h,el,g,ratio,shapes,my={};function geomvectorMain(mode){this.version='0.93';my.mode=typeof mode!=='undefined'?mode:'vector';var canvasid="canvas"+my.mode;my.dragging=false;w=540;h=360;var s='';s+='
';s+='';s+='
';s+='';s+='
';s+='
';s+='';s+='';s+='
';s+='
© 2019 MathsIsFun.com v'+this.version+'
';s+='
';document.write(s);el=document.getElementById(canvasid);ratio=2;el.width=w*ratio;el.height=h*ratio;el.style.width=w+"px";el.style.height=h+"px";g=el.getContext("2d");g.setTransform(ratio,0,0,ratio,0,0);my.coords=new Coords(0,0,w,h,-2,-1.9,15,11,true);my.graph=new Graph(g,my.coords);my.dragNo=0;my.coordsQ=false;shapes=[];makeShapes();el.addEventListener("mousedown",mouseDownListener,false);el.addEventListener('touchstart',ontouchstart,false);el.addEventListener("mousemove",domousemove,false);doType();} function winnewSvg(){var s='' s+='' s+='' s+='' s+='' s+='' s+='' return s} function winNew(){window.open(toLoc('../appcebf.html?folder=geometry&file=geom-vector&p='+my.mode))} function toLoc(s){if(window.location.href.indexOf('localhost')>0)s='/mathsisfun'+s return s} function reset(){makeShapes();update();} function update(){doType();} function toggleCoords(){my.coordsQ=!my.coordsQ;toggleBtn("coordsBtn",my.coordsQ);update();} function toggleBtn(btn,onq){if(onq){document.getElementById(btn).classList.add("hi");document.getElementById(btn).classList.remove("lo");}else{document.getElementById(btn).classList.add("lo");document.getElementById(btn).classList.remove("hi");}} function ontouchstart(evt){var touch=evt.targetTouches[0];evt.clientX=touch.clientX;evt.clientY=touch.clientY;evt.touchQ=true;mouseDownListener(evt)} function ontouchmove(evt){var touch=evt.targetTouches[0];evt.clientX=touch.clientX;evt.clientY=touch.clientY;evt.touchQ=true;mouseMoveListener(evt);evt.preventDefault();} function ontouchend(){el.addEventListener('touchstart',ontouchstart,false);window.removeEventListener("touchend",ontouchend,false);if(my.dragging){my.dragging=false;window.removeEventListener("touchmove",ontouchmove,false);}} function domousemove(e){document.body.style.cursor="default";var bRect=el.getBoundingClientRect();var mouseX=(e.clientX-bRect.left)*(el.width/ratio/bRect.width);var mouseY=(e.clientY-bRect.top)*(el.height/ratio/bRect.height);for(var i=0;ihighestIndex){my.dragHoldX=mouseX-shapes[i].x;my.dragHoldY=mouseY-shapes[i].y;highestIndex=i;my.dragNo=i;}}} if(my.dragging){if(evt.touchQ){window.addEventListener('touchmove',ontouchmove,false);}else{window.addEventListener("mousemove",mouseMoveListener,false);} doType();} if(evt.touchQ){el.removeEventListener("touchstart",ontouchstart,false);window.addEventListener("touchend",ontouchend,false);}else{el.removeEventListener("mousedown",mouseDownListener,false);window.addEventListener("mouseup",mouseUpListener,false);} if(evt.preventDefault){evt.preventDefault();} else if(evt.returnValue){evt.returnValue=false;} return false;} function mouseUpListener(){el.addEventListener("mousedown",mouseDownListener,false);window.removeEventListener("mouseup",mouseUpListener,false);if(my.dragging){my.dragging=false;window.removeEventListener("mousemove",mouseMoveListener,false);}} function mouseMoveListener(evt){var posX;var posY;var shapeRad=shapes[my.dragNo].rad;var minX=shapeRad;var maxX=el.width-shapeRad;var minY=shapeRad;var maxY=el.height-shapeRad;var bRect=el.getBoundingClientRect();var mouseX=(evt.clientX-bRect.left)*(el.width/ratio/bRect.width);var mouseY=(evt.clientY-bRect.top)*(el.height/ratio/bRect.height);posX=mouseX-my.dragHoldX;posX=(posXmaxX)?maxX:posX);posY=mouseY-my.dragHoldY;posY=(posYmaxY)?maxY:posY);shapes[my.dragNo].x=posX;shapes[my.dragNo].y=posY;doType();} function hitTest(shape,mx,my){var dx;var dy;dx=mx-shape.x;dy=my-shape.y;return(dx*dx+dy*dymaxNo){currNo=minNo+(currNo-minNo)%range;} return currNo;} function constrain(min,val,max){return(Math.min(Math.max(min,val),max));} function Coords(left,top,width,height,xStt,yStt,xEnd,yEnd,uniScaleQ){this.left=left;this.top=top;this.width=width;this.height=height;this.xStt=xStt;this.yStt=yStt;this.xEnd=xEnd;this.yEnd=yEnd;this.uniScaleQ=uniScaleQ;this.xLogQ=false;this.yLogQ=false;this.skewQ=false;this.xScale;this.xLogScale;this.yScale;this.calcScale();} Coords.prototype.calcScale=function(){if(this.xLogQ){if(this.xStt<=0) this.xStt=1;if(this.xEnd<=0) this.xEnd=1;} if(this.yLogQ){if(this.yStt<=0) this.yStt=1;if(this.yEnd<=0) this.yEnd=1;} var temp;if(this.xStt>this.xEnd){temp=this.xStt;this.xStt=this.xEnd;this.xEnd=temp;} if(this.yStt>this.yEnd){temp=this.yStt;this.yStt=this.yEnd;this.yEnd=temp;} var xSpan=this.xEnd-this.xStt;if(xSpan<=0) xSpan=0.1;this.xScale=xSpan/this.width;this.xLogScale=(Math.log(this.xEnd)-Math.log(this.xStt))/this.width;var ySpan=this.yEnd-this.yStt;if(ySpan<=0) ySpan=0.1;this.yScale=ySpan/this.height;this.yLogScale=(Math.log(this.yEnd)-Math.log(this.yStt))/this.height;if(this.uniScaleQ&&!this.xLogQ&&!this.yLogQ){var newScale=Math.max(this.xScale,this.yScale);this.xScale=newScale;xSpan=this.xScale*this.width;var xMid=(this.xStt+this.xEnd)/2;this.xStt=xMid-xSpan/2;this.xEnd=xMid+xSpan/2;this.yScale=newScale;ySpan=this.yScale*this.height;var yMid=(this.yStt+this.yEnd)/2;this.yStt=yMid-ySpan/2;this.yEnd=yMid+ySpan/2;}};Coords.prototype.getXScale=function(){return this.xScale;};Coords.prototype.getYScale=function(){return this.yScale;};Coords.prototype.toXPix=function(val){if(this.xLogQ){return this.left+(Math.log(val)-Math.log(this.xStt))/this.xLogScale;}else{return this.left+((val-this.xStt)/this.xScale);}};Coords.prototype.toYPix=function(val){if(this.yLogQ){return this.top+(Math.log(this.yEnd)-Math.log(val))/this.yLogScale;}else{return this.top+((this.yEnd-val)/this.yScale);}};Coords.prototype.toPtVal=function(pt,useCornerQ){return new Pt(this.toXVal(pt.x,useCornerQ),this.toYVal(pt.y,useCornerQ));};Coords.prototype.toXVal=function(pix,useCornerQ){if(useCornerQ){return this.xStt+(pix-this.left)*this.xScale;}else{return this.xStt+pix*this.xScale;}};Coords.prototype.toYVal=function(pix,useCornerQ){if(useCornerQ){return this.yEnd-(pix-this.top)*this.yScale;}else{return this.yEnd-pix*this.yScale;}};Coords.prototype.getTicks=function(stt,span){var ticks=[];var inter=this.tickInterval(span/5,false);var tickStt=Math.ceil(stt/inter)*inter;var i=0;var tick;do{var tick=i*inter;tick=Number(tick.toPrecision(5));ticks.push([tickStt+tick,1]);i++;}while(tick=5){if(majorQ){return(5*pow10);}else{return(2*pow10);}} if(mantissa>=2){if(majorQ){return(2*pow10);}else{return(1*pow10);}} if(mantissa>=1){if(majorQ){return(1*pow10);}else{return(0.2*pow10);}} if(majorQ){return(1*pow10);}else{return(0.2*pow10);}};Coords.prototype.xTickInterval=function(tickDensity,majorQ){return this.tickInterval((this.xEnd-this.xStt)/tickDensity,majorQ);};Coords.prototype.yTickInterval=function(tickDensity,majorQ){return this.tickInterval((this.yEnd-this.yStt)/tickDensity,majorQ);};function Graph(g,coords){this.g=g;this.coords=coords;this.xLinesQ=true;this.yLinesQ=true;this.xValsQ=true;this.yValsQ=true;this.skewQ=false;} Graph.prototype.drawGraph=function(){this.hzAxisY=this.coords.toYPix(0);if(this.hzAxisY<0)this.hzAxisY=0;if(this.hzAxisY>this.coords.height)this.hzAxisY=this.coords.height;this.hzNumsY=this.hzAxisY+14;if(this.hzAxisY>this.coords.height-10)this.hzNumsY=this.coords.height-3;this.vtAxisX=this.coords.toXPix(0);if(this.vtAxisX<0)this.vtAxisX=0;if(this.vtAxisX>this.coords.width)this.vtAxisX=this.coords.width;this.vtNumsX=this.vtAxisX-5;if(this.vtAxisX<10)this.vtNumsX=20;if(this.coords.xLogQ){this.drawLinesLogX();}else{if(this.xLinesQ){this.drawHzLines();}} if(this.coords.yLogQ){this.drawLinesLogY();}else{if(this.yLinesQ){this.drawVtLines();}}};Graph.prototype.drawVtLines=function(){var g=this.g;g.lineWidth=1;var ticks=this.coords.getTicks(this.coords.xStt,this.coords.xEnd-this.coords.xStt);for(var i=0;iMath.PI) degDiff-=Math.PI;if(isNear(degDiff,Math.PI/2,toler)){return true;} return false;}else{}};function fmt(num,digits){digits=14;if(num==Number.POSITIVE_INFINITY) return "undefined";if(num==Number.NEGATIVE_INFINITY) return "undefined";num=num.toPrecision(digits);num=num.replace(/0+$/,"");if(num.charAt(num.length-1)==".")num=num.substr(0,num.length-1);if(Math.abs(num)<1e-15)num=0;return num;} function isNear(checkVal,centralVal,limitVal){if(checkValcentralVal+limitVal)return false;return true;} CanvasRenderingContext2D.prototype.drawArrow=function(x0,y0,totLen,shaftHt,headLen,headHt,angle,sweep,invertQ){var g=this;var pts=[[0,0],[-headLen,-headHt/2],[-headLen+sweep,-shaftHt/2],[-totLen,-shaftHt/2],[-totLen,shaftHt/2],[-headLen+sweep,shaftHt/2],[-headLen,headHt/2],[0,0]];if(invertQ){pts.push([0,-headHt/2],[-totLen,-headHt/2],[-totLen,headHt/2],[0,headHt/2]);} for(var i=0;i