How do I scale shapes without scaling stroke weight?

Hi, so I've made a sketch in which a couple of base shapes and then just copied them, scaling them larger each time. However, when scaling them, while they become the size I want, I can't find a way of keeping the strokes (of the triangles) from also being scaled, can you help?

Here's the code at the moment

void setup() {
  size(1280, 720);
}

void draw() {
  background(255);



  pushMatrix();
  translate(width/2, height/2);
  rotate(frameCount*radians(90) /50);
  translate(0, -9);
  noFill();
  triangle (-30, 26, 0, -26, 30, 26);
  popMatrix();

  noFill();
  ellipse(width/2, height/2, 34.64, 34.64);

  pushMatrix();
  translate(width/2, height/2);
  rotate(frameCount*radians(-90) /50);
  scale(2);
  translate(0, -9);
  noFill();
  triangle (-30, 26, 0, -26, 30, 26);
  popMatrix();

  noFill();
  ellipse(width/2, height/2, 34.64*2, 34.64*2);

  pushMatrix();
  translate(width/2, height/2);
  rotate(frameCount*radians(90) /50);
  scale(4);
  translate(0, -9);
  noFill();
  triangle (-30, 26, 0, -26, 30, 26);
  popMatrix();

  noFill();
  ellipse(width/2, height/2, 34.64*4, 34.64*4);

  pushMatrix();
  translate(width/2, height/2);
  rotate(frameCount*radians(-90) /50);
  scale(8);
  translate(0, -9);
  noFill();
  triangle (-30, 26, 0, -26, 30, 26);
  popMatrix();

  noFill();
  ellipse(width/2, height/2, 34.64*8, 34.64*8);

  pushMatrix();
  translate(width/2, height/2);
  rotate(frameCount*radians(90) /50);
  scale(16);
  translate(0, -9);
  noFill();
  triangle (-30, 26, 0, -26, 30, 26);
  popMatrix();

  noFill();
  ellipse(width/2, height/2, 34.64*16, 34.64*16);

  pushMatrix();
  translate(width/2, height/2);
  rotate(frameCount*radians(-90) /50);
  scale(32);
  translate(0, -9);
  noFill();
  triangle (-30, 26, 0, -26, 30, 26);
  popMatrix();

  noFill();
  ellipse(width/2, height/2, 34.64*32, 34.64*32);
}

Answers

  • Answer ✓

    That's pretty :-) I think we'll have to do the scaling manually if we don't want the strokeWeight along the the ride, but that shouldn't be too painful if we parametrize a bit:

    void setup() {
      size(1280, 720);
    }
    
    void myShapes(float nScale, float nDirection)
    {
      pushMatrix();
      translate(width/2, height/2);
      rotate(nDirection*frameCount*radians(90) /50);
      translate(0, -9 * nScale);
      noFill();
      triangle (-30 * nScale, 26 * nScale, 0 * nScale, -26 * nScale, 30 * nScale, 26 * nScale);
      popMatrix();
    
      ellipse(width/2, height/2, 34.64*nScale, 34.64*nScale);
    }
    
    void draw() {
      background(255);
    
      myShapes(1,   1);
      myShapes(2,  -1);
      myShapes(4,   1);
      myShapes(8,  -1);
      myShapes(16,  1);
      myShapes(32, -1);
    }
    
  • Ah that's perfect! Sorry it's taken me so long to reply, this was just what I was looking for! Thank you :)

Sign In or Register to comment.