1 # SPDX-FileCopyrightText: 2021 Melissa LeBlanc-Williams for Adafruit Industries
2 # SPDX-FileCopyrightText: 2021 James Carr
4 # SPDX-License-Identifier: MIT
8 ================================================================================
10 Area for Blinka Displayio
12 **Software and Dependencies:**
15 https://github.com/adafruit/Adafruit_Blinka/releases
17 * Author(s): James Carr, Melissa LeBlanc-Williams
21 from __future__ import annotations
23 __version__ = "0.0.0+auto.0"
24 __repo__ = "https://github.com/adafruit/Adafruit_Blinka_Displayio.git"
28 """Area Class to represent an area to be updated."""
30 # pylint: disable=invalid-name
31 def __init__(self, x1: int = 0, y1: int = 0, x2: int = 0, y2: int = 0):
39 return f"Area TL({self.x1},{self.y1}) BR({self.x2},{self.y2})"
41 def _copy_into(self, dst) -> None:
47 def _scale(self, scale: int) -> None:
53 def _shift(self, dx: int, dy: int) -> None:
59 def compute_overlap(self, other, overlap) -> bool:
60 """Compute the overlap between two areas. Returns True if there is an overlap."""
62 overlap.x1 = max(a.x1, other.x1)
63 overlap.x2 = min(a.x2, other.x2)
65 if overlap.x1 >= overlap.x2:
68 overlap.y1 = max(a.y1, other.y1)
69 overlap.y2 = min(a.y2, other.y2)
71 return overlap.y1 < overlap.y2
74 return (self.x1 == self.x2) or (self.y1 == self.y2)
78 self.x1, self.x2 = self.x2, self.x1
80 self.y1, self.y2 = self.y2, self.y1
82 def _union(self, other, union):
84 self._copy_into(union)
86 if other._empty(): # pylint: disable=protected-access
87 other._copy_into(union) # pylint: disable=protected-access
90 union.x1 = min(self.x1, other.x1)
91 union.y1 = min(self.y1, other.y1)
92 union.x2 = max(self.x2, other.x2)
93 union.y2 = max(self.y2, other.y2)
95 def width(self) -> int:
96 """Return the width of the area."""
97 return self.x2 - self.x1
99 def height(self) -> int:
100 """Return the height of the area."""
101 return self.y2 - self.y1
103 def size(self) -> int:
104 """Return the size of the area."""
105 return self.width() * self.height()
107 def __eq__(self, other):
108 if not isinstance(other, Area):
113 and self.y1 == other.y1
114 and self.x2 == other.x2
115 and self.y2 == other.y2
119 def transform_within(
127 """Transform an area within a larger area."""
128 # pylint: disable=too-many-arguments
129 # Original and whole must be in the same coordinate space.
131 transformed.x1 = whole.x1 + (whole.x2 - original.x2)
132 transformed.x2 = whole.x2 - (original.x1 - whole.x1)
134 transformed.x1 = original.x1
135 transformed.x2 = original.x2
138 transformed.y1 = whole.y1 + (whole.y2 - original.y2)
139 transformed.y2 = whole.y2 - (original.y1 - whole.y1)
141 transformed.y1 = original.y1
142 transformed.y2 = original.y2
147 transformed.y1 = whole.y1 + (transformed.x1 - whole.x1)
148 transformed.y2 = whole.y1 + (transformed.x2 - whole.x1)
149 transformed.x1 = whole.x1 + (y1 - whole.y1)
150 transformed.x2 = whole.x1 + (y2 - whole.y1)