How to join two corresponding corners of two rectangles with a line

edited June 2015 in How To...

I want to create a kind of cube/cuboid with two rectangles. I want to know how to join two corresponding corners of two different rectangles using a line. Is there a way to reference the location of each corner of a rectangle?


  • edited June 2015

    a rect is given by

    rect(x,y,w,h); (x and y position and width and height)

    so when you draw a rect this way you already have all necessary data:

    • your first corner is x,y

    • diagonal corner is x+w, y+h

    • lower left corner is x, y+h (only y value changes)

    • upper right corner is x+w, y (only x value changes)


  • That is a good way to do it. Thanks Chrisir.

  • when you write a class Rectangle you can make this more elegant when you use PVector like

    • upperLeftCorner

    • upperRightCorner

    • lowerLeftCorner

    • lowerRightCorner

  • We can also change rectMode() to CORNERS:

  • ClassRect rect;  
    void setup() {
      size(640, 560);
      rect = new ClassRect  (  111, 122, 222, 333  );
    void draw() { 
      text ("Rect  ", 30, 30);
    } // func 
    // =============================
    class ClassRect {
      float x1, y1, // POS 
      rectWidth, rectHeight;   // SIZE 
      PVector upperLeftCorner, upperRightCorner, 
      lowerLeftCorner, lowerRightCorner;
      // constr 
      ClassRect (float x1_, float y1_, 
      float rectWidth_, float rectHeight_) 
        // constr 
      } // constr
      void draw() {
        rect(x1, y1, 
        rectWidth, rectHeight);
      } // method
    } // 
Sign In or Register to comment.