Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vector line draw functions #64

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions pyndustri.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,24 @@ class Screen:
You may use it to make rotated rectangles by setting a big enough stroke.
"""
@staticmethod
def deg_line(x0: int, y0: int, degrees: float, magnitude: float):
"""
Draw a line with vector data.

Line drawn is from `(x0, y0)` to `magnitude` pixels toward `degrees` degrees counterclockwise from east (90° -> up).

The line will be as wide as previously defined by `stroke`, and the ends will be straight.
"""
@staticmethod
def rad_line(x0: int, y0: int, radians: float, magnitude: float):
"""
Draw a line with vector data.

Line drawn is from `(x0, y0)` to `magnitude` pixels toward `radians` radians counterclockwise from east (π/2 -> up).

The line will be as wide as previously defined by `stroke`, and the ends will be straight.
"""
@staticmethod
def rect(x: int, y: int, width: int, height: int):
"""
Draw a filled rectangle with its bottom-left corner at `(x, y)` and size `(width, height)`.
Expand Down
23 changes: 23 additions & 0 deletions pyndustric/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from pathlib import Path
from typing import Callable, Union
from string import hexdigits
from math import sin, cos, radians
import ast
import inspect
import sys
Expand Down Expand Up @@ -675,6 +676,28 @@ def emit_screen_syscall(self, node: ast.Call):
x0, y0, x1, y1 = map(self.as_value, node.args)
self.ins_append(f"draw line {x0} {y0} {x1} {y1}")

elif method == "deg_line":
if len(node.args) != 4:
raise CompilerError(ERR_BAD_SYSCALL_ARGS, node)

x0, y0, degs, magnitude = map(self.as_value, node.args)

x1 = round(int(x0) + cos(radians(float(degs))) * float(magnitude))
y1 = round(int(y0) + sin(radians(float(degs))) * float(magnitude))

self.ins_append(f"draw line {x0} {y0} {x1} {y1}")

elif method == "rad_line":
if len(node.args) != 4:
raise CompilerError(ERR_BAD_SYSCALL_ARGS, node)

x0, y0, rads, magnitude = map(self.as_value, node.args)

x1 = round(int(x0) + cos(float(rads)) * float(magnitude))
y1 = round(int(y0) + sin(float(rads)) * float(magnitude))

self.ins_append(f"draw line {x0} {y0} {x1} {y1}")

elif method == "rect":
if len(node.args) != 4:
raise CompilerError(ERR_BAD_SYSCALL_ARGS, node)
Expand Down
19 changes: 16 additions & 3 deletions test_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import sys
import tempfile


_REG_TMP_RE = re.compile(r"\b" + pyndustric.REG_TMP_FMT.replace("{}", r"(\w+)") + r"\b")


Expand Down Expand Up @@ -1130,9 +1129,9 @@ def test_display_col():


@masm_test
def control_config():
def test_control_config():
"""
control config sorter 1 @titanium
control config sorter1 @titanium
"""
sorter1.config(Env.titanium)

Expand All @@ -1156,3 +1155,17 @@ def test_complex_compare():
op lessThan a a 15
"""
a = 1 < 2 < 3 < 4 < 5 < 6 < 7 < 8 < 9 < 10 < 11 < 12 < 13 < 14 < 15


@masm_test
def test_angle_lines():
"""
draw line 50 50 42 44
draw line 30 25 30 30
draw line 50 50 42 44
draw line 30 25 30 30
"""
Screen.deg_line(50, 50, 220, 10)
Screen.deg_line(30, 25, 90, 5)
Screen.rad_line(50, 50, 3.84, 10)
Screen.rad_line(30, 25, 1.57, 5)