Changed around line 1
+ document.addEventListener('DOMContentLoaded', function() {
+ const canvas = document.getElementById('dotProductCanvas');
+ const ctx = canvas.getContext('2d');
+ const inputs = ['vecAx', 'vecAy', 'vecBx', 'vecBy'].map(id => document.getElementById(id));
+
+ function updateCalculations() {
+ const [ax, ay, bx, by] = inputs.map(input => parseFloat(input.value) || 0);
+ const dotProduct = ax * bx + ay * by;
+ const magnitudeA = Math.sqrt(ax * ax + ay * ay);
+ const magnitudeB = Math.sqrt(bx * bx + by * by);
+ const angle = Math.acos(dotProduct / (magnitudeA * magnitudeB)) * (180 / Math.PI);
+
+ document.getElementById('dotProductResult').textContent = dotProduct.toFixed(2);
+ document.getElementById('angleResult').textContent = `${angle.toFixed(1)}°`;
+
+ drawVectors(ax, ay, bx, by);
+ }
+
+ function drawVectors(ax, ay, bx, by) {
+ const scale = 50;
+ const centerX = canvas.width / 2;
+ const centerY = canvas.height / 2;
+
+ ctx.clearRect(0, 0, canvas.width, canvas.height);
+
+ // Draw grid
+ ctx.strokeStyle = '#eee';
+ ctx.lineWidth = 1;
+ for(let i = 0; i < canvas.width; i += scale) {
+ ctx.beginPath();
+ ctx.moveTo(i, 0);
+ ctx.lineTo(i, canvas.height);
+ ctx.stroke();
+ }
+ for(let i = 0; i < canvas.height; i += scale) {
+ ctx.beginPath();
+ ctx.moveTo(0, i);
+ ctx.lineTo(canvas.width, i);
+ ctx.stroke();
+ }
+
+ // Draw axes
+ ctx.strokeStyle = '#666';
+ ctx.lineWidth = 2;
+ ctx.beginPath();
+ ctx.moveTo(0, centerY);
+ ctx.lineTo(canvas.width, centerY);
+ ctx.moveTo(centerX, 0);
+ ctx.lineTo(centerX, canvas.height);
+ ctx.stroke();
+
+ // Draw vectors
+ ctx.lineWidth = 3;
+
+ // Vector A
+ ctx.strokeStyle = '#3498db';
+ ctx.beginPath();
+ ctx.moveTo(centerX, centerY);
+ ctx.lineTo(centerX + ax * scale, centerY - ay * scale);
+ ctx.stroke();
+
+ // Vector B
+ ctx.strokeStyle = '#e74c3c';
+ ctx.beginPath();
+ ctx.moveTo(centerX, centerY);
+ ctx.lineTo(centerX + bx * scale, centerY - by * scale);
+ ctx.stroke();
+
+ // Draw arrowheads
+ drawArrowhead(centerX, centerY, ax, ay, scale, '#3498db');
+ drawArrowhead(centerX, centerY, bx, by, scale, '#e74c3c');
+ }
+
+ function drawArrowhead(x, y, dx, dy, scale, color) {
+ const angle = Math.atan2(-dy, dx);
+ const length = 10;
+
+ ctx.strokeStyle = color;
+ ctx.fillStyle = color;
+
+ ctx.beginPath();
+ ctx.moveTo(x + dx * scale, y - dy * scale);
+ ctx.lineTo(x + dx * scale - length * Math.cos(angle - Math.PI/6),
+ y - dy * scale + length * Math.sin(angle - Math.PI/6));
+ ctx.lineTo(x + dx * scale - length * Math.cos(angle + Math.PI/6),
+ y - dy * scale + length * Math.sin(angle + Math.PI/6));
+ ctx.closePath();
+ ctx.fill();
+ }
+
+ inputs.forEach(input => input.addEventListener('input', updateCalculations));
+ updateCalculations();
+ });