gpt answer with sloped lines

This commit is contained in:
Your Name
2025-12-13 00:33:58 +00:00
parent 25c4054dbc
commit 4d2ef1a667

View File

@@ -1,108 +1,182 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
""" """
draw_12_rects_thick_outline.py draw_12_slanted_top_bottom.py
800×600 ARGB framebuffer. * 800×600 ARGB framebuffer (A,R,G,B each 1byte)
12 tall rectangles (aspect1:12) placed sidebyside with a gap. * 12 rectangles, aspect1:12, 5px gap, 100px empty margin at top & bottom
Only the rectangle outline is drawn, the border thickness can be changed * each rectangle has an individual tilt angle (degrees) stored in
with the constant LINE_THICKNESS (default = 3 pixels). ROT_ANGLES_DEG[12]
* the top and bottom edges are *slanted* while the left and righthand
The complete framebuffer is streamed to a TCP server listening on edges remain perfectly vertical
localhost:12345 (payload is prefixed with a 4byte bigendian length). * outline colour = opaque white, thickness = LINE_THICKNESS (default=3px)
* the raw buffer (with a 4byte length prefix) is streamed to a TCP server
listening on localhost:12345
""" """
import math
import socket import socket
import sys import sys
# ---------------------------------------------------------------------- # ----------------------------------------------------------------------
# Framebuffer configuration # 1 Framebuffer configuration
# ---------------------------------------------------------------------- # ----------------------------------------------------------------------
FB_WIDTH = 800 # horizontal pixels FB_WIDTH = 800 # horizontal pixels
FB_HEIGHT = 600 # vertical pixels FB_HEIGHT = 600 # vertical pixels
BPP = 4 # bytes per pixel (A,R,G,B) BPP = 4 # bytes per pixel (A,R,G,B)
# ---------------------------------------------------------------------- # ----------------------------------------------------------------------
# Geometry of the 12 rectangles # 2⃣ Margins & rectangle geometry (before slant)
# ---------------------------------------------------------------------- # ----------------------------------------------------------------------
RECT_HEIGHT = 400 # fill the whole image vertically MARGIN_TOP = 100 # empty band at the very top
RECT_WIDTH = RECT_HEIGHT // 12 # 600 / 12 = 50px (maintains 1:12) MARGIN_BOTTOM = 300 # empty band at the very bottom
SPACING = 10 # pixels between two neighbours RECT_HEIGHT = FB_HEIGHT - MARGIN_TOP - MARGIN_BOTTOM # usable height = 400px
RECT_WIDTH = RECT_HEIGHT // 12 # ≈33px (aspect1:12)
SPACING = 10 # gap between rectangles
TOTAL_RECT_W = 12 * RECT_WIDTH + 11 * SPACING TOTAL_RECT_W = 12 * RECT_WIDTH + 11 * SPACING
X0_START = (FB_WIDTH - TOTAL_RECT_W) // 2 # centre the whole strip X0_START = (FB_WIDTH - TOTAL_RECT_W) // 2 # centre the whole strip
# ---------------------------------------------------------------------- # ----------------------------------------------------------------------
# Outline thickness (change this to whatever you need) # 3 Outline thickness (you may change it)
# ---------------------------------------------------------------------- # ----------------------------------------------------------------------
LINE_THICKNESS = 4 # >=1 ; thicker than 1 pixel LINE_THICKNESS = 5 # ≥ 1 pixel
# ---------------------------------------------------------------------- # ----------------------------------------------------------------------
# Helper write a pixel into the bytearray (no bounds check for speed) # 4⃣ Perrectangle tilt angles (degrees)
# ----------------------------------------------------------------------
# You can generate these programmatically, read them from a file, etc.
ROT_ANGLES_DEG = [
90, 5, 10, 15, 20, 25,
30, 35, 40, 45, 50, 85
]
assert len(ROT_ANGLES_DEG) == 12, "Exactly 12 angles are required."
# ----------------------------------------------------------------------
# Helper write a single pixel (boundschecked)
# ---------------------------------------------------------------------- # ----------------------------------------------------------------------
def set_pixel(buf: bytearray, x: int, y: int, color: tuple) -> None: def set_pixel(buf: bytearray, x: int, y: int, color: tuple) -> None:
"""Write an ARGB pixel at (x, y). `color` = (A,R,G,B).""" """Write an ARGB pixel at (x, y). `color` = (A,R,G,B)."""
offset = (y * FB_WIDTH + x) * BPP if 0 <= x < FB_WIDTH and 0 <= y < FB_HEIGHT:
buf[offset:offset + 4] = bytes(color) # A,R,G,B order off = (y * FB_WIDTH + x) * BPP
buf[off:off + 4] = bytes(color) # A,R,G,B order
# ---------------------------------------------------------------------- # ----------------------------------------------------------------------
# Fill a rectangular region (used for thick edges) # Helper draw a thick line (Bresenham + square brush)
# ---------------------------------------------------------------------- # ----------------------------------------------------------------------
def fill_rect(buf: bytearray, x0: int, y0: int, w: int, h: int, color: tuple) -> None: def draw_thick_line(buf: bytearray,
"""Write a solid w×h block of `color` starting at (x0, y0).""" x0: int, y0: int,
# Clip to the frame buffer safety net for very thick lines at the border x1: int, y1: int,
x0 = max(0, x0) color: tuple,
y0 = max(0, y0) thickness: int) -> None:
w = min(w, FB_WIDTH - x0) """Draw a line from (x0,y0) to (x1,y1) using a square brush of `thickness`."""
h = min(h, FB_HEIGHT - y0) dx = abs(x1 - x0)
dy = -abs(y1 - y0)
sx = 1 if x0 < x1 else -1
sy = 1 if y0 < y1 else -1
err = dx + dy # error term
row_bytes = w * BPP while True:
pixel_bytes = bytes(color) # paint a filled square centred on the current pixel
for ty in range(-thickness // 2, thickness // 2 + 1):
for tx in range(-thickness // 2, thickness // 2 + 1):
set_pixel(buf, x0 + tx, y0 + ty, color)
for y in range(y0, y0 + h): if x0 == x1 and y0 == y1:
off = (y * FB_WIDTH + x0) * BPP break
buf[off:off + row_bytes] = pixel_bytes * w e2 = 2 * err
if e2 >= dy:
err += dy
x0 += sx
if e2 <= dx:
err += dx
y0 += sy
# ---------------------------------------------------------------------- # ----------------------------------------------------------------------
# Build the framebuffer # 5 Build the framebuffer (background + white slantedtop/bottom rectangles)
# ---------------------------------------------------------------------- # ----------------------------------------------------------------------
def build_framebuffer() -> bytearray: def build_framebuffer() -> bytearray:
"""Create an ARGB buffer, fill it black and draw the thickoutline rectangles.""" """Create the ARGB buffer and draw the 12 rectangles with slanted top/bottom."""
# 1 background = opaque black (A,R,G,B) # ----- background: opaque black -----
fb = bytearray(FB_WIDTH * FB_HEIGHT * BPP) fb = bytearray(FB_WIDTH * FB_HEIGHT * BPP)
bg_color = (255, 0, 0, 0) # opaque black bg = (255, 0, 0, 0) # A,R,G,B = opaque black
fb[:] = bg_color * (FB_WIDTH * FB_HEIGHT) fb[:] = bg * (FB_WIDTH * FB_HEIGHT)
WHITE = (255, 255, 255, 255) # colour of the outlines
half_w = RECT_WIDTH // 2 # half the (unslanted) width
y_top = MARGIN_TOP
y_bottom = FB_HEIGHT - MARGIN_BOTTOM - 1 # inclusive bottom row
# 2⃣ draw each rectangle outline
for idx in range(12): for idx in range(12):
# ── colour palette (feel free to change) ─────────────────────── # --------------------------------------------------------------
rect_color = (255, 255, 255, 255) # opaque # 1⃣ Unslanted left edge of the rectangle (including spacing)
# --------------------------------------------------------------
orig_x0 = X0_START + idx * (RECT_WIDTH + SPACING)
# leftmost X coordinate of the *inner* rectangle (the border belongs to it) # --------------------------------------------------------------
x0 = X0_START + idx * (RECT_WIDTH + SPACING) # 2⃣ Centre X coordinate (kept fixed while slanting)
y0 = 100 # --------------------------------------------------------------
w = RECT_WIDTH cx = orig_x0 + half_w
h = RECT_HEIGHT
# ── thick edges ────────────────────────────────────────────────── # --------------------------------------------------------------
# 3⃣ Angle for this rectangle
# --------------------------------------------------------------
angle_rad = math.radians(ROT_ANGLES_DEG[idx])
sin_a = math.sin(angle_rad)
# --------------------------------------------------------------
# 4⃣ Vertical offset applied to the *ends* of the top/bottom lines
# (the spec says: sin(angle) * RECT_WITH / 2)
# --------------------------------------------------------------
offset = sin_a * half_w # because RECT_WITH/2 == half_w
# --------------------------------------------------------------
# 5⃣ Coordinates of the four line endpoints
# --------------------------------------------------------------
# top edge # top edge
fill_rect(fb, x0, y0, w, LINE_THICKNESS, rect_color) x0_top = cx - half_w
y0_top = int(round(y_top + offset))
x1_top = cx + half_w
y1_top = int(round(y_top - offset))
# bottom edge # bottom edge
fill_rect(fb, x0, y0 + h - LINE_THICKNESS, w, LINE_THICKNESS, rect_color) x0_bot = cx - half_w
y0_bot = int(round(y_bottom - offset))
# left edge x1_bot = cx + half_w
fill_rect(fb, x0, y0, LINE_THICKNESS, h, rect_color) y1_bot = int(round(y_bottom + offset))
# right edge # --------------------------------------------------------------
fill_rect(fb, x0 + w - LINE_THICKNESS, y0, LINE_THICKNESS, h, rect_color) # 6⃣ Draw the four sides
# --------------------------------------------------------------
# top (slanted)
draw_thick_line(fb, x0_top, y0_top, x1_top, y1_top,
WHITE, LINE_THICKNESS)
# bottom (slanted)
draw_thick_line(fb, x0_bot, y0_bot, x1_bot, y1_bot,
WHITE, LINE_THICKNESS)
# left vertical side from the *topleft* endpoint down to the
# *bottomleft* endpoint
draw_thick_line(fb, x0_top, y0_top, x0_bot, y0_bot,
WHITE, LINE_THICKNESS)
# right vertical side from the *topright* endpoint down to the
# *bottomright* endpoint
draw_thick_line(fb, x1_top, y1_top, x1_bot, y1_bot,
WHITE, LINE_THICKNESS)
return fb return fb
# ---------------------------------------------------------------------- # ----------------------------------------------------------------------
# Send the buffer to the TCP server # 6 Send the framebuffer to the TCP server (localhost:12345)
# ---------------------------------------------------------------------- # ----------------------------------------------------------------------
def send_framebuffer(buf: bytearray, def send_framebuffer(buf: bytearray,
host: str = "127.0.0.1", host: str = "127.0.0.1",
@@ -110,7 +184,7 @@ def send_framebuffer(buf: bytearray,
"""Open a TCP socket, connect, and stream the whole framebuffer.""" """Open a TCP socket, connect, and stream the whole framebuffer."""
try: try:
with socket.create_connection((host, port), timeout=5) as sock: with socket.create_connection((host, port), timeout=5) as sock:
# optional length prefix (many protocols expect it) # optional 4byte length prefix many simple protocols expect it
length_prefix = len(buf).to_bytes(4, byteorder="big") length_prefix = len(buf).to_bytes(4, byteorder="big")
sock.sendall(length_prefix + buf) sock.sendall(length_prefix + buf)
print(f"✔ Sent {len(buf)} bytes to {host}:{port}") print(f"✔ Sent {len(buf)} bytes to {host}:{port}")
@@ -119,7 +193,7 @@ def send_framebuffer(buf: bytearray,
# ---------------------------------------------------------------------- # ----------------------------------------------------------------------
# Main entry point # 7 Main entry point
# ---------------------------------------------------------------------- # ----------------------------------------------------------------------
def main() -> None: def main() -> None:
fb = build_framebuffer() fb = build_framebuffer()
@@ -128,3 +202,4 @@ def main() -> None:
if __name__ == "__main__": if __name__ == "__main__":
main() main()