var w,h,ratio,el,g,dragIndex,dragging,dragHoldX,dragHoldY,dots,my={};function reflectmodelMain(mode){my.version='0.61';my.mode=typeof mode!=='undefined'?mode:'ellipse';my.modes=[{id:'mirror',pts:[[21,57,"A"],[316,80,"B"],[90,118,"C"],[266,32,"D"]],wallTyp:'rect',animQ:false},{id:'pulse',pts:[[21,57,"A"],[316,80,"B"],[90,118,"C"],[266,32,"D"]],wallTyp:'rect',animQ:true},{id:'walls',pts:[[21,57,"A"],[316,80,"B"],[90,118,"C"],[266,32,"D"]],wallTyp:'ellipse',animQ:true},{id:'ellipse',pts:[[80,140,"F"],[300,140,"G"]],wallTyp:'ellipse',animQ:true},];var modeNo=0 for(var i=0;i';s+='';s+='
' s+='
Segments:
' s+='';s+='
2
';s+='
' if(my.mode.animQ){s+='
' s+=playHTML(36) s+='
'} s+='
© 2018 MathsIsFun.com v'+my.version+'
';s+='';document.write(s);el=document.getElementById('canvas1');ratio=3;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.clrs=[["Blue",'#0000FF'],["Red",'#FF0000'],["Black",'#000000'],["Green",'#00cc00'],["Orange",'#FFA500'],["Slate Blue",'#6A5ACD'],["Lime",'#00FF00'],["Spring Green",'#00FF7F'],["Teal",'#008080'],["Gold",'#ffd700'],["Med Purple",'#aa00aa'],["Light Blue",'#ADD8E6'],["Navy",'#000080'],["Purple",'#800080'],["Dark SeaGreen",'#8FBC8F']];my.clrNum=0;el.addEventListener("mousedown",onMouseDown,false);el.addEventListener('touchstart',onTouchStart,false);el.addEventListener("mousemove",doPointer,false);my.playQ=false my.frameNo=0 my.skipNo=0 my.skipMax=3 dragIndex=0;dots=[];dragging=false;my.dotRad=10 my.rayLen=6 my.rayMax=8 makeDots() my.segs=4 onSegChg(0,6)} function onSegChg(n,v){v=Number(v);my.segs=Math.round(v*v*v/2) console.log("onSegChg="+n,v,my.segs);update() document.getElementById('seg').innerHTML=my.segs;} function raysMake(){my.rays=[] for(var i=0;i1e10){r2.x=r1.x r2.y=r1.y}else{r2.x*=fact r2.y*=fact} r2.x+=iPt.x r2.y+=iPt.y return r2} function deg(rad){return rad*180.0/Math.PI} function anim(){if(my.playQ){my.skipNo++ if(my.skipNo>my.skipMax){my.skipNo=0 for(var i=0;imajorRadius*2){lineQ=false;my.walls=[] return;} var isCircQ=false;if(dist(d0.x-d1.x,d0.y-d1.y)<2){console.log("update: make d0 and d1 very close to simulate circle without problems of divide by zero etc");d0.x=d1.x+0.1;d0.y=d1.y+0.1;isCircQ=true;} var ln=new Line(d0,d1);var distFoci=ln.getDist();ln.setLen(2000);g.strokeStyle='blue';if(!isCircQ){} var mPt=new Pt();mPt.setAvg([d0,d1]);var minorRadius=Math.sqrt(Math.pow(majorRadius,2)-Math.pow(distFoci/2,2));var pts=getArcPts(mPt.x,mPt.y,majorRadius,minorRadius,0,Math.PI*2,my.segs);pts=rotatePts(pts,mPt.x,mPt.y,-ln.getAngle());my.walls=[] for(var i=0;ihighestIndex){dragHoldX=mouseX-dots[i].x;dragHoldY=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-dragHoldX;posX=(posXmaxX)?maxX:posX);posY=mouseY-dragHoldY;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*dytoAngle){while(fromAngle>toAngle){fromAngle-=2*Math.PI;}} var steps=segN;for(var i=0;i<=steps;i++){var radians=fromAngle+(toAngle-fromAngle)*(i/steps);var thisX=midX+(Math.cos(radians)*radiusX);var thisY=midY-(Math.sin(radians)*radiusY);points.push(new Pt(thisX,thisY));} return points;} function rotatePts(pts,midX,midY,rot){var newPts=[];for(var i=0;icentralVal+limitVal) return false;return true;} function Line(pt1,pt2){this.a=pt1;this.b=pt2;} Line.prototype.getIntersection=function(ln,asSegmentsQ){var A=this.a;var B=this.b;var E=ln.a;var F=ln.b;var a1=B.y-A.y;var b1=A.x-B.x;var c1=B.x*A.y-A.x*B.y;var a2=F.y-E.y;var b2=E.x-F.x;var c2=F.x*E.y-E.x*F.y;var denom=a1*b2-a2*b1;if(denom==0){return null;} var ip={x:(b1*c2-b2*c1)/denom,y:(a2*c1-a1*c2)/denom} if(asSegmentsQ){if(Math.pow(ip.x-B.x,2)+Math.pow(ip.y-B.y,2)>Math.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;} Line.prototype.rotatePtMe=function(pt,angle){this.a.x-=pt.x;this.a.y-=pt.y;this.b.x-=pt.x;this.b.y-=pt.y;this.a.rotateMe(angle);this.b.rotateMe(angle);this.a.x+=pt.x;this.a.y+=pt.y;this.b.x+=pt.x;this.b.y+=pt.y;} Line.prototype.getDist=function(){return dist(this.b.x-this.a.x,this.b.y-this.a.y);} Line.prototype.getMidPt=function(){return new Pt((this.a.x+this.b.x)/2,(this.a.y+this.b.y)/2);} Line.prototype.setLen=function(newLen,fromMidQ){fromMidQ=typeof fromMidQ!=='undefined'?fromMidQ:true;var len=this.getLength();if(fromMidQ){var midPt=this.getMidPt();var halfPt=new Pt(this.a.x-midPt.x,this.a.y-midPt.y);halfPt.multiplyMe(newLen/len);this.a=midPt.translate(halfPt);this.b=midPt.translate(halfPt,false);}else{var diffPt=new Pt(this.a.x-this.b.x,this.a.y-this.b.y);diffPt.multiplyMe(newLen/len);this.b=this.a.translate(diffPt,false);}} Line.prototype.getLength=function(){var dx=this.b.x-this.a.x;var dy=this.b.y-this.a.y;return Math.sqrt(dx*dx+dy*dy)} Line.prototype.getAngle=function(){return Math.atan2(this.b.y-this.a.y,this.b.x-this.a.x);} Line.prototype.fmt=function(){return '('+round1(this.a.x)+','+round1(this.a.y)+')=>('+round1(this.b.x)+','+round1(this.b.y)+')['+round2(this.getLength())+']'} function round1(x){return Math.round(x*10)/10} function round2(x){return Math.round(x*100)/100} function Pt(x,y,name){this.x=x this.y=y this.name=typeof name!=='undefined'?name:''} Pt.prototype.add=function(dx,dy){return new Pt(this.x+dx,this.y+dy);} Pt.prototype.rotate=function(angle){var cosa=Math.cos(angle);var sina=Math.sin(angle);var xPos=this.x*cosa+this.y*sina;var yPos=-this.x*sina+this.y*cosa;return new Pt(xPos,yPos);} Pt.prototype.rotateMe=function(angle){var t=new Pt(this.x,this.y).rotate(angle);this.x=t.x;this.y=t.y;} Pt.prototype.getAvg=function(pts){var xSum=0;var ySum=0;for(var i=0;imaxNo){currNo=minNo+(currNo-minNo)%range;} return currNo;}