diff --git a/line-segment-intersection/src/App.svelte b/line-segment-intersection/src/App.svelte index ce0c724..5c20dbd 100644 --- a/line-segment-intersection/src/App.svelte +++ b/line-segment-intersection/src/App.svelte @@ -4,11 +4,24 @@ let canvas; let ctx; const GRID_SIZE = 40; // pixels per grid unit - const WIDTH = 800; - const HEIGHT = 640; + const WIDTH = 320; + const HEIGHT = 320; let userPoint = { x: 5, y: 2 }; + // Rectangle coordinates + const blueRect = { x1: 6, y1: 1 }; // Blue rectangle corner + const redRect = { x1: 1, y1: 7 }; // Red rectangle corner + + // Calculate rectangle areas + $: blueWidth = Math.abs(blueRect.x1 - userPoint.x); + $: blueHeight = Math.abs(blueRect.y1 - userPoint.y); + $: blueArea = blueWidth * blueHeight; + + $: redWidth = Math.abs(redRect.x1 - userPoint.x); + $: redHeight = Math.abs(redRect.y1 - userPoint.y); + $: redArea = redWidth * redHeight; + $: if (ctx && userPoint) { redraw(); } @@ -21,10 +34,10 @@ function redraw() { drawGrid(); drawRectangle(userPoint.x, userPoint.y, 6, 1, 'rgba(100, 108, 255, 0.3)'); - drawRectangle(userPoint.x, userPoint.y, 1, 6, 'rgba(255, 0, 0, 0.3)'); + drawRectangle(userPoint.x, userPoint.y, 1, 7, 'rgba(255, 0, 0, 0.3)'); drawLineSegment(1, 1, 6, 7, '#ff3e00'); drawLabel('A', 1, 1); - drawLabel('B', 6, 7); + drawLabel('C', 6, 7); drawUserPoint(); } @@ -50,20 +63,16 @@ } function toCanvasCoords(x, y) { - const centerX = WIDTH / 2; - const centerY = HEIGHT / 2; return { - x: centerX + x * GRID_SIZE, - y: centerY - y * GRID_SIZE + x: x * GRID_SIZE, + y: HEIGHT - y * GRID_SIZE }; } function toCartesianCoords(canvasX, canvasY) { - const centerX = WIDTH / 2; - const centerY = HEIGHT / 2; return { - x: (canvasX - centerX) / GRID_SIZE, - y: (centerY - canvasY) / GRID_SIZE + x: canvasX / GRID_SIZE, + y: (HEIGHT - canvasY) / GRID_SIZE }; } @@ -112,7 +121,7 @@ ctx.font = 'bold 16px monospace'; ctx.textAlign = 'center'; ctx.textBaseline = 'bottom'; - ctx.fillText('C', coords.x, coords.y - 12); + ctx.fillText('B', coords.x, coords.y - 12); } function drawGrid() { @@ -120,9 +129,6 @@ ctx.fillStyle = '#1a1a1a'; ctx.fillRect(0, 0, WIDTH, HEIGHT); - const centerX = WIDTH / 2; - const centerY = HEIGHT / 2; - // Draw grid lines ctx.strokeStyle = '#333'; ctx.lineWidth = 1; @@ -143,20 +149,20 @@ ctx.stroke(); } - // Draw axes + // Draw axes (left and bottom edges) ctx.strokeStyle = '#666'; ctx.lineWidth = 2; - // Y-axis + // Y-axis (left edge) ctx.beginPath(); - ctx.moveTo(centerX, 0); - ctx.lineTo(centerX, HEIGHT); + ctx.moveTo(0, 0); + ctx.lineTo(0, HEIGHT); ctx.stroke(); - // X-axis + // X-axis (bottom edge) ctx.beginPath(); - ctx.moveTo(0, centerY); - ctx.lineTo(WIDTH, centerY); + ctx.moveTo(0, HEIGHT); + ctx.lineTo(WIDTH, HEIGHT); ctx.stroke(); // Draw axis labels @@ -166,23 +172,22 @@ // X-axis labels for (let x = GRID_SIZE; x <= WIDTH; x += GRID_SIZE) { - const value = Math.round((x - centerX) / GRID_SIZE); - if (value !== 0) { - ctx.fillText(value, x, centerY + 20); - } + const value = Math.round(x / GRID_SIZE); + ctx.fillText(value, x, HEIGHT - 5); } // Y-axis labels - ctx.textAlign = 'right'; - for (let y = GRID_SIZE; y <= HEIGHT; y += GRID_SIZE) { - const value = Math.round((centerY - y) / GRID_SIZE); - if (value !== 0) { - ctx.fillText(value, centerX - 10, y + 5); + ctx.textAlign = 'left'; + for (let y = 0; y < HEIGHT; y += GRID_SIZE) { + const value = Math.round((HEIGHT - y) / GRID_SIZE); + if (value > 0) { + ctx.fillText(value, 5, y + 5); } } // Origin label - ctx.fillText('0', centerX - 10, centerY + 20); + ctx.textAlign = 'left'; + ctx.fillText('0', 5, HEIGHT - 5); } @@ -191,6 +196,27 @@
+ +
+

Rectangle Areas

+

Point B: ({userPoint.x.toFixed(2)}, {userPoint.y.toFixed(2)})

+ +
redArea}> +

Blue Rectangle

+

Corner: ({blueRect.x1}, {blueRect.y1})

+

Width = |{blueRect.x1} - {userPoint.x.toFixed(2)}| = {blueWidth.toFixed(2)}

+

Height = |{blueRect.y1} - {userPoint.y.toFixed(2)}| = {blueHeight.toFixed(2)}

+

Area = {blueWidth.toFixed(2)} × {blueHeight.toFixed(2)} = {blueArea.toFixed(2)}

+
+ +
blueArea}> +

Red Rectangle

+

Corner: ({redRect.x1}, {redRect.y1})

+

Width = |{redRect.x1} - {userPoint.x.toFixed(2)}| = {redWidth.toFixed(2)}

+

Height = |{redRect.y1} - {userPoint.y.toFixed(2)}| = {redHeight.toFixed(2)}

+

Area = {redWidth.toFixed(2)} × {redHeight.toFixed(2)} = {redArea.toFixed(2)}

+
+