So let’s go over the steps:
- You draw a rectangle at corners (c1, c2), with dimensions (w1,h1) (width and height respectively)
- The center of that same rectangle is the point (c1 + w1/2 , c2 + h1/2)
- Let’s say now that you want the inner rectangle to have dimensions w2, h2
- To find the corner and maintain the same center you need to go -w2/2, -h2/2
Thus the corner of the rectangle would need to be
(c1 + w1/2 -w2/2 , c2 + h1/2-h2/2)
So the code became:
void setup() {
size(600, 600);
}
void draw() {
background(240);
noStroke();
// draw a (blue) rectangle
int w = 200;
int h = 200;
int c1 = 0;
int c2 = 0;
fill(0, 0, 255);
rect(c1, c2, w, h, 10, 10, 10, 10);
// from now on - no Fill
noFill();
// draw a second smaller rectangle
// so let's assume s in ]0, 1[
float s = 0.75;
//SO find the dimensions of width and height
float w2 = s * w;
float h2 = s * h;
stroke(255);
strokeWeight(3);
// w - s*s is the x-difference length between both rect
float tx = c1 + w/2 - w2/2;
float ty = c2 + h/2 - h2/2;
//scale(s);
translate(tx, ty);
rect(0, 0, w2, h2, 10, 10, 10, 10);
}
Result:
Best regards!