/** * Arctangent. Alba G. Corral. Marzo 2008 * * ejercicio de arcotangente, perpendicular y tangente. * click mouse to begin / haz click para empezar */ import processing.pdf.*; Eye e1; int cont = 0; void setup() { size(500, 500); noStroke(); iniciar(); //begin record pdf // beginRecord(PDF, "record/everything0-####.pdf"); } void draw() { smooth(); if ( cont < 1000) { cont++; e1.update((int)random(width), (int)random(height)); e1.display(); } if (mousePressed) { endRecord(); iniciar(); } } void keyPressed() { if(keyPressed){ println("key:"+key); switch (key){ case 'q': //exit and end record pdf //endRecord(); exit(); } } } void iniciar(){ cont = 0; e1 = new Eye( width/2, height/2, (int) random(250)); } class Eye { float hh = 0; int ex, ey; int size; float angle = 0.0; Eye(int x, int y, int s) { background(255); ex = x; ey = y; size = s; } void update(int mx, int my) { //para hayar la recta a un punto tenemos que saber su angulo, esto lo hacemos con la arcotangente, que es la inversa de la tangente q //e as u vez es la razon entre el cateto opuesto y el contiguo. angle = atan2(my-ey, mx-ex); } void display() { pushMatrix(); stroke(0); translate(ex, ey); noFill(); rotate(angle+PI/2); stroke(0,0,0,10); //ellipse(size, 0, size/3, size/3); for(int i=1; i<5; i=i+1) { line(size/i,0,cos(angle)*size/2,sin(angle)*size/2); } stroke(0,20); strokeWeight(1); pushMatrix(); translate(size/2,0); rotate(PI/2-angle); if (hh > 255) hh = 0; hh++; stroke(hh,0,0,20); line(0,0,cos(angle)*size,sin(angle)*size); line(0,0,-cos(angle)*size,-sin(angle)*size); popMatrix(); popMatrix(); } }