canvas = document.getElementById('dessin');


context = canvas.getContext('2d');
if(!context)
{
	alert("Impossible de récupérer le context du canvas");

}

Ax=0;
Ay=0;
Bx=0;
By=0;
Cx=0;
Cy=0;

Ex=0;
Ey=0;
Fx=0;
Fy=0;
rapport_num=0;
rapport_den=0;

papillon=false;
lettreA="A";
lettreB="B";
lettreC="C";
lettreE="E";
lettreF="F";
chainelettre="ABCDEFGHIJKLMOPQRSTUVWXYZ";
echelle=0.1;
consigne="";
tabdistance=[];
boolaide=0;
introv=1;
rapportv=1;
rotation=1; //0,1,2, on procède par échange de sommet
function changeconfiguration()
{
	papillon=!papillon;
	initialisation();
}
function changeleslettres()
{
	var chainelettretmp=chainelettre;
	var index=Math.floor(Math.random()*(chainelettretmp.length-1));
	lettreA=chainelettretmp[index];
	var chainelettretmp=chainelettretmp.substring(0,index)+chainelettretmp.substring(index+1);
	
	var index=Math.floor(Math.random()*(chainelettretmp.length-1));
	lettreB=chainelettretmp[index];
	var chainelettretmp=chainelettretmp.substring(0,index)+chainelettretmp.substring(index+1);
	var index=Math.floor(Math.random()*(chainelettretmp.length-1));
	lettreC=chainelettretmp[index];
	var chainelettretmp=chainelettretmp.substring(0,index)+chainelettretmp.substring(index+1);
	
	var index=Math.floor(Math.random()*(chainelettretmp.length-1));
	lettreD=chainelettretmp[index];
	var chainelettretmp=chainelettretmp.substring(0,index)+chainelettretmp.substring(index+1);
	var index=Math.floor(Math.random()*(chainelettretmp.length-1));
	lettreE=chainelettretmp[index];
	var chainelettretmp=chainelettretmp.substring(0,index)+chainelettretmp.substring(index+1);
	var index=Math.floor(Math.random()*(chainelettretmp.length-1));
	lettreF=chainelettretmp[index];
	var chainelettretmp=chainelettretmp.substring(0,index)+chainelettretmp.substring(index+1);
	
	
}
function generefigure()
{
	if (papillon)
	{
		rotation=Math.floor(Math.random()*2);
	}
	else
	{
		rotation=Math.floor(Math.random()*3);
	}
	
	if (papillon)
	{
		Ax=canvas.width/2;
		Ay=canvas.width/2;	
		if (rotation==0)
		{
			Bx=Math.floor(Math.random()*(canvas.width/3-20))+20;
			By=Math.floor(canvas.height-20-Math.random()*canvas.height/4);
			Cx=Math.floor(canvas.width - 20 - Math.random()*canvas.width/3);
			Cy=Math.floor(canvas.height-20-Math.random()*canvas.height/4);
		}
		else
		{
			Bx=Math.floor(Math.random()*(canvas.width/3-20))+20;
			By=Math.floor(canvas.height-20-Math.random()*canvas.height/4);
			
			Cx=Math.floor(Math.random()*(canvas.width/3-20))+20;
			//Cx=Math.floor(canvas.width - 20 - Math.random()*canvas.width/3);
			Cy=Math.floor(20+Math.random()*(canvas.height/3-20));
		}
	}
	else
	{
		if (rotation==0)
		{
			Ax=Math.floor(Math.random()*(canvas.width/3-20))+20;
			Ay=Math.floor(Math.random()*(canvas.height/3-30))+30;
		
			Bx=Math.floor(Math.random()*(canvas.width/3-20))+20;
			By=Math.floor(canvas.height-20-Math.random()*canvas.height/3);
			Cx=Math.floor(canvas.width - 20 - Math.random()*canvas.width/3);
			Cy=Math.floor(canvas.height/4+ Math.random()*canvas.height/2);
		}
		else
		{
			if (rotation==1)
			{
				Cx=Math.floor(Math.random()*(canvas.width/3-20))+20;
				Cy=Math.floor(Math.random()*(canvas.height/3-30))+30;
			
				Bx=Math.floor(Math.random()*(canvas.width/3-20))+20;
				By=Math.floor(canvas.height-20-Math.random()*canvas.height/3);
				Ax=Math.floor(canvas.width - 20 - Math.random()*canvas.width/3);
				Ay=Math.floor(canvas.height/4+ Math.random()*canvas.height/2);
			}
			else
			{
				Bx=Math.floor(Math.random()*(canvas.width/3-20))+20;
				By=Math.floor(Math.random()*(canvas.height/3-30))+30;
			
				Ax=Math.floor(Math.random()*(canvas.width/3-20))+20;
				Ay=Math.floor(canvas.height-20-Math.random()*canvas.height/3);
				Cx=Math.floor(canvas.width - 20 - Math.random()*canvas.width/3);
				Cy=Math.floor(canvas.height/4+ Math.random()*canvas.height/2);
			}
		}
	}	
	
		rapport_den=Math.floor(Math.random()*5)+5;
	if (papillon)
	{
		rapport_num=-(Math.floor(Math.random()*(rapport_den-1))+1);
	}
	else
	{
		rapport_num=Math.floor(Math.random()*(rapport_den-1))+1;
	}
	Ex=Ax+(Bx-Ax)*rapport_num/rapport_den;
	Ey=Ay+(By-Ay)*rapport_num/rapport_den;
	Fx=Ax+(Cx-Ax)*rapport_num/rapport_den;
	Fy=Ay+(Cy-Ay)*rapport_num/rapport_den;
	
	choixraportconnu= Math.floor(Math.random()*3);
	choixvaleurinconnu1=Math.floor(Math.random()*2); // fraction 1 , incomplète (0=numérateur, 1=denominateurinconnu)
	choixvaleurinconnu2=Math.floor(Math.random()*2); // fraction 2 , incomplète (0=numérateur, 1=denominateurinconnu)
	// AE/AB AF/AC  EF/BC
	tabdistance=[[distance(Ax,Ay,Ex,Ey),distance(Ax,Ay,Bx,By)],[distance(Ax,Ay,Fx,Fy),distance(Ax,Ay,Cx,Cy)],[distance(Ex,Ey,Fx,Fy),distance(Bx,By,Cx,Cy)]];
	consigne="Les droites ("+lettreE+lettreF+") et ("+lettreB+lettreC+") sont parallèles.<br/>";
	switch (choixraportconnu)
	{
		case 0: 
		consigne =consigne+" On donne : "+lettreA+lettreE+"="+distance(Ax,Ay,Ex,Ey)+" , "+lettreA+lettreB+"="+distance(Ax,Ay,Bx,By)+".<br/>";
		
			if (choixvaleurinconnu1==0)
			{
				if (choixvaleurinconnu2==0)
				{
						consigne =consigne+" On donne aussi : "+lettreA+lettreC+"="+distance(Ax,Ay,Cx,Cy)+" , "+lettreB+lettreC+"="+distance(Bx,By,Cx,Cy);
						consigne=consigne+".<br/>Calculer "+lettreA+lettreF+" et "+lettreE+lettreF+'.';
				}
				else
				{
						consigne =consigne+" On donne aussi : "+lettreA+lettreC+"="+distance(Ax,Ay,Cx,Cy)+" , "+lettreE+lettreF+"="+distance(Ex,Ey,Fx,Fy);
						consigne=consigne+".<br/>Calculer "+lettreA+lettreF+" et "+lettreB+lettreC+'.';
				}
			}
			else
			{
				if (choixvaleurinconnu2==0)
				{
						consigne =consigne+" On donne aussi : "+lettreA+lettreF+"="+distance(Ax,Ay,Fx,Fy)+" , "+lettreB+lettreC+"="+distance(Bx,By,Cx,Cy);
						consigne=consigne+".<br/>Calculer "+lettreA+lettreC+" et "+lettreE+lettreF+'.';
				}
				else
				{
						consigne =consigne+" On donne aussi : "+lettreA+lettreF+"="+distance(Ax,Ay,Fx,Fy)+" , "+lettreE+lettreF+"="+distance(Ex,Ey,Fx,Fy);
						consigne=consigne+".<br/>Calculer "+lettreA+lettreC+" et "+lettreB+lettreC+'.';
				}
			}
		break;
		case 1: 
			consigne =consigne+" On donne : "+lettreA+lettreF+"="+distance(Ax,Ay,Fx,Fy)+" , "+lettreA+lettreC+"="+distance(Ax,Ay,Cx,Cy)+".<br/>";
			if (choixvaleurinconnu1==0)
			{
				if (choixvaleurinconnu2==0)
				{
						consigne =consigne+" On donne aussi : "+lettreA+lettreB+"="+distance(Ax,Ay,Bx,By)+" , "+lettreB+lettreC+"="+distance(Bx,By,Cx,Cy);
						consigne=consigne+".<br/>Calculer "+lettreA+lettreE+" et "+lettreE+lettreF+'.';
				}
				else
				{
						consigne =consigne+" On donne aussi : "+lettreA+lettreB+"="+distance(Ax,Ay,Bx,By)+" , "+lettreE+lettreF+"="+distance(Ex,Ey,Fx,Fy);
						consigne=consigne+".<br/>Calculer "+lettreA+lettreE+" et "+lettreB+lettreC+'.';
				}
			}
			else
			{
				if (choixvaleurinconnu2==0)
				{
						consigne =consigne+" On donne aussi : "+lettreA+lettreE+"="+distance(Ax,Ay,Ex,Ey)+" , "+lettreB+lettreC+"="+distance(Bx,By,Cx,Cy);
						consigne=consigne+".<br/>Calculer "+lettreA+lettreB+" et "+lettreE+lettreF+'.';
				}
				else
				{
						consigne =consigne+" On donne aussi : "+lettreA+lettreE+"="+distance(Ax,Ay,Ex,Ey)+" , "+lettreE+lettreF+"="+distance(Ex,Ey,Fx,Fy);
						consigne=consigne+".<br/>Calculer "+lettreA+lettreB+" et "+lettreB+lettreC+'.';
				}
			}
		break;
		case 2: 
		consigne =consigne+" On donne : "+lettreE+lettreF+"="+distance(Ex,Ey,Fx,Fy)+" , "+lettreB+lettreC+"="+distance(Bx,By,Cx,Cy)+".<br/>";
			if (choixvaleurinconnu1==0)
			{
				if (choixvaleurinconnu2==0)
				{
						consigne =consigne+" On donne aussi : "+lettreA+lettreB+"="+distance(Ax,Ay,Bx,By)+" , "+lettreA+lettreC+"="+distance(Ax,Ay,Cx,Cy);
						consigne=consigne+".<br/>Calculer "+lettreA+lettreE+" et "+lettreA+lettreF+'.';
				}
				else
				{
						consigne =consigne+" On donne aussi : "+lettreA+lettreB+"="+distance(Ax,Ay,Bx,By)+" , "+lettreA+lettreF+"="+distance(Ax,Ay,Fx,Fy);
						consigne=consigne+".<br/>Calculer "+lettreA+lettreE+" et "+lettreA+lettreC+'.';
				}
			}
			else
			{
				if (choixvaleurinconnu2==0)
				{
						consigne =consigne+" On donne aussi : "+lettreA+lettreE+"="+distance(Ax,Ay,Ex,Ey)+" , "+lettreA+lettreC+"="+distance(Ax,Ay,Cx,Cy);
						consigne=consigne+".<br/>Calculer "+lettreA+lettreB+" et "+lettreA+lettreF+'.';
				}
				else
				{
						consigne =consigne+" On donne aussi : "+lettreA+lettreE+"="+distance(Ax,Ay,Ex,Ey)+" , "+lettreA+lettreF+"="+distance(Ax,Ay,Fx,Fy);
						consigne=consigne+".<br/>Calculer "+lettreA+lettreB+" et "+lettreA+lettreC+'.';
				}
			}
		break;
	}
	document.getElementById('consigne').innerHTML=consigne;
}
function distance(x,y,c,v)
{
	return Math.floor(echelle*Math.sqrt((x-c)*(x-c)+(y-v)*(y-v)));
}
function frac(num,denom)
{
	chaine='<div class="frac"><span>'+num+'</span><span class="symbol">/</span><span class="bottom">'+denom+'</span></div>';
    return chaine;

}
function fraccouleur(num,denom)
{
	chaine='<div class="frac"><span style="color:green">'+num+'</span><span class="symbol">/</span><span  style="color:red" class="bottom">'+denom+'</span></div>';
    return chaine;
}
function fraccouleur2(num,denom)
{
	chaine='<div class="frac"><span style="color:red">'+num+'</span><span class="symbol">/</span><span  style="color:green" class="bottom">'+denom+'</span></div>';
    return chaine;

}
function pgcd(a,b) 
{  
	
	while (a!=b) 
	{
		if (a>b) 
		{
			a-=b;
		}
		else 
		{
			 b-=a;
		}  
	}
	return a;  
}
function produitencroix(inconnu,a,b,c)
{
	chaine="";
	chaine=chaine+inconnu+'×'+a+'='+b+'×'+c+'<br/>';
	chaine=chaine+inconnu+'×'+a+'='+(b*c)+'<br/>';
	chaine=chaine+inconnu+'='+eval(b*c)+'÷'+a+'<br/>';
	var k=pgcd(a,eval(b*c));
	if (k==a)
	{
			chaine=chaine+inconnu+'='+eval(b*c)/a+'<br/><br/>';
	}
	else
	{
		chaine=chaine+inconnu+'='+frac(eval(b*c)/k,a/k)+'<br/><br/>';
	}
	return chaine;
}
function affichecorrection()
{
	if (boolaide==0)
	{
		aide();
	}
	switch (introv)
	{
		case 0 :
			if (papillon)
			{
				correctionintro="On sait que les droites ("+lettreE+lettreF+") et ("+lettreB+lettreC+") sont parallèles.<br/> "+lettreF+" appartient à ("+lettreC+lettreA+").<br/> et  "+lettreE+" appartient à ("+lettreB+lettreA+").";
			}
			else
			{
				correctionintro="On sait que les droites ("+lettreE+lettreF+") et ("+lettreB+lettreC+") sont parallèles.<br/> "+lettreF+" appartient à ["+lettreC+lettreA+"].<br/> et  "+lettreE+" appartient à ["+lettreB+lettreA+"].";
			}
		break;
		case 1 :
			correctionintro="On sait que les droites ("+lettreE+lettreF+") et ("+lettreB+lettreC+") sont parallèles.<br/>Les droites ("+lettreF+lettreC+") et ("+lettreB+lettreE+") sont sécantes en "+lettreA+"."
		break;
		case 2 :
			correctionintro="On sait que les droites ("+lettreE+lettreF+") et ("+lettreB+lettreC+") sont parallèles.<br/>Les points "+lettreF+","+lettreC+","+lettreA+" sont alignés<br/> et les points "+lettreB+","+lettreE+","+lettreA+" sont alignés.";
		break;
	}
	document.getElementById('introcorrection').innerHTML=correctionintro;
	
	theocorrection="D'après le <b>théorème de Thalès</b>, on a donc : ";
	document.getElementById('theocorrection').innerHTML=theocorrection;
	
	if (rapportv==1)
	{
		conclcorrection =fraccouleur(lettreA+lettreE,lettreA+lettreB)+"="+fraccouleur(lettreA+lettreF,lettreA+lettreC)+"="+fraccouleur(lettreE+lettreF,lettreB+lettreC)+"<br/>";
	}
	else
	{
		conclcorrection =fraccouleur2(lettreA+lettreB,lettreA+lettreE)+"="+fraccouleur2(lettreA+lettreC,lettreA+lettreF)+"="+fraccouleur2(lettreB+lettreC,lettreE+lettreF)+"<br/>";
	}
	document.getElementById('conclcorrection').innerHTML=conclcorrection;

	document.getElementById('intro').style.display="";
	document.getElementById('theo').style.display="";
	document.getElementById('conclusion').style.display="";
	document.getElementById('calcul').style.display="";
	document.getElementById('introcorrection').style.display="";
	document.getElementById('theocorrection').style.display="";
	document.getElementById('conclcorrection').style.display="";
	document.getElementById('calculcorrection').style.display="";
	
	correction ="On remplace les longueurs connues par les valeurs de l'énoncé : <br/>"
	switch (choixraportconnu)
	{
		case 0: 
		
			if (choixvaleurinconnu1==0)
			{
				if (choixvaleurinconnu2==0)
				{
					if (rapportv==1)
					{
						correction =correction+frac(distance(Ax,Ay,Ex,Ey),distance(Ax,Ay,Bx,By))+" = "+frac(lettreA+lettreF,distance(Ax,Ay,Cx,Cy))+" = "+frac(lettreE+lettreF,distance(Bx,By,Cx,Cy))+"<br/>";
					}
					else
					{
						correction =correction+frac(distance(Ax,Ay,Bx,By),distance(Ax,Ay,Ex,Ey))+" = "+frac(distance(Ax,Ay,Cx,Cy),lettreA+lettreF)+" = "+frac(distance(Bx,By,Cx,Cy),lettreE+lettreF)+"<br/>";
					}
					correction =correction+"Les fractions sont égales donc les produits en croix sont égaux.<br/>";
						correction=correction+produitencroix(lettreA+lettreF,distance(Ax,Ay,Bx,By),distance(Ax,Ay,Ex,Ey),distance(Ax,Ay,Cx,Cy));
						correction=correction+produitencroix(lettreE+lettreF,distance(Ax,Ay,Bx,By),distance(Ax,Ay,Ex,Ey),distance(Bx,By,Cx,Cy));
						
						
				}
				else
				{
					if (rapportv==1)
					{
						correction =correction+frac(distance(Ax,Ay,Ex,Ey),distance(Ax,Ay,Bx,By))+" = "+frac(lettreA+lettreF,distance(Ax,Ay,Cx,Cy))+" = "+frac(distance(Ex,Ey,Fx,Fy),lettreB+lettreC)+"<br/>";
					}
					else
					{
						correction =correction+frac(distance(Ax,Ay,Bx,By),distance(Ax,Ay,Ex,Ey))+" = "+frac(distance(Ax,Ay,Cx,Cy),lettreA+lettreF)+" = "+frac(lettreB+lettreC,distance(Ex,Ey,Fx,Fy))+"<br/>";
					}
					correction =correction+"Les fractions sont égales donc les produits en croix sont égaux.<br/>";
					correction=correction+produitencroix(lettreA+lettreF,distance(Ax,Ay,Bx,By),distance(Ax,Ay,Ex,Ey),distance(Ax,Ay,Cx,Cy));
					correction=correction+produitencroix(lettreB+lettreC,distance(Ax,Ay,Ex,Ey),distance(Ax,Ay,Bx,By),distance(Ex,Ey,Fx,Fy));
					
				}
			}
			else
			{
				if (choixvaleurinconnu2==0)
				{
					if (rapportv==1)
					{
						correction =correction+frac(distance(Ax,Ay,Ex,Ey),distance(Ax,Ay,Bx,By))+" = "+frac(distance(Ax,Ay,Fx,Fy),lettreA+lettreC)+" = "+frac(lettreE+lettreF,distance(Bx,By,Cx,Cy))+"<br/>";
					}
					else
					{
						correction =correction+frac(distance(Ax,Ay,Bx,By),distance(Ax,Ay,Ex,Ey))+" = "+frac(lettreA+lettreC,distance(Ax,Ay,Fx,Fy))+" = "+frac(distance(Bx,By,Cx,Cy),lettreE+lettreF)+"<br/>";
					}
					correction =correction+"Les fractions sont égales donc les produits en croix sont égaux.<br/>";
					correction=correction+produitencroix(lettreA+lettreC,distance(Ax,Ay,Ex,Ey),distance(Ax,Ay,Bx,By),distance(Ax,Ay,Fx,Fy));
					correction=correction+produitencroix(lettreE+lettreF,distance(Ax,Ay,Bx,By),distance(Ax,Ay,Ex,Ey),distance(Bx,By,Cx,Cy));
					
				}
				else
				{
					if (rapportv==1)
					{
						correction =correction+frac(distance(Ax,Ay,Ex,Ey),distance(Ax,Ay,Bx,By))+" = "+frac(distance(Ax,Ay,Fx,Fy),lettreA+lettreC)+" = "+frac(distance(Ex,Ey,Fx,Fy),lettreB+lettreC)+"<br/>";
					}
					else
					{
						correction =correction+frac(distance(Ax,Ay,Bx,By),distance(Ax,Ay,Ex,Ey))+" = "+frac(lettreA+lettreC,distance(Ax,Ay,Fx,Fy))+" = "+frac(lettreB+lettreC,distance(Ex,Ey,Fx,Fy))+"<br/>";
					}
						correction =correction+"Les fractions sont égales donc les produits en croix sont égaux.<br/>";
						correction=correction+produitencroix(lettreA+lettreC,distance(Ax,Ay,Ex,Ey),distance(Ax,Ay,Bx,By),distance(Ax,Ay,Fx,Fy));
						correction=correction+produitencroix(lettreB+lettreC,distance(Ax,Ay,Ex,Ey),distance(Ax,Ay,Bx,By),distance(Ex,Ey,Fx,Fy));
				}
			}
		break;
		case 1: 
			if (choixvaleurinconnu1==0)
			{
				if (choixvaleurinconnu2==0)
				{
					if (rapportv==1)
					{
						correction =correction+frac(lettreA+lettreE,distance(Ax,Ay,Bx,By))+" = "+frac(distance(Ax,Ay,Fx,Fy),distance(Ax,Ay,Cx,Cy))+" = "+frac(lettreE+lettreF,distance(Bx,By,Cx,Cy))+"<br/>";
					}
					else
					{
						correction =correction+frac(distance(Ax,Ay,Bx,By),lettreA+lettreE)+" = "+frac(distance(Ax,Ay,Cx,Cy),distance(Ax,Ay,Fx,Fy))+" = "+frac(distance(Bx,By,Cx,Cy),lettreE+lettreF)+"<br/>";
					}
					correction =correction+"Les fractions sont égales donc les produits en croix sont égaux.<br/>";
						correction=correction+produitencroix(lettreA+lettreE,distance(Ax,Ay,Cx,Cy),distance(Ax,Ay,Bx,By),distance(Ax,Ay,Fx,Fy));
						correction=correction+produitencroix(lettreE+lettreF,distance(Ax,Ay,Cx,Cy),distance(Ax,Ay,Fx,Fy),distance(Bx,By,Cx,Cy));
					
						
				}
				else
				{
					if (rapportv==1)
					{
						correction =correction+frac(lettreA+lettreE,distance(Ax,Ay,Bx,By))+" = "+frac(distance(Ax,Ay,Fx,Fy),distance(Ax,Ay,Cx,Cy))+" = "+frac(distance(Ex,Ey,Fx,Fy),lettreB+lettreC)+"<br/>";
					}
					else
					{
						correction =correction+frac(distance(Ax,Ay,Bx,By),lettreA+lettreE)+" = "+frac(distance(Ax,Ay,Cx,Cy),distance(Ax,Ay,Fx,Fy))+" = "+frac(lettreB+lettreC,distance(Ex,Ey,Fx,Fy))+"<br/>";
					}
					correction =correction+"Les fractions sont égales donc les produits en croix sont égaux.<br/>";
					correction=correction+produitencroix(lettreA+lettreE,distance(Ax,Ay,Cx,Cy),distance(Ax,Ay,Bx,By),distance(Ax,Ay,Fx,Fy));
					correction=correction+produitencroix(lettreB+lettreC,distance(Ax,Ay,Fx,Fy),distance(Ax,Ay,Cx,Cy),distance(Ex,Ey,Fx,Fy));
				}
			}
			else
			{
				if (choixvaleurinconnu2==0)
				{
					if (rapportv==1)
					{
						correction =correction+frac(distance(Ax,Ay,Ex,Ey),lettreA+lettreB)+" = "+frac(distance(Ax,Ay,Fx,Fy),distance(Ax,Ay,Cx,Cy))+" = "+frac(lettreE+lettreF,distance(Bx,By,Cx,Cy))+"<br/>";
					}
					else
					{
						correction =correction+frac(lettreA+lettreB,distance(Ax,Ay,Ex,Ey))+" = "+frac(distance(Ax,Ay,Cx,Cy),distance(Ax,Ay,Fx,Fy))+" = "+frac(distance(Bx,By,Cx,Cy),lettreE+lettreF)+"<br/>";
					}
					correction =correction+"Les fractions sont égales donc les produits en croix sont égaux.<br/>";
					correction=correction+produitencroix(lettreA+lettreB,distance(Ax,Ay,Fx,Fy),distance(Ax,Ay,Ex,Ey),distance(Ax,Ay,Cx,Cy));
					correction=correction+produitencroix(lettreE+lettreF,distance(Ax,Ay,Cx,Cy),distance(Ax,Ay,Fx,Fy),distance(Bx,By,Cx,Cy));
				}
				else
				{
					if (rapportv==1)
					{
						correction =correction+frac(distance(Ax,Ay,Ex,Ey),lettreA+lettreB)+" = "+frac(distance(Ax,Ay,Fx,Fy),distance(Ax,Ay,Cx,Cy))+" = "+frac(distance(Ex,Ey,Fx,Fy),lettreB+lettreC)+"<br/>";
					}
					else
					{
						correction =correction+frac(lettreA+lettreB,distance(Ax,Ay,Ex,Ey))+" = "+frac(distance(Ax,Ay,Cx,Cy),distance(Ax,Ay,Fx,Fy))+" = "+frac(lettreB+lettreC,distance(Ex,Ey,Fx,Fy))+"<br/>";
					}
					correction =correction+"Les fractions sont égales donc les produits en croix sont égaux.<br/>";
					correction=correction+produitencroix(lettreA+lettreB,distance(Ax,Ay,Fx,Fy),distance(Ax,Ay,Ex,Ey),distance(Ax,Ay,Cx,Cy));
					correction=correction+produitencroix(lettreB+lettreC,distance(Ax,Ay,Fx,Fy),distance(Ax,Ay,Cx,Cy),distance(Ex,Ey,Fx,Fy));
				}
			}
		break;
		case 2: 
			if (choixvaleurinconnu1==0)
			{
				if (choixvaleurinconnu2==0)
				{
					if (rapportv==1)
					{
						correction =correction+frac(lettreA+lettreE,distance(Ax,Ay,Bx,By))+" = "+frac(lettreA+lettreF,distance(Ax,Ay,Cx,Cy))+" = "+frac(distance(Ex,Ey,Fx,Fy),distance(Bx,By,Cx,Cy))+"<br/>";
					}
					else
					{
						correction =correction+frac(distance(Ax,Ay,Bx,By),lettreA+lettreE)+" = "+frac(distance(Ax,Ay,Cx,Cy),lettreA+lettreF)+" = "+frac(distance(Bx,By,Cx,Cy),distance(Ex,Ey,Fx,Fy))+"<br/>";
					}
						correction =correction+"Les fractions sont égales donc les produits en croix sont égaux.<br/>";
						correction=correction+produitencroix(lettreA+lettreE,distance(Bx,By,Cx,Cy),distance(Ax,Ay,Bx,By),distance(Ex,Ey,Fx,Fy));
						correction=correction+produitencroix(lettreA+lettreF,distance(Bx,By,Cx,Cy),distance(Ax,Ay,Cx,Cy),distance(Ex,Ey,Fx,Fy));
						
				}
				else
				{
					if (rapportv==1)
					{
						correction =correction+frac(lettreA+lettreE,distance(Ax,Ay,Bx,By))+" = "+frac(distance(Ax,Ay,Fx,Fy),lettreA+lettreC)+" = "+frac(distance(Ex,Ey,Fx,Fy),distance(Bx,By,Cx,Cy))+"<br/>";
					}
					else
					{
						correction =correction+frac(distance(Ax,Ay,Bx,By),lettreA+lettreE)+" = "+frac(lettreA+lettreC,distance(Ax,Ay,Fx,Fy))+" = "+frac(distance(Bx,By,Cx,Cy),distance(Ex,Ey,Fx,Fy))+"<br/>";
					}
					correction =correction+"Les fractions sont égales donc les produits en croix sont égaux.<br/>";
					correction=correction+produitencroix(lettreA+lettreE,distance(Bx,By,Cx,Cy),distance(Ax,Ay,Bx,By),distance(Ex,Ey,Fx,Fy));
					correction=correction+produitencroix(lettreA+lettreC,distance(Ex,Ey,Fx,Fy),distance(Ax,Ay,Fx,Fy),distance(Bx,By,Cx,Cy));
				}
			}
			else
			{
				if (choixvaleurinconnu2==0)
				{
					if (rapportv==1)
					{
						correction =correction+frac(distance(Ax,Ay,Ex,Ey),lettreA+lettreB)+" = "+frac(lettreA+lettreF,distance(Ax,Ay,Cx,Cy))+" = "+frac(distance(Ex,Ey,Fx,Fy),distance(Bx,By,Cx,Cy))+"<br/>";
					}
					else
					{
						correction =correction+frac(lettreA+lettreB,distance(Ax,Ay,Ex,Ey))+" = "+frac(distance(Ax,Ay,Cx,Cy),lettreA+lettreF)+" = "+frac(distance(Bx,By,Cx,Cy),distance(Ex,Ey,Fx,Fy))+"<br/>";
					}
						correction =correction+"Les fractions sont égales donc les produits en croix sont égaux.<br/>";
						correction=correction+produitencroix(lettreA+lettreB,distance(Ex,Ey,Fx,Fy),distance(Ax,Ay,Ex,Ey),distance(Bx,By,Cx,Cy));
						correction=correction+produitencroix(lettreA+lettreF,distance(Bx,By,Cx,Cy),distance(Ax,Ay,Cx,Cy),distance(Ex,Ey,Fx,Fy));
						
						
				}
				else
				{
					if (rapportv==1)
					{
						correction =correction+frac(distance(Ax,Ay,Ex,Ey),lettreA+lettreB)+" = "+frac(distance(Ax,Ay,Fx,Fy),lettreA+lettreC)+" = "+frac(distance(Ex,Ey,Fx,Fy),distance(Bx,By,Cx,Cy))+"<br/>";
					}
					else
					{
						correction =correction+frac(lettreA+lettreB,distance(Ax,Ay,Ex,Ey))+" = "+frac(lettreA+lettreC,distance(Ax,Ay,Fx,Fy))+" = "+frac(distance(Bx,By,Cx,Cy),distance(Ex,Ey,Fx,Fy))+"<br/>";
					}
						correction =correction+"Les fractions sont égales donc les produits en croix sont égaux.<br/>";
						correction=correction+produitencroix(lettreA+lettreB,distance(Ex,Ey,Fx,Fy),distance(Ax,Ay,Ex,Ey),distance(Bx,By,Cx,Cy));
						correction=correction+produitencroix(lettreA+lettreC,distance(Ex,Ey,Fx,Fy),distance(Ax,Ay,Fx,Fy),distance(Bx,By,Cx,Cy));
						
				}
			}
		break;
	}
	document.getElementById('calculcorrection').innerHTML=correction;
}
function aide()
{
	boolaide=(boolaide+1)%2;
	 dessinefigure()
	 if (boolaide==1)
	 {
		 context.beginPath();
		context.lineWidth = 10;
		context.lineCap = "round";
		context.strokeStyle = "#FF000075";
		context.moveTo(Ax,Ay);
		context.lineTo(Bx,By);
		context.lineTo(Cx,Cy);
		context.lineTo(Ax,Ay);
		context.stroke();

		context.closePath();
		
		context.beginPath();
		context.lineWidth = 15;
		context.lineCap = "round";
		context.strokeStyle = "#00880045";
		context.moveTo(Ax,Ay);
		context.lineTo(Ex,Ey);
		context.lineTo(Fx,Fy);
		context.lineTo(Ax,Ay);
		context.stroke();

		context.closePath();
	}
}
function dessinefigure()
{
	clearCanvas();
	context.font = "20px Arial";

	context.beginPath();
	context.lineWidth = 3;
	context.moveTo(Ax-5,Ay-5);
	context.lineTo(Ax+5,Ay+5);
	context.moveTo(Ax-5,Ay+5);
	context.lineTo(Ax+5,Ay-5);
	context.stroke();
	
	if (papillon)
	{
		if (rotation==0)
		{
			context.textAlign = "right";
			context.textBaseline = "middle"; 
			context.fillText(lettreA, Ax-15, Ay); 
		}
		else
		{
			context.textAlign = "center";
			context.textBaseline = "bottom"; 
			context.fillText(lettreA, Ax, Ay-15); 
		}
	}
	else
	{
		switch (rotation)
		{
			case 0:
				context.textAlign = "right";
				context.textBaseline = "bottom"; 
				context.fillText(lettreA, Ax-5, Ay-5); 
			break;
			case 1:
				context.textAlign = "left";
				context.textBaseline = "bottom"; 
				context.fillText(lettreA, Ax+5, Ay-5); 
			break;
			case 2:
				context.textAlign = "left";
				context.textBaseline = "top"; 
				context.fillText(lettreA, Ax-5, Ay+5); 
			break;
		}
	}
	
	context.closePath();
	
		context.beginPath();

	context.moveTo(Bx-5,By-5);
	context.lineTo(Bx+5,By+5);
	context.moveTo(Bx-5,By+5);
	context.lineTo(Bx+5,By-5);
	if (papillon)
	{
		switch (rotation)
		{
			case 0:
				context.textAlign = "right";
				context.textBaseline = "top"; 
				context.fillText(lettreB, Bx-5, By+5); 
			break;
			case 1:
				context.textAlign = "right";
				context.textBaseline = "top"; 
				context.fillText(lettreB, Bx-5, By+5); 
			break;
			case 2:
				context.textAlign = "right";
				context.textBaseline = "top"; 
				context.fillText(lettreB, Bx-5, By+5); 
			break;
		}		
	}
	else
	{
		switch (rotation)
		{
			case 0:
				context.textAlign = "right";
				context.textBaseline = "top"; 
				context.fillText(lettreB, Bx-5, By+5); 
			break;
			case 1:
				context.textAlign = "right";
				context.textBaseline = "top"; 
				context.fillText(lettreB, Bx-5, By+5); 
			break;
			case 2:
				context.textAlign = "right";
				context.textBaseline = "bottom"; 
				context.fillText(lettreB, Bx-5, By-5); 
			break;
		}
	}
	
	context.stroke();

	context.closePath();
	
		context.beginPath();

	context.moveTo(Cx-5,Cy-5);
	context.lineTo(Cx+5,Cy+5);
	context.moveTo(Cx-5,Cy+5);
	context.lineTo(Cx+5,Cy-5);
	context.stroke();
	if (papillon)
	{
		switch (rotation)
	{
		case 0:
			context.textAlign = "left";
			context.textBaseline = "bottom"; 
			context.fillText(lettreC, Cx+5, Cy+5); 
		break;
		case 1:
			context.textAlign = "right";
			context.textBaseline = "bottom"; 
			context.fillText(lettreC, Cx-5, Cy-5); 
		break;
		case 2:
			context.textAlign = "right";
			context.textBaseline = "bottom"; 
			context.fillText(lettreC, Cx-5, Cy-5); 
		break;
	}
	}
	else
	{
		switch (rotation)
		{
			case 0:
				context.textAlign = "left";
				context.textBaseline = "bottom"; 
				context.fillText(lettreC, Cx+5, Cy+5); 
			break;
			case 1:
				context.textAlign = "right";
				context.textBaseline = "bottom"; 
				context.fillText(lettreC, Cx-5, Cy-5); 
			break;
			case 2:
			
				context.textAlign = "left";
				context.textBaseline = "bottom"; 
				context.fillText(lettreC, Cx+5, Cy+5); 
			break;
		}
	}
	context.closePath();
	
		context.beginPath();

	context.moveTo(Ex-5,Ey-5);
	context.lineTo(Ex+5,Ey+5);
	context.moveTo(Ex-5,Ey+5);
	context.lineTo(Ex+5,Ey-5);
	context.stroke();
	
	if (papillon)
	{
		if (Ax<Bx)
		{
			context.textAlign = "left";
			context.textBaseline = "top"; 
		}
		else
		{
			context.textAlign = "left";
			context.textBaseline = "bottom"; 
		}
		context.fillText(lettreE, Ex+5, Ey); 
	}
	else
	{
		if ((((Ax<Bx)&&(rotation==0))||((Ax>Bx)&&(rotation==2))))
		{
			context.textAlign = "right";
			context.textBaseline = "top"; 
			context.fillText(lettreE, Ex-5, Ey); 
		}
		else
		{
			if (rotation==1)
			{
				context.textAlign = "left";
				context.textBaseline = "top"; 
				context.fillText(lettreE, Ex+5, Ey+5); 
			}
			else
			{
				context.textAlign = "right";
				context.textBaseline = "bottom"; 
				context.fillText(lettreE, Ex-5, Ey); 
			}
		}
		
	}
	
	context.closePath();
	
		context.beginPath();
	
	
	context.moveTo(Fx-5,Fy-5);
	context.lineTo(Fx+5,Fy+5);
	context.moveTo(Fx-5,Fy+5);
	context.lineTo(Fx+5,Fy-5);
	context.stroke();
	if (papillon)
	{
		if (rotation==0)
		{
			context.textAlign = "right";
			context.textBaseline = "bottom"; 
			context.fillText(lettreF, Fx-5, Fy); 
		}
		else
		{
			context.textAlign = "left";
			context.textBaseline = "top"; 
			context.fillText(lettreF, Fx+5, Fy+5); 
		}
	}
	else
	{
		switch (rotation)
		{
			case 0:
				context.textAlign = "left";
				context.textBaseline = "bottom"; 
				context.fillText(lettreF, Fx+5, Fy); 
			break;
			case 1:
					context.textAlign = "left";
				context.textBaseline = "bottom"; 
				context.fillText(lettreF, Fx+5, Fy); 
			break;
			case 2:
					context.textAlign = "left";
					context.textBaseline = "top"; 
					context.fillText(lettreF, Fx+5, Fy+5); 
			break;
		}

	}
	context.closePath();
	
	if (papillon)
	{
		context.beginPath();
		context.moveTo(Ex,Ey);
		context.lineTo(Bx,By);
		context.lineTo(Cx,Cy);
		context.lineTo(Fx,Fy);
		context.stroke();
		context.closePath();
	}
	else
	{
		context.beginPath();
		context.moveTo(Ax,Ay);
		context.lineTo(Bx,By);
		context.lineTo(Cx,Cy);
		context.lineTo(Ax,Ay);
		context.stroke();
		context.closePath();
	}	
	context.beginPath();

	context.moveTo(Ex,Ey);
	context.lineTo(Fx,Fy);
	context.stroke();

	context.closePath();
}

function getMousePos(canvas, evt) {
        var rect = canvas.getBoundingClientRect();
        return {
          x: evt.clientX - rect.left,
          y: evt.clientY - rect.top
        };
}

function changeintro()
{
	introv=(introv+1)%3;
	affichecorrection();
}

function changeconclusion()
{
	rapportv=(rapportv+1)%2;
	affichecorrection();
}

function initialisation()
{
	boolaide=0;
	changeleslettres();
	generefigure();
	dessinefigure();
	document.getElementById('conclcorrection').innerHTML='';
	document.getElementById('introcorrection').innerHTML='';
	document.getElementById('calculcorrection').innerHTML='';
	document.getElementById('theocorrection').innerHTML='';
	document.getElementById('introcorrection').style.display="none";
	document.getElementById('theocorrection').style.display="none";
	document.getElementById('conclcorrection').style.display="none";
	document.getElementById('calculcorrection').style.display="none";
	document.getElementById('intro').style.display="none";
	document.getElementById('theo').style.display="none";
	document.getElementById('conclusion').style.display="none";
	document.getElementById('calcul').style.display="none";
}

function clearCanvas() {
  context.clearRect(0, 0, canvas.width, canvas.height);
  var w = canvas.width;
  canvas.width = 1;
  canvas.width = w;
}

window.load=initialisation();
