chatgpt-4o-latest-20250326· OpenAI
LMArena Human Preference 140K
Write a single page webpage to show a linear transformation in 2d given 4 inputs (the…
- User
Write a single page webpage to show a linear transformation in 2d given 4 inputs (the matrix)
- Assistant
Sure! Below is a simple single-page HTML file that allows a user to input a 2×2 matrix and visualizes the linear transformation of the standard basis vectors in 2D using HTML5 Canvas and JavaScript.
📌 Features:
- Input fields for a 2×2 matrix (a, b, c, d).
- A canvas that shows:
- The original basis vectors (î = [1, 0], ĵ = [0, 1]) in gray.
- The transformed vectors (A·î and A·ĵ) in red and blue.
You can copy and paste this into an .html file and open it in a browser.
✅ Single Page Web App:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>2D Linear Transformation Visualizer</title> <style> body { font-family: Arial, sans-serif; text-align: center; margin: 20px; } canvas { border: 1px solid #ccc; margin-top: 20px; } input { width: 50px; text-align: center; } .matrix { display: inline-block; margin: 10px; } button { margin-top: 10px; } </style> </head> <body> <h2>2D Linear Transformation Visualizer</h2> <p>Enter a 2×2 matrix:</p> <div class="matrix"> <input type="number" id="a" value="1"> <input type="number" id="b" value="0"><br> <input type="number" id="c" value="0"> <input type="number" id="d" value="1"> </div> <br> <button onclick="draw()">Apply Transformation</button> <br> <canvas id="canvas" width="400" height="400"></canvas> <script> const canvas = document.getElementById('canvas'); const ctx = canvas.getContext('2d'); const width = canvas.width; const height = canvas.height; const origin = { x: width / 2, y: height / 2 }; const scale = 40; // pixels per unit function drawArrow(x1, y1, x2, y2, color) { ctx.beginPath(); ctx.moveTo(x1, y1); ctx.lineTo(x2, y2); ctx.strokeStyle = color; ctx.lineWidth = 2; ctx.stroke(); // Draw arrowhead const angle = Math.atan2(y2 - y1, x2 - x1); const headlen = 10; ctx.beginPath(); ctx.moveTo(x2, y2); ctx.lineTo(x2 - headlen * Math.cos(angle - Math.PI / 6), y2 - headlen * Math.sin(angle - Math.PI / 6)); ctx.lineTo(x2 - headlen * Math.cos(angle + Math.PI / 6), y2 - headlen * Math.sin(angle + Math.PI / 6)); ctx.lineTo(x2, y2); ctx.fillStyle = color; ctx.fill(); } function drawGrid() { ctx.clearRect(0, 0, width, height); ctx.strokeStyle = '#eee'; ctx.lineWidth = 1; for (let x = 0; x <= width; x += scale) { ctx.beginPath(); ctx.moveTo(x, 0); ctx.lineTo(x, height); ctx.stroke(); } for (let y = 0; y <= height; y += scale) { ctx.beginPath(); ctx.moveTo(0, y); ctx.lineTo(width, y); ctx.stroke(); } // Draw axes ctx.beginPath(); ctx.moveTo(0, origin.y); ctx.lineTo(width, origin.y); ctx.moveTo(origin.x, 0); ctx.lineTo(origin.x, height); ctx.strokeStyle = '#aaa'; ctx.lineWidth = 1.5; ctx.stroke(); } function draw() { const a = parseFloat(document.getElementById('a').value); const b = parseFloat(document.getElementById('b').value); const c = parseFloat(document.getElementById('c').value); const d = parseFloat(document.getElementById('d').value); drawGrid(); // Draw original basis vectors drawArrow(origin.x, origin.y, origin.x + scale, origin.y, '#888'); // î drawArrow(origin.x, origin.y, origin.x, origin.y - scale, '#888'); // ĵ // Transformed vectors const iTrans = { x: a, y: -c }; // Note: y is inverted for canvas const jTrans = { x: b, y: -d }; drawArrow(origin.x, origin.y, origin.x + iTrans.x * scale, origin.y + iTrans.y * scale, 'red'); drawArrow(origin.x, origin.y, origin.x + jTrans.x * scale, origin.y + jTrans.y * scale, 'blue'); } draw(); // Initial draw </script> </body> </html>🧠 How It Works:
- The canvas is centered at (200, 200) and scaled so that 1 unit = 40 pixels.
- The user inputs a 2×2 matrix:
[ a b ] [ c d ] - The standard basis vectors [1, 0] and [0, 1] are transformed using the matrix:
- A·[1, 0] = [a, c]
- A·[0, 1] = [b, d]
- These transformed vectors are drawn in red and blue.
Let me know if you'd like to add more features like transforming arbitrary vectors, animation, or eigenvectors!
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.