xxxxxxxxxx
//Construction des rayons permettant de déterminer la position de l'image d'un objet par une lentille divergente.
//©J.roussel
//12 Aout 2012
boolean over = false; // If mouse over
boolean move = false; // If mouse down and over
int focale;//focale de la lentille
int posObjet;//position de l'objet
int posAA;//position de l'image
float gamma;//grandissement
int hObjet;//hauteur de l'objet
int w1; //demi-largeur de l'écran
int h1; //demi-hauteur de l'écran
int h2=12;//demi-hauteur de la zone de texte
color gris1,noir1,rouge1,blanc1,bleu1,orange1,couleurActive;//couleurs
void setup() {
size(window.innerWidth,int(window.innerWidth/2));
smooth();
strokeCap(SQUARE);
frameRate(15);
w1=int(width/2);
h1=int(height/2-h2);
focale=-width/6;
posObjet=1.5*focale;
hObjet=height/10;
textAlign(CENTER,BASELINE);
//couleurs
colorMode(RGB,1.0);
noir1=color(0.15,0.15,0.15,0.5);
gris1=color(0.4,0.4,0.4);
rouge1=color(1,0,0);
blanc1=color(1,1,1);
bleu1=color(#0099CC);
orange1=color(#ff9d00);
}
void draw() {
background(.25);
fill(gris1);
textSize(12);
//text("© Jimmy Roussel",width-50,16);
// On teste si la souris est sur l'objet
if(mouseX > (w1+posObjet-20) && mouseX < (w1+posObjet+20) && mouseY < h1 && mouseY >h1-hObjet)
{
over = true;
couleurActive=rouge1;
}
else {
over = false;
couleurActive=gris1;
}
if(move) {
posObjet = mouseX-w1;
}
traceLentilleD(focale);//tracé de la lentille
calcul();
traceObjet(couleurActive);
traceImage(couleurActive);
traceRayons(rouge1);
resultats();
}
// Tracé de la lentille convergente
void traceLentilleD(int f) {
textSize(12);
pushMatrix();
translate(w1,h1);//centrage de l'origine du repère
//tracé de la lentille
fill(bleu1);
stroke(bleu1);
strokeWeight(2);
line(0,h1,0,-h1);
beginShape(TRIANGLES);
vertex(0,h1-10);
vertex(5, h1);
vertex(-5, h1);
vertex(0,-h1+10);
vertex(5, -h1);
vertex(-5, -h1);
endShape();
//Tracé de son axe optique
strokeWeight(1);
stroke(gris1);
fill(gris1);
line(-w1,0,w1,0);
//Tracé des foyers
line(-f,-2,-f,2);
text("F",-f,-5);
line(f,-2,f,2);
text("F'",f,-5);
popMatrix();
}
//Tracé de l'objet
void traceObjet(color couleurObjet) {
fill(couleurObjet);
pushMatrix();
translate(w1+posObjet,h1);
textSize(height/8);
text("\u261D",0,0);//la forme de l'objet est un caractère (codé en js)
textSize(12);
text("Objet",0,-height/8);
popMatrix();
}
// Tracé de l'image
void traceImage(color couleurImage) {
textSize(height/8);
pushMatrix();
translate(w1+posAA,h1);
scale(pow(gamma,2),abs(gamma));
if(gamma<0) {
rotate(PI);
}
fill(couleurImage,0.5);
text("\u261D",0,0);
popMatrix();
strokeWeight(1);
}
//calcul du grandissment et de la position de l'image
void calcul() {
if(posObjet+focale!=0 & posObjet!=0) {
posAA=int(posObjet*focale/(posObjet+focale));
gamma=float(focale)/float(posObjet+focale);
}
if(posObjet+focale==0) {
posAA=-1000;
gamma=1000/focale;
}
if(posObjet==0) {
posAA=0;
gamma=1;
}
}
// Tracé des rayons
void traceRayons(color couleurRayons) {
pushMatrix();
translate(w1,h1);
stroke(couleurRayons,.8);
strokeWeight(2);
if(posObjet<0) {
line(posObjet,-hObjet,w1,-hObjet*w1/posObjet);//passant par l'origine
line(posObjet,-hObjet,0,-hObjet);//parallèle à l'axe optique
line(0,-hObjet,w1,-hObjet+(w1*hObjet)/focale);
line(posObjet,-hObjet,0,-hObjet*focale/(posObjet+focale));//rayon se prolongeant en F
line(0,-hObjet*focale/(posObjet+focale),w1,-hObjet*focale/(posObjet+focale));//rayon émergeant parallèlement
stroke(couleurRayons,0.25);
line(focale,0,w1,-hObjet+(w1*hObjet)/focale);//prolongement passant par F'
line(0,-hObjet*focale/(posObjet+focale),-focale,0);//prolongement en F
line(0,-gamma*hObjet,posAA,-gamma*hObjet);
}
if(posObjet>0) {
line(-w1,hObjet*w1/posObjet,w1,-hObjet*w1/posObjet);//passant par l'origine
line(-w1,-hObjet,0,-hObjet);//parallèle à l'axe optique
line(0,-hObjet,w1,-hObjet*(1-w1/focale));//issu de F'
if(posObjet+focale<0){//objet avant F
line(-w1,-hObjet*(-w1+focale)/(posObjet+focale),0,-hObjet*focale/(posObjet+focale));
line(0,-hObjet*focale/(posObjet+focale),w1,-hObjet*focale/(posObjet+focale));
stroke(couleurRayons,0.25);//ci dessous les prolongements
line(0,-hObjet,posObjet,-hObjet);//parallèle à l'axe optique
line(focale,0,0,-hObjet);//issu de F'
line(0,-hObjet*focale/(posObjet+focale),posObjet,-hObjet);
}
if(posObjet+focale>0){
line(-w1,-hObjet*(-w1+focale)/(posObjet+focale),0,-hObjet*focale/(posObjet+focale));//passant par F
line(0,-hObjet*focale/(posObjet+focale),w1,-hObjet*focale/(posObjet+focale));//emergeant parallèle à l'axe optique
stroke(couleurRayons,0.25);//ci dessous les prolongements
line(0,-hObjet,posObjet,-hObjet);//parallèle à l'axe optique
line(posAA,-hObjet*focale/(posObjet+focale),w1,-hObjet*focale/(posObjet+focale));//parallèle à l'axe optique
line(posAA,-hObjet*focale/(posObjet+focale),0,-hObjet);//issu de F'
line(0,-hObjet*focale/(posObjet+focale),posObjet,-hObjet);}
}
stroke(couleurRayons);
popMatrix();
}
void resultats() {
fill(noir1);
noStroke();
rect(0,height-20,width,20);
textSize(12);
fill(blanc1);
if (posObjet<0) {
text("Objet réel",width/12,height-5);
}
else {
text("Objet virtuel",width/12,height-5);
}
if (posAA<0) {
text("Image virtuelle",width/4,height-5);
}
else {
text("Image réelle",width/4,height-5);
}
text("\u0194 transversal = "+nfp(gamma,2,1),width/2,height-5);
text("\u0194 latéral = "+nfp(pow(gamma,2),2,1),5*width/6,height-5);
}
void mousePressed() {
if(over) {
move = true;
}
}
void mouseReleased() {
move = false;
}