var w,h,ratio,i,s,el,g,div,dragQ,game,my={};function anifracmatchMain(from,to,rel){my.version='0.85';my.from=typeof from!=='undefined'?from:'words';my.to=typeof to!=='undefined'?to:'line';my.rel=typeof rel!=='undefined'?rel:'../';w=360;h=250;circleRadius=75;numClr='orange';denClr='blue';var id="anifrac";s="";s+='
';s+='
Make this fraction:
';if(my.from=='words'){s+='
';}
if(my.from=='frac'){s+='
';s+='';s+='
';}
if(my.to=='pizza'){s+='
Click the pizza →
';circleX=circleRadius;circleY=circleRadius;s+='
';s+='

';s+='
';s+='
';}
if(my.to=='line'){s+='
Click the line →
';s+='
';s+='';s+='
';}
s+='
';var denLt=70;var denTp=15;s+='
Slices:
';s+='
';s+='
';s+='
';s+='
';s+='
';s+='
';s+='
Result:
';s+='
';s+='';s+='
';s+='
';s+='
';console.log("=>",my.from,my.to);var successTp=120;if(my.from=='words'){if(my.to=='pizza')successTp=120;if(my.to=='line')successTp=120;}
if(my.from=='frac'){if(my.to=='pizza')successTp=200;if(my.to=='line')successTp=100;}
s+='
Yes!
';s+='
';s+='
© 2018 MathsIsFun.com v'+my.version+'
';s+='
';document.write(s);if(my.to=='pizza'){el=document.getElementById('canPizza');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);el.addEventListener('touchmove',function(e){var touch=e.targetTouches[0];e.clientX=touch.clientX;e.clientY=touch.clientY;e.touchQ=true;onmousemove(e);e.preventDefault();});el.addEventListener('mousemove',function(e){var rect=el.getBoundingClientRect();var mx=(e.clientX-rect.left)-circleX;var my=(e.clientY-rect.top)-circleY;if(mx*mx+my*my2){n--;document.getElementById("den").value=n;my.den=n;update();}}
function denUp(){var n=getDen();if(n<100){n++;document.getElementById("den").value=n;my.den=n;update();}}
CanvasRenderingContext2D.prototype.drawChosenSectors=function(sectors,denom){g=this;var n=0;if(denom>100){var f=100/denom;numer*=f;denom*=f;console.log("drawSectors="+numer,denom);}
var dAngle=2*Math.PI/denom;var angleNum=0;for(var k=0;k100){var f=100/denom;numer*=f;denom*=f;console.log("drawSectors="+numer,denom);}
var dAngle=2*Math.PI/denom;var angleNum=0;for(var k=0;k=numer)g.fill();g.stroke();angleNum+=dAngle;}}
function drawFrac(g,plusQ,numer,denom,xp,yp){var sz=50;g.clearRect(0,0,g.canvas.width,g.canvas.height);g.font=(sz*1.5)+"px Arial";g.textAlign="center";if(plusQ){}else{g.fillText("-",xp-18,yp+4);}
g.font="bold "+sz+"px Arial";var up=sz*0.2;var dn=sz*0.95;g.fillStyle=numClr;g.fillText(numer,xp,yp-up);g.fillStyle=denClr;g.fillText(denom,xp,yp+dn);g.strokeStyle='black';g.beginPath();g.lineWidth=sz/14;g.moveTo(xp-sz*0.6,yp);g.lineTo(xp+sz*0.6,yp);g.stroke();}
function Words(){var Lang="en";switch(Lang){case "en":LangDescrNumberComma=",";LangDescrDenomUnit="One";LangDescrDenomHalves="Halves";LangDescr10Join=" ";Langand="and";Langpoint="point";LangDescrDenomHyphen="-";LangDescrDenomOrdQ="y";LangDescrDenom_th="th";LangDescrDenom_s="s";langnumexact=new Array('Zero');langthousands=['','Thousand','Million','Billion','Trillion','Quadrillion','Quintillion','Sextillion','Septillion'];langnum=['','One','Two','Three','Four','Five','Six','Seven','Eight','Nine','Ten','Eleven','Twelve','Thirteen','Fourteen','Fifteen','Sixteen','Seventeen','Eighteen','Nineteen','Twenty','Twenty One','Twenty Two','Twenty Three','Twenty Four','Twenty Five','Twenty Six','Twenty Seven','Twenty Eight','Twenty Nine'];langnum10=['Zero','Ten','Twenty','Thirty','Forty','Fifty','Sixty','Seventy','Eighty','Ninety'];langnum100=['Zero','One Hundred','Two Hundred','Three Hundred','Four Hundred','Five Hundred','Six Hundred','Seven Hundred','Eight Hundred','Nine Hundred'];langnumer=['Zero'];langfirstfractions=[null,'Whole','Half','Third','Quarter'];langunitord=['','First','Second','Third','Fourth','Fifth','Sixth','Seventh','Eighth','Ninth','Tenth','Eleventh','Twelfth','Thirteenth','Fourteenth','Fifteenth','Sixteenth','Seventeenth','Eighteenth','Nineteenth'];langtenord=['','Tenth','Twentieth','Thirtieth','Fortieth','Fiftieth','Sixtieth','Seventieth','Eightieth','Ninetieth'];break;case "es":LangDescrNumberComma="";LangDescrDenomUnit="un";LangDescrDenomHalves="medios";LangDescr10Join=" y ";Langand="y";Langpoint="point";LangDescrDenomHyphen=" ";LangDescrDenomOrdQ="n";LangDescrDenom_th="avo";LangDescrDenom_s="s";langnumexact=['cero','uno'];langnumexact[100]='cien';langthousands=['','mil','millón','billón','trillón','cuatrillón','quintillón','sextillón','septillón'];langnum=['','un','dos','tres','cuatro','cinco','seis','siete','ocho','nueve','diez','once','doce','trece','catorce','quince','dieciséis','diecisiete','dieciocho','diecinueve','veinte','veintiuno','veintidós','veintitrés','veinticuatro','veinticinco','veintiséis','veintisiete','veintiocho','veintinueve','treinta',[40,'cuarenta'],[50,'cincuenta'],[60,'sesenta'],[70,'setenta'],[80,'ochenta'],[90,'noventa'],[100,'ciento'],[200,'doscientos'],[300,'trescientos'],[400,'cuatrocientos'],[500,'quinientos'],[600,'seiscientos'],[700,'setecientos'],[800,'ochocientos'],[900,'novecientos']];langnumer=['cero','un',[21,'veintiún'],[31,'treinta y un'],[41,'cuarenta y un'],[51,'cincuenta y un'],[61,'sesenta y un'],[71,'setenta y un'],[81,'ochenta y un'],[91,'noventa y un']];langfirstfractions=['','entero','medio','tercio','cuarto','quinto','sexto','séptimo','octavo','noveno','décimo',[100,'centésimo'],[1000,'milésimo']];langunitord=['','Primero','Segundo','Tercero','Cuarto','Quinto','Sexto','Séptimo','Octavo','Noveno','Décimo','Decimoprimero','Decimosegundo','Decimotercero','Decimocuarto','Decimoquinto','Decimosexto','Decimoséptimo','Decimoctavo','Decimonoveno'];langtenord=['','Décimo','Vigésimo','Trigésimo','Cuadragésimo','Quincuagésimo','Sexagésimo','Septuagésimo','Octogésimo','Nonagésimo'];break;default:}}
Words.prototype.num2words=function(num,reducedfraction,showdenom,decAsFractionQ){return this.str2words(num.toString(),reducedfraction,showdenom,decAsFractionQ);};Words.prototype.str2words=function(num,reducedfraction,showdenom,decAsFractionQ){var s="";var negative_flag="";if(num.charAt(0)=="-"){negative_flag='Negative ';num=num.substring(1);}
var origNumber=num;var parts=num.split('.');var integer=parts[0];var decpart=parts[1];var decimalQ=(showdenom!=0||parts.length>1);if(langnumexact[integer]!=undefined){s=langnumexact[integer];}else{if(showdenom!=0&&langnumer[integer]!=undefined){s=langnumer[integer];}else{for(var i=0;integer.length>0;i++,integer=integer.substr(0,-Math.min(3,integer.length))){var threedig=integer.substr(-Math.min(3,integer.length));if(parseInt(threedig)!=0){if(i==0){s=this.handleThreeDigit(threedig);}else{if(langthousands[i]!=undefined){if(s.length==0){s=this.handleThreeDigit(threedig)+' '+langthousands[i];}else{s=this.handleThreeDigit(threedig)+' '+langthousands[i]+LangDescrNumberComma+' '+s;}}else{s="A Big Number!";}}}}
s=negative_flag+s;}}
if(decimalQ){if(showdenom!=0){if(origNumber=="1")s=LangDescrDenomUnit;s+=' '+this.describeDenom(showdenom,false,origNumber!="1");}else{var decimal="";if(reducedfraction=="n"){decimal=this.handleDecimal(decpart,false,decAsFractionQ);}else{decimal=this.handleDecimal(decpart,true,decAsFractionQ);}
if(decimal.length>0){if(decAsFractionQ){if(s==langnumexact[0]){s=decimal;}else{s+=' '+Langand+' '+decimal;}}else{s+=' '+Langpoint+' '+decimal;}}else{}}}
return(s);};Words.prototype.placeStr=function(power10){var numStr="";if(power10>=0){numStr="1"+"0".repeat(power10);}else{if(power10<-9){return "";}
numStr="0."+"0".repeat(-1-power10)+"1";}
console.log("placeStr numStr="+numStr);var s=this.str2words(numStr);if(s.substr(0,4)=="One "){s=s.substr(4);if(s.substr(0,4)=="One-"){s=s.substr(4);}}
if(s=="One")s="Unit";s+="s";return(s);};Words.prototype.handleThreeDigit=function(number){var s="";if(number.length>=3){if(number.charAt(0)!="0"){var hundreds=number.substr(0,1);s+=langnum100[hundreds];}
number=number.substr(1);}
var twodig=this.handleTwoDigit(number);if(s.length>0&&twodig.length>0)s+=' ';s+=twodig;return(s);};Words.prototype.handleTwoDigit=function(number){number=parseInt(number).toString();if(parseInt(number)<30){return(langnum[number]);}
var s="";var units=parseInt(number.toString().substr(-1));var tens=parseInt(number.toString().substr(0,1));if(units==0){s=langnum10[tens];}else{s=langnum10[tens]+" "+langnum[units];}
return(s);};Words.prototype.handleDecimal=function(numStr,reduceQ,asFractionQ){console.log("handleDecimal="+numStr,reduceQ);var s="";if(numStr=="")return(s);var denominator="1"+"0".repeat(numStr.length);if(reduceQ){}
if(asFractionQ){var num=parseInt(numStr);if(langnumer[numStr]!=undefined){s=langnumer[numStr];}else{s=this.num2words(num);}
console.log("q1="+denominator);s+=" "+this.describeDenom(parseInt(denominator),false,num!=1);}else{for(var i=0;in2){n1=n1+n2;n2=n1-n2;n1=n1-n2;}
if(n2==(Math.round(n2/n1))*n1){x=n1;}else{for(var i=Math.round(n1/2);i>1;i=i-1){if(n1==(Math.round(n1/i))*i)
if(n2==(Math.round(n2/i))*i){x=i;i=-1;}}}
return x;}
function Frac(canName){this.wd=180;this.ht=120;this.lt=145;this.tp=55;var el=document.getElementById(canName);var ratio=2;el.width=this.wd*ratio;el.height=this.ht*ratio;el.style.width=this.wd+"px";el.style.height=this.ht+"px";this.g=el.getContext("2d");this.g.setTransform(ratio,0,0,ratio,0,0);this.labelsQ=true;this.labels=['Slices we have:','Total slices:'];}
Frac.prototype.drawMe=function(numer,denom){var plusQ=true;var xp=this.lt;var yp=this.tp;var sz=50;var g=this.g;g.clearRect(0,0,g.canvas.width,g.canvas.height);g.font=(sz*1.5)+"px Arial";g.textAlign="center";g.fillStyle='#def';g.beginPath();g.rect(xp-35,0,70,this.ht)
g.fill();if(plusQ){}else{g.fillText("-",xp-18,yp+4);}
g.font="bold "+sz+"px Arial";var up=sz*0.2;var dn=sz*0.95;g.fillStyle=numClr;g.fillText(numer,xp,yp-up);g.fillStyle=denClr;g.fillText(denom,xp,yp+dn);if(this.labelsQ){g.font="bold "+(sz/4)+"px Arial";g.textAlign="right";g.fillStyle=numClr;g.fillText(this.labels[0],xp-38,yp-up-sz/3);g.fillStyle=denClr;g.fillText(this.labels[1],xp-38,yp+dn-sz/3);}
g.strokeStyle='black';g.beginPath();g.lineWidth=sz/14;g.moveTo(xp-sz*0.6,yp);g.lineTo(xp+sz*0.6,yp);g.stroke();}
function FracLine(left,top,clr,den){this.clr=clr;this.den=den;this.lt=8;this.tp=0;this.ht=60;this.wd=344;this.hiLiteTop=0;this.fullWd=this.wd+2*this.lt;this.el=document.getElementById('canLine');ratio=2;this.el.width=this.fullWd*ratio;this.el.height=this.ht*ratio;this.el.style.width=this.fullWd+"px";this.el.style.height=this.ht+"px";this.g=this.el.getContext("2d");this.g.setTransform(ratio,0,0,ratio,0,0);this.el.addEventListener('touchmove',this.touchmove.bind(this));this.el.addEventListener('mousemove',this.mousemove.bind(this));this.el.addEventListener('mousedown',this.mousedown.bind(this));}
FracLine.prototype.touchmove=function(e){var touch=e.targetTouches[0];e.clientX=touch.clientX;e.clientY=touch.clientY;e.touchQ=true;this.mousemove(e);e.preventDefault();}
FracLine.prototype.mousemove=function(e){var rect=this.el.getBoundingClientRect();var x=(e.clientX-rect.left);var y=(e.clientY-rect.top);var num=Math.round(this.den*(x-this.lt)/this.wd);var qx=num/this.den*this.wd;this.drawMe();var g=this.g;g.beginPath();g.fillStyle='rgba(0,0,255,0.2)';g.lineWidth=1;g.rect(this.lt,0,qx,20);g.fill();}
FracLine.prototype.mousedown=function(e){var rect=this.el.getBoundingClientRect();var x=(e.clientX-rect.left);var y=(e.clientY-rect.top);this.num=Math.round(this.den*(x-this.lt)/this.wd);my.num=this.num;update();this.drawMe();document.getElementById('lineLbl').style.visibility='hidden';}
FracLine.prototype.drawMe=function(){var g=this.g;g.clearRect(0,0,this.el.width,this.el.height);g.beginPath();g.strokeStyle='black';g.lineWidth=2;g.moveTo(this.lt,this.tp+10);g.lineTo(this.lt+this.wd,this.tp+10);g.stroke();for(var i=0;i<=this.den;i++){var x0=this.lt+this.wd*(i/this.den)+1;g.beginPath();g.fillStyle='black';g.strokeStyle='black';g.lineWidth=2;g.moveTo(x0,this.tp);g.lineTo(x0,this.tp+20);g.stroke();if(i==0||i==this.den){g.font='25px Arial';g.textAlign='center';var nStr='1';if(i==0)nStr='0';g.fillText(nStr,x0,this.tp+41);}else{this.drawFrac(g,x0,this.tp+20,i,this.den,false);}}
var qx=this.num/this.den*this.wd;g.beginPath();g.fillStyle='rgba(0,0,255,0.6)';g.strokeStyle='black';g.lineWidth=1;g.rect(this.lt,0,qx,20);g.fill();g.stroke();}
FracLine.prototype.drawFrac=function(g,xp,yp,num,den){g.font='14px Arial';g.textAlign='center';g.fillStyle='black';g.fillText(num.toString(),xp,yp+14);g.beginPath();g.strokeStyle='black';g.lineWidth=1;g.moveTo(xp-8,yp+17);g.lineTo(xp+8,yp+17);g.stroke();g.fillText(den.toString(),xp,yp+30);}
FracLine.prototype.setDen=function(den){this.den=den;this.drawMe();}
FracLine.prototype.setHiLiteTop=function(top){this.hiLiteTop=top;}
FracLine.prototype.getNearest=function(xmatch){var qx=(parseInt(this.den*(xmatch)/FullWidth+0.5)*FullWidth/this.den);return(qx);};FracLine.prototype.getNumer=function(xmatch){x=parseInt(this.den*xmatch/FullWidth+0.5);return(x);};FracLine.prototype.hiLite=function(Numer){var x0=this.lt+FullWidth*Numer/this.den;g.beginPath();g.fillStyle='rgba(255,0,255,0.7)';g.rect(x0-25,this.top+this.hiLiteTop-5,50,40);g.fill();if(Numer==0||Numer==this.den){g.font='bold 26px Arial';g.textAlign='center';g.fillStyle='black';if(Numer==0){g.fillText('0',x0+2,this.top+this.hiLiteTop+22);}else{g.fillText('1',x0-6,this.top+this.hiLiteTop+22);}}else{drawFrac(x0,this.top+this.hiLiteTop,Numer,this.den,true);}};FracLine.prototype.markMe=function(){if(Numer==0||Numer==this.den){}else{var x0=this.lt+FullWidth*Numer/this.den;drawFrac(x0,this.top+this.hiLiteTop,Numer,this.den,true);}};