Create interactive dilation animations using p5.js where students explore dilations with adjustable scale factors.
Use this skill when creating animations where students:
Perfect for:
Not suitable for:
Uses p5.js for the interactive coordinate plane because:
// ==========================================
// CONFIGURATION - Easily modifiable
// ==========================================
// Polygon vertices (any shape)
let points = [
{ x: 10, y: 10, label: 'P' },
{ x: 8, y: 8, label: 'Q' },
{ x: 12, y: 6, label: 'R' },
{ x: 14, y: 8, label: 'S' }
];
// Center of dilation
let center = { x: 10, y: 8, label: 'O' };
// Scale factors available
let scaleFactors = [
{ value: 1/3, label: '1/3' },
{ value: 1/2, label: '1/2' },
{ value: 1, label: '1' },
{ value: 2, label: '2' },
{ value: 3, label: '3' },
{ value: 4, label: '4' }
];
// Current scale factor index
let scaleIndex = 3; // Start at scale factor 2
// Toggle for showing dilation rays
let showRays = true;
// Toggle for showing grid and axes
let showGrid = true;
// Axis ranges
let xMin = 0, xMax = 20;
let yMin = 0, yMax = 15;
// Colors
let originalColor = [59, 130, 246]; // Blue
let dilatedColor = [239, 68, 68]; // Red
let gridColor = 220;
let axisColor = 100;
function coordToPixel(x, y, plotWidth, plotHeight) {
let px = padding.left + ((x - xMin) / (xMax - xMin)) * plotWidth;
let py = padding.top + ((yMax - y) / (yMax - yMin)) * plotHeight;
return { x: px, y: py };
}
// Calculate dilated points
let dilatedPoints = points.map(p => ({
x: center.x + (p.x - center.x) * scaleFactor,
y: center.y + (p.y - center.y) * scaleFactor,
label: p.label + "'"
}));
// Draw dilation rays (from center, extending outward)
if (showRays) {
stroke(0);
strokeWeight(2);
drawingContext.setLineDash([4, 4]);
for (let i = 0; i < points.length; i++) {
let centerPixelPos = coordToPixel(center.x, center.y, plotWidth, plotHeight);
let pointPixel = coordToPixel(points[i].x, points[i].y, plotWidth, plotHeight);
// Calculate direction vector
let dx = pointPixel.x - centerPixelPos.x;
let dy = pointPixel.y - centerPixelPos.y;
// Ray starts at center and extends far in the direction of the point
let scale = 1000;
let x2 = centerPixelPos.x + dx * scale;
let y2 = centerPixelPos.y + dy * scale;
line(centerPixelPos.x, centerPixelPos.y, x2, y2);
}
drawingContext.setLineDash([]);
}
let points = [
{ x: 2, y: 2, label: 'A' },
{ x: 4, y: 2, label: 'B' },
{ x: 4, y: 4, label: 'C' },
{ x: 2, y: 4, label: 'D' }
];
let center = { x: 0, y: 0, label: 'O' };
let scaleFactors = [
{ value: 1/2, label: '1/2' },
{ value: 1, label: '1' },
{ value: 2, label: '2' },
{ value: 3, label: '3' }
];
let points = [
{ x: 2, y: 8, label: 'A' },
{ x: 2, y: 4, label: 'B' },
{ x: 6, y: 4, label: 'C' }
];
let center = { x: 6, y: 4, label: 'C' }; // Center at vertex C
let scaleFactors = [
{ value: 1/2, label: '1/2' },
{ value: 1, label: '1' },
{ value: 2, label: '2' },
{ value: 3, label: '3' }
];
let points = [
{ x: 0, y: 0, label: 'A' },
{ x: 2, y: 2, label: 'B' },
{ x: 6, y: 1, label: 'C' },
{ x: 4, y: -1, label: 'D' }
];
let center = { x: 4, y: -1, label: 'O' };
// Adjust axis ranges to accommodate negative values
let xMin = -5, xMax = 25;
let yMin = -10, yMax = 15;
Modify the points array to create any polygon. Points should be in order to form a closed shape.
Set center to any point. Common choices:
Customize the scaleFactors array with any positive values:
Adjust xMin, xMax, yMin, yMax to fit your shape and dilations.
Customize colors for:
originalColor: Color of the original polygondilatedColor: Color of the dilated polygongridColor: Color of grid linesaxisColor: Color of coordinate axesscaleIndex: Starting scale factor (index in scaleFactors array)showRays: Whether to show dilation rays by defaultshowGrid: Whether to show grid by defaultpoints array for target polygoncenter coordinates and labelscaleFactors arrayxMin, xMax, yMin, yMax)Understanding Scale Factors:
Similarity: Dilations preserve shape (angles) but change size (side lengths)
Center Effects: Explore how center location affects the dilated figure's position
Coordinate Relationships: See how each coordinate changes based on center and scale factor
Base implementation: src/app/animations/examples/geometry/dilation.ts
When asked to create a dilation animation: