Radio rendering pixel-exact: the display's circle routine

Harvested the cir/cirs output for every radius 1..159 from Nextion
Editor's simulator and derived the incremental midpoint rule that
reproduces all of them; radio = width-sized circle clipped to the box,
dot radius r//2. Basic rendering coverage 18/20.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
mangavalkandClaude Opus 5.5 committed 2026-11-15 15:53:13 +01:00
1 parent 6f8341c66c
commit 9b50eea03f
7 files changed
+83 -14

No files matched your search

+50 -4
View File
@@ -21,6 +21,36 @@ from .page import Component, Page
from . import pictures
def circle_octant(r: int):
"""First octant (x >= y >= 0) of the display's circle routine -- reproduces the Nextion `cir`
command and the Editor canvas exactly (verified for every radius 1..159)."""
pts = []
x, y = r, 0
while x >= y:
pts.append((x, y))
y += 1
if x * x - x + y * y + 2 * y > r * r + 1:
x -= 1
return pts
def circle_points(r: int):
"""All points of a ring of radius r around (0, 0) (8-way symmetric)."""
pts = set()
for x, y in circle_octant(r):
for a, b in ((x, y), (y, x)):
pts.update({(a, b), (-a, b), (a, -b), (-a, -b)})
return pts
def disc_rows(r: int):
"""{dy: half-width} of a filled circle (Nextion `cirs`): every pixel inside the ring."""
rows = {}
for a, b in circle_points(r):
rows[b] = max(rows.get(b, 0), abs(a))
return rows
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)
@@ -326,12 +356,28 @@ class Renderer:
d.rectangle((x + w // 4, y + h // 4, x + 3 * w // 4 - 1, y + 3 * h // 4 - 1), fill=rgb(c.get("pco") or 0))
def radio(self, img, c):
from PIL import ImageDraw
"""Circle sized by the width, centred at ((w-1)//2, (w-1)//2), clipped to the box:
bco disc, ring (65,68,65), and when checked a pco dot of half the radius."""
x, y, w, h = self._box(c)
d = ImageDraw.Draw(img)
d.ellipse((x, y, x + w - 2, y + h - 2), fill=rgb(c.get("bco") or 0), outline=(65, 68, 65))
r = (w - 1) // 2
cx, cy = x + r, y + r
px = img.load()
W, H = img.size
def put(xx, yy, col):
if x <= xx < x + w and y <= yy < y + h and 0 <= xx < W and 0 <= yy < H:
px[xx, yy] = col
def fill(rad, col):
for dy, hw in disc_rows(rad).items():
for dx in range(-hw, hw + 1):
put(cx + dx, cy + dy, col)
fill(r, rgb(c.get("bco") or 0))
for dx, dy in circle_points(r):
put(cx + dx, cy + dy, (65, 68, 65))
if c.get("val"):
d.ellipse((x + w // 4, y + h // 4, x + 3 * w // 4 - 2, y + 3 * h // 4 - 2), fill=rgb(c.get("pco") or 0))
fill(r // 2, rgb(c.get("pco") or 0))
def gauge(self, img, c):
from PIL import ImageDraw