Skip to content

Commit

Permalink
clip pipes to tile polygons
Browse files Browse the repository at this point in the history
this addresses gereleth#91 but it has its own glitches
  • Loading branch information
gordonwoodhull committed Aug 26, 2023
1 parent 8b07771 commit 9a8a9f2
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 10 deletions.
17 changes: 17 additions & 0 deletions src/lib/puzzle/ClipPolygon.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<script>
/** @type {Number} i*/
export let i;
/**
* @type {import('$lib/puzzle/game').PipesGame} game
*/
export let game;
export let cx = 0;
export let cy = 0;
</script>

<clipPath id="clip-path-{i}">
<path
d={game.grid.getClipPath(i)}
/>
</clipPath>
11 changes: 11 additions & 0 deletions src/lib/puzzle/Puzzle.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { settings } from '$lib/stores';
import { controls } from '$lib/puzzle/controls';
import Tile from '$lib/puzzle/Tile.svelte';
import ClipPolygon from '$lib/puzzle/ClipPolygon.svelte';
import { onMount, onDestroy, createEventDispatcher, tick } from 'svelte';
import { PipesGame } from '$lib/puzzle/game';
import { Solver } from './solver';
Expand Down Expand Up @@ -330,6 +331,16 @@
on:contextmenu|preventDefault={() => {}}
on:save={save.soon}
>
<defs>
{#each $visibleTiles as visibleTile, i (visibleTile.key)}
<ClipPolygon
i={visibleTile.index}
{game}
cx={visibleTile.x}
cy={visibleTile.y}
/>
{/each}
</defs>
{#each $visibleTiles as visibleTile, i (visibleTile.key)}
<Tile
i={visibleTile.index}
Expand Down
1 change: 1 addition & 0 deletions src/lib/puzzle/Tile.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
<g
class="pipe"
style="transform: {tile_transform} rotate({game.grid.getAngle($state.rotations, i)}rad)"
clip-path="url(#clip-path-{i})"
>
<!-- Pipe outline -->
<path
Expand Down
9 changes: 9 additions & 0 deletions src/lib/puzzle/grids/abstractgrid.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,15 @@ export class AbstractGrid {
return this.polygon_at(index).contour_path;
}

/**
* Clipping path for svg pipes
* @param {Number} index
* @returns {String}
*/
getClipPath(index) {
return this.polygon_at(index).clip_path;
}

/**
* Pipes lines path
* @param {Number} tile
Expand Down
26 changes: 16 additions & 10 deletions src/lib/puzzle/grids/polygonutils.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,8 @@ export class RegularPolygonTile {
}

// draw tile contour
let angle = angle_offset - this.angle_unit / 2;
const r = this.radius_out - border_width;
this.contour_path = `m ${r * Math.cos(angle)} ${-r * Math.sin(angle)}`;
for (let i = 1; i <= this.num_directions; i++) {
angle += this.angle_unit;
this.contour_path += ` L ${r * Math.cos(angle)} ${-r * Math.sin(angle)}`;
}
this.contour_path += ' z';
this.contour_path = this.polygon_path(this.radius_out - border_width);
this.clip_path = this.polygon_path(this.radius_out + border_width/2);

// caches for frequently recomputed values
this.cache = {
Expand All @@ -111,6 +105,17 @@ export class RegularPolygonTile {
};
}

polygon_path(r) {
let angle = this.angle_offset - this.angle_unit / 2;
let path = `m ${r * Math.cos(angle)} ${-r * Math.sin(angle)}`;
for (let i = 1; i <= this.num_directions; i++) {
angle += this.angle_unit;
path += ` L ${r * Math.cos(angle)} ${-r * Math.sin(angle)}`;
}
path += ' z';
return path;
}

/**
* Return mininum equivalent number of rotations
* In case of 4 directions rotating +3 is the same as rotating -1
Expand Down Expand Up @@ -189,8 +194,9 @@ export class RegularPolygonTile {
this.directions.forEach((direction, index) => {
if ((direction & tile) > 0) {
const angle = this.angle_offset + this.angle_unit * index;
const dx = this.radius_in * Math.cos(angle);
const dy = this.radius_in * Math.sin(angle);
const radius = this.radius_in * 1.2;
const dx = radius * Math.cos(angle);
const dy = radius * Math.sin(angle);
path += ` l ${dx} ${-dy} L 0 0`;
}
});
Expand Down

0 comments on commit 9a8a9f2

Please # to comment.