Rotate Rect
Adapted from Rotate Rect, created by Fabian Morón Zirfas.
The user-defined rotate(x, y, width, height, angle) function draws a rectangle rotated around the canvas center. Its x and y describe the rectangle's center relative to that pivot, while angle is measured in degrees. Sine and cosine calculate the four corners before quad draws the shape.
A call at 13 degrees places a large white rectangle against a black ground. Its cropped edges make the tilted form read as a figure passing through the canvas. Change the angle in the call to explore the rotation.
// ABOUTME: Calculates rectangle corners with a user-defined rotation function.
// ABOUTME: Demonstrates figure and ground through one tilted rectangle crossing a dark Canvas.
// Adapted from Fabian Morón Zirfas's Rotate Rect:
// https://github.com/ff6347/gestalten-in-code/tree/e8b8f8446c13c01494ccfd1e92ce2bb1638daf66/form/2d/processing/rotate_rect
func rotate(x, y, width, height, angle) {
let left = x - width / 2;
let right = x + width / 2;
let top = y - height / 2;
let bottom = y + height / 2;
let cosine = cos(angle);
let sine = sin(angle);
let centerX = WIDTH / 2;
let centerY = HEIGHT / 2;
quad(
centerX + cosine * left - sine * top,
centerY + sine * left + cosine * top,
centerX + cosine * right - sine * top,
centerY + sine * right + cosine * top,
centerX + cosine * right - sine * bottom,
centerY + sine * right + cosine * bottom,
centerX + cosine * left - sine * bottom,
centerY + sine * left + cosine * bottom
);
return;
}
background("#000000");
noStroke();
fill("#ffffff");
rotate(-WIDTH / 3, 0, WIDTH, HEIGHT, 13);