Skip to content

Commit 2f17911

Browse files
Rounding error was the problem
closes #7
1 parent b13dec1 commit 2f17911

File tree

4 files changed

+22
-23
lines changed

4 files changed

+22
-23
lines changed

Diff for: schemascii/utils.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ def colinear(*points: complex) -> bool:
2727
return len(set(phase(p - points[0]) for p in points[1:])) == 1
2828

2929

30+
def force_int(p: complex) -> complex:
31+
"Force the coordinates of the complex number to line on the integer grid."
32+
return complex(round(p.real), round(p.imag))
33+
34+
3035
def sharpness_score(points: list[complex]) -> float:
3136
"""Returns a number indicating how twisty the line is -- higher means
3237
the corners are sharper."""
@@ -95,7 +100,7 @@ def iterate_line(p1: complex, p2: complex, step: float = 1.0):
95100
vec = p2 - p1
96101
point = p1
97102
while abs(vec) > abs(point - p1):
98-
yield point
103+
yield force_int(point)
99104
point += rect(step, phase(vec))
100105
yield point
101106

Diff for: schemascii/wires.py

+10-12
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from cmath import phase, rect
22
from math import pi
33
from .grid import Grid
4-
from .utils import iterate_line, bunch_o_lines, XML, find_dots
4+
from .utils import force_int, iterate_line, bunch_o_lines, XML, find_dots
55

66
# cSpell:ignore dydx
77

@@ -77,18 +77,16 @@ def blank_wire(grid: Grid, p1: complex, p2: complex):
7777
"Blank out the wire from p1 to p2."
7878
# Crazy math!!
7979
way = int(phase(p1 - p2) / pi % 1.0 * 2)
80-
side = rect(1, phase(p1 - p2) + pi / 2)
80+
side = force_int(rect(1, phase(p1 - p2) + pi / 2))
81+
# way: 0: Horizontal, 1: Vertical
82+
# Don't mask out wire crosses
83+
cross = ["|()", "-"][way]
84+
swap = "|-"[way]
8185
for px in iterate_line(p1, p2):
82-
old = grid.get(px)
83-
# way: 0: Horizontal, 1: Vertical
84-
# Don't mask out wire crosses
85-
keep = ["|()", "-"][way]
86-
swap = "|-"[way]
87-
if old not in keep:
88-
if grid.get(px + side) in keep and grid.get(px - side) in keep:
89-
grid.setmask(px, swap)
90-
else:
91-
grid.setmask(px)
86+
if grid.get(px + side) in cross and grid.get(px - side) in cross:
87+
grid.setmask(px, swap)
88+
else:
89+
grid.setmask(px)
9290

9391

9492
def next_wire(grid: Grid, **options) -> str | None:

Diff for: schemascii_example.css

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ svg.schemascii .wire polyline {
66
stroke: var(--sch-color, blue);
77
stroke-width: 2;
88
stroke-linecap: round;
9+
stroke-linejoin: round;
910
transition-duration: 0.2s;
1011
fill: transparent;
1112
}

Diff for: test_data/test1.txt

+5-10
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
1-
!padding=30!
2-
G3----------L1------G4
3-
4-
G2------L2###########-------G1
5-
L1:220u
6-
L2:330u
7-
G1:earth
8-
G2:chassis
9-
G3:signal
10-
G4:common
1+
---------------------*
2+
|
3+
---------------------|------
4+
|
5+
---------------------*

0 commit comments

Comments
 (0)