|
| 1 | +# SPDX-FileCopyrightText: 2023 Bernhard Bablok |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +""" |
| 6 | +`arc` |
| 7 | +================================================================================ |
| 8 | +
|
| 9 | +Various common shapes for use with displayio - Arc shape! |
| 10 | +
|
| 11 | +
|
| 12 | +* Author(s): Bernhard Bablok |
| 13 | +
|
| 14 | +Implementation Notes |
| 15 | +-------------------- |
| 16 | +
|
| 17 | +**Software and Dependencies:** |
| 18 | +
|
| 19 | +* Adafruit CircuitPython firmware for the supported boards: |
| 20 | + https://github.com/adafruit/circuitpython/releases |
| 21 | +
|
| 22 | +""" |
| 23 | + |
| 24 | +try: |
| 25 | + from typing import Optional |
| 26 | +except ImportError: |
| 27 | + pass |
| 28 | + |
| 29 | +import math |
| 30 | +import displayio |
| 31 | +from adafruit_display_shapes.polygon import Polygon |
| 32 | + |
| 33 | +try: |
| 34 | + import vectorio |
| 35 | + |
| 36 | + HAVE_VECTORIO = True |
| 37 | +except ImportError: |
| 38 | + HAVE_VECTORIO = False |
| 39 | + |
| 40 | +__version__ = "0.0.0+auto.0" |
| 41 | +__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Display_Shapes.git" |
| 42 | + |
| 43 | + |
| 44 | +class Arc(displayio.Group): |
| 45 | + # pylint: disable=too-few-public-methods, invalid-name |
| 46 | + """An arc. Technically, an arc is a Group with one or two polygons. |
| 47 | +
|
| 48 | + An arc is defined by a radius, an angle (in degrees) and a direction (also in |
| 49 | + degrees). The latter is the direction of the midpoint of the arc. |
| 50 | +
|
| 51 | + The direction-parameter uses the layout of polar-coordinates, i.e. zero points |
| 52 | + to the right, 90 to the top, 180 to the left and 270 to the bottom. |
| 53 | +
|
| 54 | + The Arc-class creates the arc as a polygon. The number of segments define |
| 55 | + how round the arc is. There is a memory-tradeoff if the segment-number is |
| 56 | + large. |
| 57 | +
|
| 58 | + :param float radius: The (outer) radius of the arc. |
| 59 | + :param float angle: The angle of the arc in degrees. |
| 60 | + :param float direction: The direction of the middle-point of the arc in degrees (0) |
| 61 | + :param int segments: The number of segments of the arc. |
| 62 | + :param arc_width int: (Optional) The width of the arc. This creates an inner arc as well. |
| 63 | + :param int|None outline: The outline of the arc. Can be a hex value for a color or |
| 64 | + ``None`` for no outline. |
| 65 | + :param int|None fill: The fill-color of the arc. Can be a hex value for a color or |
| 66 | + ``None`` for no filling. Ignored if port does not support vectorio. |
| 67 | + """ |
| 68 | + |
| 69 | + def __init__( |
| 70 | + # pylint: disable=too-many-arguments, too-many-locals |
| 71 | + self, |
| 72 | + radius: float, |
| 73 | + angle: float, |
| 74 | + direction: float, |
| 75 | + segments: int, |
| 76 | + *args, |
| 77 | + arc_width: Optional[int] = 1, |
| 78 | + outline: Optional[int] = None, |
| 79 | + fill: Optional[int] = None, |
| 80 | + **kwargs, |
| 81 | + ) -> None: |
| 82 | + super().__init__(*args, **kwargs) |
| 83 | + # shift direction by angle/2 |
| 84 | + direction = direction - angle / 2 |
| 85 | + # create outer points |
| 86 | + points = [] |
| 87 | + for i in range(segments + 1): |
| 88 | + alpha = (i * angle / segments + direction) / 180 * math.pi |
| 89 | + x0 = int(radius * math.cos(alpha)) |
| 90 | + y0 = -int(radius * math.sin(alpha)) |
| 91 | + points.append((x0, y0)) |
| 92 | + |
| 93 | + # create inner points |
| 94 | + if arc_width > 1: |
| 95 | + for i in range(segments, -1, -1): |
| 96 | + alpha = (i * angle / segments + direction) / 180 * math.pi |
| 97 | + x0 = int((radius - arc_width) * math.cos(alpha)) |
| 98 | + y0 = -int((radius - arc_width) * math.sin(alpha)) |
| 99 | + points.append((x0, y0)) |
| 100 | + |
| 101 | + # create polygon(s) and add to ourselves |
| 102 | + if arc_width > 1 and HAVE_VECTORIO and fill is not None: |
| 103 | + palette = displayio.Palette(1) |
| 104 | + palette[0] = fill |
| 105 | + self.append(vectorio.Polygon(pixel_shader=palette, points=points, x=0, y=0)) |
| 106 | + if outline is not None: |
| 107 | + self.append(Polygon(points, outline=outline, colors=1, close=arc_width > 1)) |
0 commit comments