var w,h,ratio,el,g,drag,dragIndex,dragging,dots,my={} function sincostanMain(mode){my.version='0.5';mode=typeof mode!=='undefined'?mode:'sin';console.log("mode",mode);mode='sin' w=360;h=300;my.lt=100;my.bt=h-50 my.midx=w/2 my.modes=[{id:"sin",name:"Sine",fn:drawTri,pts:[[w/2+80,80,"A"],[w/2+60,my.bt,"B"]]},] my.mode=my.modes[0];for(var i=0;i';s+='';s+='
 
';s+='
 
';s+='
' my.funcs=['sin','cos','tan'] s+=radioHTML('','func',my.funcs,'chgFunc');s+='
';if(my.mode.sideN==0){s+='
' s+='';s+='
' s+='
';s+='
Sides:
' s+='';s+='
'+my.sideN+'
';s+='
';} s+='
© 2018 MathsIsFun.com v'+my.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.dotRad=10 dragging=false;drag={hold:{x:0,y:0}} my.prevTgt={typ:'',a:0,b:0};dotsMake() chgFunc(0) update() el.addEventListener("mousedown",onMouseDown,false);el.addEventListener('touchstart',onTouchStart,false);el.addEventListener("mousemove",doPointer,false);} function chgFunc(n){my.func=my.funcs[n] console.log("chgFunc=",n,my);update()} function update(){g.clearRect(0,0,el.width,el.height);my.mode.fn() dotsDraw()} function drawTri(){var orig={x:50,y:h-50} dots[0].x=Math.max(orig.x+5,dots[0].x) dots[0].y=Math.min(orig.y,dots[0].y) dots[1].x=Math.max(orig.x+80,dots[1].x) dots[1].y=orig.y var r=dots[1].x-orig.x var ang=-Math.atan2(dots[0].y-orig.y,dots[0].x-orig.x) var angDeg=Math.round(ang*(180.0/Math.PI)) ang=angDeg*(Math.PI/180.0) g.strokeStyle='blue' g.fillStyle='black' g.beginPath() g.moveTo(orig.x,orig.y) g.lineTo(dots[1].x,dots[1].y) g.fill();g.stroke() g.fillStyle=g.strokeStyle g.font="18px Arial";g.beginPath() var adj=round2(r/10) g.fillText(adj,(orig.x+dots[1].x)/2,orig.y+20) g.fill();var hypLen=r/Math.cos(ang) var top={x:orig.x+hypLen*Math.cos(ang),y:orig.y-hypLen*Math.sin(ang)} g.strokeStyle='orange' g.fillStyle='black' g.beginPath() g.moveTo(orig.x,orig.y) g.lineTo(top.x,top.y) g.fill();g.stroke() g.fillStyle=g.strokeStyle g.font="18px Arial";g.textAlign='right' g.beginPath() var hyp=round2(dist(dots[1].x-orig.x,orig.y-top.y)/10) g.fillText(hyp,(orig.x+dots[1].x)/2+10,(orig.y+top.y)/2-10) g.fill();g.strokeStyle='red' g.fillStyle='black' g.beginPath() g.moveTo(top.x,orig.y) g.lineTo(top.x,top.y) g.fill();g.stroke() g.fillStyle=g.strokeStyle g.textAlign='left' g.font="18px Arial";g.beginPath() var opp=round2((orig.y-top.y)/10) g.fillText(opp,top.x+5,(orig.y+top.y)/2) g.fill();g.fillStyle='hsla(240,100%,70%,0.6)' g.beginPath() g.moveTo(orig.x,orig.y) g.arc(orig.x,orig.y,50,-ang,0) g.fill();g.fillStyle='black' g.font="18px Arial";g.beginPath() var angStr=angDeg+'°' g.fillText(angStr,orig.x+58-ang*15,orig.y-ang*25) g.fill();var s='' switch(my.func){case 'sin':s='sin('+angStr+') ≈ '+opp+'/'+hyp+' ≈ '+round3(opp/hyp) break case 'cos':s='cos('+angStr+') ≈ '+adj+'/'+hyp+' ≈ '+round3(adj/hyp) break case 'tan':s='tan('+angStr+') ≈ '+opp+'/'+adj+' ≈ '+round3(opp/adj) break default:} var div=document.getElementById('descr') div.innerHTML=s} function round2(x){return Math.round(x*100)/100} function round3(x){return Math.round(x*1000)/1000} function dotsMake(){var i;dots=[];for(i=0;ihighestIndex){drag.hold.x=mouseX-dots[i].x;drag.hold.y=mouseY-dots[i].y;highestIndex=i;dragIndex=i;}}} if(dragging){if(evt.touchQ){window.addEventListener('touchmove',onTouchMove,false);}else{window.addEventListener("mousemove",onMouseMove,false);}} if(evt.touchQ){el.removeEventListener("touchstart",onTouchStart,false);window.addEventListener("touchend",onTouchEnd,false);}else{el.removeEventListener("mousedown",onMouseDown,false);window.addEventListener("mouseup",onMouseUp,false);} if(evt.preventDefault){evt.preventDefault();} else if(evt.returnValue){evt.returnValue=false;} return false;} function onMouseUp(){if(my.playQ)return el.addEventListener("mousedown",onMouseDown,false);window.removeEventListener("mouseup",onMouseUp,false);if(dragging){dragging=false;window.removeEventListener("mousemove",onMouseMove,false);}} function onMouseMove(evt){if(my.playQ)return var posX;var posY;var minX=my.dotRad;var maxX=el.width-my.dotRad;var minY=my.dotRad;var maxY=el.height-my.dotRad;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-drag.hold.x;posX=(posXmaxX)?maxX:posX);posY=mouseY-drag.hold.y;posY=(posYmaxY)?maxY:posY);if(dragging){dots[dragIndex].x=posX;dots[dragIndex].y=posY;} update();} function hitTest(shape,x,y){var dx;var dy;dx=x-shape.x;dy=y-shape.y;return(dx*dx+dy*dyMath.pow(A.x-B.x,2)+Math.pow(A.y-B.y,2)){return null;} if(Math.pow(ip.x-A.x,2)+Math.pow(ip.y-A.y,2)>Math.pow(A.x-B.x,2)+Math.pow(A.y-B.y,2)){return null;} if(Math.pow(ip.x-F.x,2)+Math.pow(ip.y-F.y,2)>Math.pow(E.x-F.x,2)+Math.pow(E.y-F.y,2)){return null;} if(Math.pow(ip.x-E.x,2)+Math.pow(ip.y-E.y,2)>Math.pow(E.x-F.x,2)+Math.pow(E.y-F.y,2)){return null;}} return ip;} function radioHTML(prompt,id,lbls,func){var s='';s+='
';s+=prompt;for(var i=0;i';s+='';} s+='
';return s;}