Here is an adjusted version of the JavaScript code that resizes the resulting matrix to fit in the grid:
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
const DOT_SIZE = 5;
const GRID_SIZE = 50;
const DOT_SPACING = 10;
const NUM_DOTS = GRID_SIZE * GRID_SIZE;
const MAX_SIZE = GRID_SIZE * DOT_SPACING;
const dots = [];
function init() {
canvas.width = MAX_SIZE;
canvas.height = MAX_SIZE;
for (let i = 0; i < NUM_DOTS; i++) {
dots.push({x: i % GRID_SIZE * DOT_SPACING, y: Math.floor(i / GRID_SIZE) * DOT_SPACING});
}
drawDots();
}
function resetDots() {
for (let i = 0; i < NUM_DOTS; i++) {
dots[i] = {x: i % GRID_SIZE * DOT_SPACING, y: Math.floor(i / GRID_SIZE) * DOT_SPACING};
}
drawDots();
}
function drawDots() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = 'black';
for (let i = 0; i < NUM_DOTS; i++) {
ctx.beginPath();
ctx.arc(dots[i].x, dots[i].y, DOT_SIZE, 0, Math.PI * 2);
ctx.fill();
}
}
function transform() {
const matrixString = document.getElementById('matrix').value;
const matrixArray = matrixString.trim().split(/\s+/).map(Number);
if (matrixArray.length !== 6 || matrixArray.some(isNaN)) {
alert('Invalid transformation matrix.');
return;
}
const a = matrixArray[0];
const b = matrixArray[1];
const c = matrixArray[2];
const d = matrixArray[3];
const e = matrixArray[4];
const f = matrixArray[5];
// Initialize min and max for x and y to canvas limits
let minX = MAX_SIZE;
let maxX = -MAX_SIZE;
let minY = MAX_SIZE;
let maxY = -MAX_SIZE;
for (let i = 0; i < NUM_DOTS; i++) {
const x = dots[i].x;
const y = dots[i].y;
const newX = a * x + c * y + e;
const newY = b * x + d * y + f;
dots[i].x = newX;
dots[i].y = newY;
// Update min and max after transformation
if(newX < minX){
minX = newX;
}
if(newX > maxX){
maxX = newX;
}
if(newY < minY){
minY = newY;
}
if(newY > maxY){
maxY = newY;
}
}
// Calculate how much to reduce the X and Y so that it fits in the grids
const xFactor = (maxX - minX) / MAX_SIZE;
const yFactor = (maxY - minY) / MAX_SIZE;
for (let i = 0; i < NUM_DOTS; i++) {
const x = dots[i].x;
const y = dots[i].y;
if(xFactor === 0){
// if xFactor is zero that means all X values are the same
dots[i].x = DOT_SPACING;
} else {
dots[i].x = (x - minX) / xFactor;
}
if(yFactor === 0){
// if yFactor is zero that means all Y values are the same
dots[i].x = DOT_SPACING;
} else {
dots[i].y = (y - minY) / yFactor;
}
}
drawDots();
}
init();
I've also added a reset function that could be added to a button in the HTML.
Here is the modified HTML with the added button:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Dot Transformation</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1>Dot Transformation</h1>
<canvas id="canvas"></canvas>
<form>
<label for="matrix">Transformation Matrix:</label>
<textarea id="matrix" name="matrix" rows="4" cols="20"></textarea>
<button type="button" onclick="transform()">Transform</button>
<button type="button" onclick="resetDots()">Reset</button>
</form>
<script src="script.js"></script>
</body>
</html>
Keep in mind resizing the grid means you will only see relative value changes.
Does this fix your problem?