diff --git a/line-segment-intersection/src/App.svelte b/line-segment-intersection/src/App.svelte index e0664a9..a0ad125 100644 --- a/line-segment-intersection/src/App.svelte +++ b/line-segment-intersection/src/App.svelte @@ -50,8 +50,15 @@ const width = Math.abs(corner2.x - corner1.x); const height = Math.abs(corner2.y - corner1.y); + ctx.beginPath(); + ctx.roundRect(x, y, width, height, 1); ctx.fillStyle = color; - ctx.fillRect(x, y, width, height); + ctx.fill(); + + // Add border + ctx.strokeStyle = color.replace('0.3', '0.8'); // Make border more opaque + ctx.lineWidth = 2; + ctx.stroke(); } function handleMouseMove(event) { @@ -59,7 +66,11 @@ const mouseX = event.clientX - rect.left; const mouseY = event.clientY - rect.top; - userPoint = toCartesianCoords(mouseX, mouseY); + // Add half grid unit offset for snapping to closest grid intersection + userPoint = { + x: Math.floor((mouseX + GRID_SIZE / 2) / GRID_SIZE), + y: Math.floor((HEIGHT - mouseY + GRID_SIZE / 2) / GRID_SIZE) + }; } function toCanvasCoords(x, y) { @@ -199,22 +210,22 @@

Rectangle Areas

-

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

+

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

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)} x {blueHeight.toFixed(2)} = {blueArea.toFixed(2)}

+

Width = |{blueRect.x1} - {userPoint.x}| = {blueWidth}

+

Height = |{blueRect.y1} - {userPoint.y}| = {blueHeight}

+

Area = {blueWidth} x {blueHeight} = {blueArea}

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)} x {redHeight.toFixed(2)} = {redArea.toFixed(2)}

+

Width = |{redRect.x1} - {userPoint.x}| = {redWidth}

+

Height = |{redRect.y1} - {userPoint.y}| = {redHeight}

+

Area = {redWidth} x {redHeight} = {redArea}