Render gauge needles pixel-exact
The needle is the display's own routine, measured on 112 needles in the Editor simulator: tip from quadrant-reduced sin/cos, truncated; line from tip to centre with a biased DDA; wider needles stamp wid x wid squares along a line shortened by wid - 1. Length follows the height. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
9b50eea03f
commit
f0bcc608b1
7 files changed
+67
-14
No files matched your search
+39
-7
@@ -51,6 +51,41 @@ def disc_rows(r: int):
|
||||
return rows
|
||||
|
||||
|
||||
def qsin(deg: int) -> float:
|
||||
"""sin() as the display computes it: reduced to 0..90 degrees first, so sin(30) is a hair
|
||||
under 0.5 and the truncated needle tip lands one pixel short on exact halves."""
|
||||
deg %= 360
|
||||
sign = 1
|
||||
if deg >= 180:
|
||||
deg, sign = deg - 180, -1
|
||||
if deg > 90:
|
||||
deg = 180 - deg
|
||||
return sign * math.sin(math.radians(deg))
|
||||
|
||||
|
||||
def needle_line(x0: int, y0: int, x1: int, y1: int):
|
||||
"""Line from the tip (x0, y0) to the centre (x1, y1): step the major axis, minor offset
|
||||
`(k * minor + minor - 1) // major` (the gauge needle routine)."""
|
||||
dx, dy = abs(x1 - x0), abs(y1 - y0)
|
||||
sx, sy = (1 if x1 >= x0 else -1), (1 if y1 >= y0 else -1)
|
||||
if dx >= dy:
|
||||
return [(x0 + sx * k, y0 + sy * ((k * dy + max(dy - 1, 0)) // dx if dx else 0)) for k in range(dx + 1)]
|
||||
return [(x0 + sx * ((k * dx + max(dx - 1, 0)) // dy), y0 + sy * k) for k in range(dy + 1)]
|
||||
|
||||
|
||||
def gauge_needle(w: int, h: int, val: int, wid: int):
|
||||
"""Pixels of a gauge needle relative to the component's top-left corner.
|
||||
Centre (w//2, h//2); tip at length h//2 - wid; every pixel of that thin line is stamped
|
||||
with a wid x wid square starting at -(wid//2). The needle may leave the component box."""
|
||||
wid = max(1, wid)
|
||||
cx, cy = w // 2, h // 2
|
||||
r = h // 2 - wid
|
||||
tip = (cx + int(-r * qsin(val + 90)), cy + int(-r * qsin(val)))
|
||||
lo = -(wid // 2)
|
||||
return {(x + ax, y + ay) for x, y in needle_line(*tip, cx, cy)
|
||||
for ax in range(lo, lo + wid) for ay in range(lo, lo + wid)}
|
||||
|
||||
|
||||
def rgb(v: int):
|
||||
r, g, b = v >> 11, (v >> 5) & 63, v & 31
|
||||
return (r << 3 | r >> 2, g << 2 | g >> 4, b << 3 | b >> 2)
|
||||
@@ -380,15 +415,12 @@ class Renderer:
|
||||
fill(r // 2, rgb(c.get("pco") or 0))
|
||||
|
||||
def gauge(self, img, c):
|
||||
from PIL import ImageDraw
|
||||
x, y, w, h = self._box(c)
|
||||
self._background(img, c, x, y, w, h)
|
||||
d = ImageDraw.Draw(img)
|
||||
r = min(w, h) // 2 - 1
|
||||
cx, cy = x + w // 2, y + h // 2
|
||||
a = math.radians(c.get("val") or 0)
|
||||
d.line((cx, cy, cx - int(r * math.cos(a)), cy - int(r * math.sin(a))), fill=rgb(c.get("pco") or 0),
|
||||
width=max(1, c.get("wid") or 1))
|
||||
px, col = img.load(), rgb(c.get("pco") or 0)
|
||||
for ax, ay in gauge_needle(w, h, c.get("val") or 0, c.get("wid") or 1):
|
||||
if 0 <= x + ax < img.width and 0 <= y + ay < img.height:
|
||||
px[x + ax, y + ay] = col
|
||||
|
||||
def waveform(self, img, c):
|
||||
from PIL import ImageDraw
|
||||
|
||||
Reference in new issue
Block a user