• fullscreen
  • geometry.pde
  • PFont font;
    float centerX; 
    float centerY;
    float centerXTarget;
    float centerYTarget;
    float pointX;
    float pointY;
    float pointXTarget;
    float pointYTarget;
    int doubleClickInterval;
    
    void setup() {
      size(500,500);
     font = loadFont("Monospaced-15.vlw");
     textFont(font,15);
     centerX = centerXTarget = width/2;
     centerY = centerYTarget = height/2;
     pointX = pointXTarget = 120;
     pointY = pointYTarget = 300;
     strokeWeight(2);
     smooth();
    }
    
    void draw() {
      background(10);
      
      // Move center towards target
      centerX = (centerXTarget-centerX)/10.0 + centerX;
      centerY = (centerYTarget-centerY)/10.0 + centerY;
      pointX = (pointXTarget-pointX)/10.0 + pointX;
      pointY = (pointYTarget-pointY)/10.0 + pointY;
      
      
      // Timeout the double click as a single click
      if ( doubleClickInterval != 0 &&  (doubleClickInterval + 250 < millis())) {
        pointXTarget = mouseX;
        pointYTarget = mouseY;
        doubleClickInterval = 0;
      }
      
      drawGeometry();
      
    }
    
    void keyPressed() {
      
      if ( key == 'c' ) {
        centerXTarget = width/2;
        centerYTarget = height/2;
      } 
      /*
      // Change strokeWeight through keys
      else if ( key > 48 && key < 58 ) { //keys 1-9
        strokeWeight((key-48)*2 -1 );
      } else if ( key == 48 ) { //key 0
         strokeWeight(19);
      }
      */
     
    }
    
    void mousePressed() {
      
      if ( doubleClickInterval == 0 ) {
        doubleClickInterval = millis();
      } else {
        centerXTarget = mouseX;
        centerYTarget = mouseY;
        doubleClickInterval = 0;
      }
      
    }
    
    void mouseDragged() {
      if ( mousePressed ) {
        pointX = pointXTarget =  mouseX;
        pointY = pointYTarget = mouseY;
      }
      
    }
    
    void drawGeometry() {
      stroke(255,255,0);
      line(centerX,centerY,pointX,pointY);
      stroke(255,0,0);
      line(centerX,centerY,centerX,pointY);
      stroke(0,255,0);
      line(centerX,pointY,pointX,pointY);
      
       
      int distance = (int)dist(centerX,centerY,pointX,pointY);
      float angle = atan2(pointY-centerY,pointX- centerX);
      if (angle < 0 ) angle = angle + TWO_PI;
      noFill();
     
      float[] list = { distance, width-centerX, centerX,height-centerY,centerY };
      float shortDistance = min(list);
     
      stroke(0,0,127);
      ellipse(centerX,centerY,shortDistance*2,shortDistance*2);
      stroke(255,127,0);
      
      arc(centerX,centerY,shortDistance*2,shortDistance*2,0,angle);
      //stroke(0,0,255);
      //arc(centerX,centerY,shortDistance*2,shortDistance*2,TWO_PI+angle,TWO_PI);
      fill(255,127,0);
      float x = cos(angle/2) * shortDistance + centerX + 5;
      float y = sin(angle/2) * shortDistance + centerY + 5;
      noStroke();
      rect(x-1,y-1,50,12);
      fill(0);
      textAlign(LEFT,TOP);
      text(angle,x-5,y);
     
      fill(255,255,0);
      noStroke();
      rect((centerX-pointX)/2+pointX-1,(centerY-pointY)/2+pointY-1,27,12);
      fill(0);
      textAlign(LEFT,TOP);
      
       text(distance,(centerX-pointX)/2+pointX,(centerY-pointY)/2+pointY);
      
      fill(255,0,0);
      distance = (int)dist(centerX,centerY,centerX,pointY);
      
      noStroke();
      rect(centerX-1,(centerY-pointY)/2+pointY-1,27,12);
      fill(0);
      textAlign(LEFT,TOP);
      
      text(distance,centerX, (centerY-pointY)/2+pointY);
      fill(0,255,0);
       distance = (int)dist(centerX,pointY,pointX,pointY);
       
      noStroke();
      rect((centerX-pointX)/2+pointX -1,pointY-1,27,12);
      fill(0);
      textAlign(LEFT,TOP);
       
      text(distance,(centerX-pointX)/2+pointX ,pointY);
      
    }
    
    
    
    

    code

    tweaks (0)

    about this sketch

    This sketch is running as Java applet, exported from Processing.

    license

    advertisement

    Thomas O Fredericks

    Trigonometry

    Add to Faves Me Likey@! 2
    You must login/register to add this sketch to your favorites.

    Click or drag to change point.
    Double click to change the centre.
    Press 'c' to centre to the stage.

    Dennis Daniels
    24 Jan 2011
    Very impressive! I get very excited when I see processing sketches that are more than toys. Your sketch could actually be used for teaching pi/ circumference, area, etc.
    Kudos to you!

    Dennis
    Dennis Daniels
    24 Jan 2011
    Numbers in green do not display correctly over 100.
    Dennis Daniels
    24 Jan 2011
    Some notes from the geometry app you created and I have slightly modified:

    //based on http://openprocessing.org/visuals/?visualID=18290
    //Thomas O Fredericks
    /*

    ideas:
    *load images in background to measure area/ distance/
    *use for triangulating on maps (where are you, game)
    *generate questions for users, calculate missing value based on randomly generated cirles
    and triangles


    issues:
    green numbers over 100 not displayed correctly

    exercise ideas:
    *calculate the number of X sized triangles that can fit
    *squares/ rectangles
    *3D volumes
    *display area of circle
    *dispaly formula calculating circum/ hypotenuse/etc.
    *display area of triangle
    *change color of background based on x,y value or area of triangle: background(draggedX,draggedY,0);//to change up the colors
    (fixes the green number over 100 not working)

    */

    PFont font;
    int clickX;//when clicking on background
    int clickY;
    int draggedX;
    int draggedY;
    boolean centered;

    void setup() {
    size(500,500);
    font = loadFont("BitstreamVeraSansMono-Roman-24.vlw");
    textFont(font,12);
    clickX = width/2;//always middle of sketch
    clickY = height/2;
    centered = true;// circle starts in middle
    smooth();
    }

    void draw() {
    background(draggedX,draggedY,0);//to change up the colors
    drawStuff();

    }

    void keyPressed() {

    if ( key == 'c' ) {//c key does not apparently do anything
    clickX = width/2;
    clickY = height/2;
    } else if ( key == ' ' ) {//space bar recenters mouse location
    clickX = mouseX;
    clickY = mouseY;
    }


    }

    void mousePressed() {
    if ( centered ) {
    draggedX = mouseX;
    draggedY = mouseY;
    } else {
    clickX = mouseX;
    clickY = mouseY;
    draggedX = mouseX;
    draggedY = mouseY;
    }

    }

    void mouseDragged() {
    if ( mousePressed ) {
    draggedX = mouseX;
    draggedY = mouseY;
    }

    }

    void drawStuff() {
    stroke(255);//white line hypotenuse
    line(clickX,clickY,draggedX,draggedY);
    stroke(255,0,0);//red line (red, green, blue)
    line(clickX,clickY,clickX,draggedY);
    stroke(0,255,0);//green line (red, green, blue)
    line(clickX,draggedY,draggedX,draggedY);


    int distance = (int)dist(clickX,clickY,draggedX,draggedY);
    float angle = atan2(draggedY-clickY,draggedX- clickX);
    if (angle &lt; 0 ) angle = angle + TWO_PI;
    noFill();

    stroke(0,0,127);
    ellipse(clickX,clickY,distance*2,distance*2);
    stroke(255,0,255);
    arc(clickX,clickY,distance*2,distance*2,0,angle);
    stroke(0,0,255);
    arc(clickX,clickY,distance*2,distance*2,TWO_PI+angle,TWO_PI);
    fill(255,0,255);
    float x = cos(angle/2) * distance + clickX + 5;
    float y = sin(angle/2) * distance + clickY + 5;
    noStroke();
    rect(x,y,35,10);
    fill(0);
    textAlign(LEFT,TOP);
    text(angle,x,y);

    fill(255);
    noStroke();
    rect((clickX-draggedX)/2+draggedX,(clickY-draggedY)/2+draggedY,25,10);
    fill(0);
    textAlign(LEFT,TOP);

    text(distance,(clickX-draggedX)/2+draggedX,(clickY-draggedY)/2+draggedY);

    fill(255,0,0);
    distance = (int)dist(clickX,clickY,clickX,draggedY);

    noStroke();
    rect(clickX,(clickY-draggedY)/2+draggedY,15,10);
    fill(0);
    textAlign(LEFT,TOP);

    text(distance,clickX, (clickY-draggedY)/2+draggedY);
    fill(0,255,0);
    distance = (int)dist(clickX,draggedY,draggedX,draggedY);

    noStroke();
    rect((clickX-draggedX)/2+draggedX ,draggedY,15,10);
    fill(0);//this the green line
    textAlign(LEFT,TOP);

    text(distance,(clickX-draggedX)/2+draggedX ,draggedY);

    }
    You need to login/register to comment.