Skip to content

Commit

Permalink
Make marker sizes correspond to radius.
Browse files Browse the repository at this point in the history
This change means that the size geometry in Marker objects is roughly the
radius of the marker.  The primary advantage of this is that the sizes no
longer step every 2 increase in size due to integer division by 2.

Fixes #70.
  • Loading branch information
corranwebster committed Dec 9, 2024
1 parent 0c09064 commit 1928fc2
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 17 deletions.
1 change: 0 additions & 1 deletion examples/line_plot_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from tempe import colors
from tempe.data_view import Repeat
from tempe.geometry import ColumnGeometry, PointsToLines
from tempe.markers import Marker
from tempe.surface import Surface
from tempe.text import CENTER, TOP, RIGHT

Expand Down
2 changes: 1 addition & 1 deletion examples/polar_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@
]
),
),
ColumnGeometry([Repeat(24)]),
ColumnGeometry([Repeat(12)]),
]
),
Repeat(grey_3),
Expand Down
5 changes: 2 additions & 3 deletions examples/polar_plot_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@

from tempe import colors
from tempe.colormaps.viridis import viridis
from tempe.data_view import Range, Repeat
from tempe.geometry import ColumnGeometry, RowGeometry, PointsToLines
from tempe.markers import Marker
from tempe.data_view import Repeat
from tempe.geometry import ColumnGeometry, PointsToLines
from tempe.polar_geometry import polar_points, polar_r_lines
from tempe.surface import Surface
from tempe.text import LEFT, CENTER, RIGHT, TOP, BOTTOM
Expand Down
2 changes: 1 addition & 1 deletion examples/scatter_plot_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def scale_values(self, data):
low_screen = self.low_screen
scale = self.scale
for i, value in enumerate(data):
screen[i] = int(sqrt(low_screen + scale * (value - low_data)))
screen[i] = int(sqrt(low_screen + scale * (value - low_data)) // 2)
return screen


Expand Down
2 changes: 1 addition & 1 deletion examples/shapes_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@
[
Range(160, 320, 16),
Repeat(30),
Range(5, 16, 1),
Range(1, 13, 1),
]
),
Interpolated(viridis, 10),
Expand Down
18 changes: 9 additions & 9 deletions src/tempe/markers.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,22 @@ def draw_raster(self, raster):
size = geometry[2]
if px + size < 0 or px - size > w or py + size < 0 or py - size > h:
continue
if size < 2 or marker == Marker.PIXEL:
if size < 1 or marker == Marker.PIXEL:
buffer.pixel(px, py, color)
elif marker == Marker.CIRCLE:
buffer.ellipse(px, py, size // 2, size // 2, color, True)
buffer.ellipse(px, py, size, size, color, True)
elif marker == Marker.SQUARE:
buffer.rect(px - size // 2, py - size // 2, size, size, color, True)
buffer.rect(px - size, py - size, 2*size+1, 2*size+1, color, True)
elif marker == Marker.HLINE:
buffer.hline(px - size // 2, py, size, color)
buffer.hline(px - size, py, 2*size+1, color)
elif marker == Marker.VLINE:
buffer.vline(px, py - size // 2, size, color)
buffer.vline(px, py - size, 2*size+1, color)
elif marker == Marker.PLUS:
size = 2 * (size // 2) + 1 # odd numper of pixels
buffer.hline(px - size // 2, py, size, color)
buffer.vline(px, py - size // 2, size, color)
d = 2 * size + 1 # odd numper of pixels
buffer.hline(px - size, py, d, color)
buffer.vline(px, py - size, d, color)
elif marker == Marker.CROSS:
d = (size * 17 // 48) + 1 # very rough approximation of 1/(2*sqrt(2))
d = (size * 17 // 24) + 1 # very rough approximation of r/(sqrt(2)) + 1
buffer.line(px - d, py - d, px + d, py + d, color)
buffer.line(px - d, py + d, px + d, py - d, color)
elif isinstance(marker, str):
Expand Down
4 changes: 4 additions & 0 deletions src/tempe/markers.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ class Marker:
class Markers(ColoredGeometry[point_length]):
"""Display sized, colored markers at points.
Markers expect a geometry of the form (x, y, radius).
Parameters
----------
geometry : Iterable[geom] | None
Expand Down Expand Up @@ -73,6 +75,8 @@ class Markers(ColoredGeometry[point_length]):
class Points(ColoredGeometry[point]):
"""Display colored markers at points.
Points expect a geometry of the form (x, y)
Parameters
----------
geometry : Iterable[geom] | None
Expand Down
4 changes: 3 additions & 1 deletion src/tempe/surface.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -342,12 +342,14 @@ class Surface:
) -> Markers:
"""Create a new Markers object and add it to the layer.
Markers expect a geometry of the form (x, y, radius).
Parameters
----------
layer : Any
The layer that the Markers object is added to.
geometry : Geometry[tuple[int, int, int]] | tuple[int, int, int]
The geometry to use, or if a tuple of 3 ints, a single point and size.
The geometry to use, or if a tuple of 3 ints, a single point and radius.
colors : Iterable[int] | int
The colors of each marker, or a color to use for all markers.
markers : Iterable[Any] | int | str | framebuf.FrameBuffer
Expand Down
Binary file modified tests/tempe/shapes.rgb565
Binary file not shown.

0 comments on commit 1928fc2

Please sign in to comment.