Change the origin of a rectangle, so that it draws from the top-right corner.

edited June 2017 in How To...

Hello, I need help in drawing a rectangle from its top-right corner, rather than the top-left corner. I've tried using rectMode() but still can't figure out how to get it to its top-right corner. Is there any possible way to do this?

Tagged:

Answers

  • edited June 2017 Answer ✓

    Merely subtract rect()'s width from its coordinate x at its 1st argument: *-:)

    // forum.Processing.org/two/discussion/22870/
    // change-the-origin-of-a-rectangle-so-that-it-draws-
    // from-the-top-right-corner#Item_1
    
    // GoToLoop (2017-Jun-02)
    
    void setup() {
      size(600, 400);
      smooth(3);
      noLoop();
    
      rectMode(CORNER);
      strokeWeight(2.5);
      stroke(#FF0000);
    }
    
    void draw() {
      int x = width>>1, y = height>>1;
      int w = width/2 + width/4, h = height>>2;
    
      background(#0000FF);
    
      fill(#00FF00);
      rect(x, 0, w, h);
    
      fill(#FFFF00);
      rectTopRight(x, y, w, h);
    }
    
    void rectTopRight(float x, float y, float w, float h) {
      rect(x - w, y, w, h);
    }
    
Sign In or Register to comment.