# Move around the perimeter of a rectangle (using maths)

Hello, im here, learning maths, or trying… so, i want to move around the perimeter of a rectangle using trigonometrics function like sin or cos, it is possible? In the example i using the regular if else stament. I know that with a sin and cos i can describe a circunference. But how can i get the rectangle? I asked chatGPT but with no result… Thanks

``````let x, y; // position of the point
let step = 0; // current step along the perimeter
let rectWidth = 200;
let rectHeight = 100;

function setup() {
createCanvas(400, 400);
x = -rectWidth/2; // start at the top left corner of the rectangle
y = -rectHeight/2;
}

function draw() {
background(220);

// calculate the position of the point based on the current step
if (step < rectWidth) {
x += 1;
} else if (step < rectWidth + rectHeight) {
y += 1;
} else if (step < 2 * rectWidth + rectHeight) {
x -= 1;
} else if (step < 2 * rectWidth + 2 * rectHeight) {
y -= 1;
}

// move the origin to the center of the canvas
translate(width / 2, height / 2);

// draw the rectangle
noFill();
stroke(0);
rect(-rectWidth/2, -rectHeight/2, rectWidth, rectHeight);

// draw the point
fill(255, 0, 0);
noStroke();
circle(x, y, 10);

// update the step
step += 1;
if (step >= 2 * rectWidth + 2 * rectHeight) {
step = 0;
x = -rectWidth/2;
y = -rectHeight/2;
}
}
``````