Can I count on `width` being a constant (final)?

edited September 2015 in Programming Questions

Greetings.

The following code works just fine in 3.0 beta 5.

size (400, 400);
final float d = width / 2.0;
ellipse (d, d, d, d);

However, the code counts on width being a constant; otherwise the line defining d would fail.

I use this property of width quite extensively in teaching material. (I can justify this in detail, but it's probably not that relevant here.) Is it safe to assume that in Processing width and height are always constants?

Answers

    • Neither width nor height can be constants b/c functions like size() & setSize() need to re-assign them.
    • However, as long as we don't re-invoke those functions above or directly re-assign those variables, we should assume they're fixed.
    • Watch out for surface.setResizable(true); though. As both width & height can be changed by the user.
  • edited September 2015
    void setup() {
      size(200, 150);
      println(width, height);
      surface.setSize(300, 200);
      println(width, height);
      surface.setResizable(true);
      frameRate(1);
    }
    
    void draw() {
      println(width, height);
    }
    
  • Greetings.

    Yes, I knew of the possibility to change width and size. In our examples we never change the size.

    Actually, now that I think of it, my question is kind of weird. I guess it is obvious that you can define a constant using the value of a variable, because this assignment is done only once.

    So my question is pretty much unnecessary. You can even delete it if you want to.

    Sorry and thanks!

Sign In or Register to comment.