Skip to content

Commit

Permalink
<Enhancement>[Rectangle]: <Make Rectangle.Inside() better>
Browse files Browse the repository at this point in the history
[
* Include unit tests
* Some debug in OglInterface2
]

[#112]
  • Loading branch information
Humberto Sanchez II committed Jun 8, 2024
1 parent 988b7b7 commit b64e92c
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 8 deletions.
17 changes: 10 additions & 7 deletions src/miniogl/RectangleShape.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,12 @@ def Inside(self, x, y) -> bool:
"""
True if (x, y) is inside the rectangle.
@param x
@param y
@return bool
Args:
x:
y:
Returns:
"""
# this also works if width and/or height is negative.
sx, sy = self.GetPosition()
Expand All @@ -167,10 +170,10 @@ def Inside(self, x, y) -> bool:
topLeftX: int = sx - self._ox
topLeftY: int = sy - self._oy

topXLeftIsInside: bool = x > topLeftX
topXRightIsInside: bool = x < topLeftX + width
topYLeftIsInside: bool = y > topLeftY
topYRightIsInside: bool = y < topLeftY + height
topXLeftIsInside: bool = x >= topLeftX
topXRightIsInside: bool = x <= topLeftX + width
topYLeftIsInside: bool = y >= topLeftY
topYRightIsInside: bool = y <= topLeftY + height
if topXLeftIsInside and topXRightIsInside and topYLeftIsInside and topYRightIsInside:
return True
else:
Expand Down
1 change: 1 addition & 0 deletions src/ogl/OglInterface2.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ def Draw(self, dc: DC, withChildren: bool = True):
dc.SetFont(self._defaultFont)

xFaceName: str = self.pyutInterface.name
self.logger.debug(f'{xFaceName=} {self._pyutInterface.id=}')

extentSize: Tuple[int, int] = dc.GetTextExtent(xFaceName) # width, height

Expand Down
2 changes: 1 addition & 1 deletion src/ogl/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '2.1.32'
__version__ = '2.1.33'
32 changes: 32 additions & 0 deletions tests/miniogl/TestRectangleShape.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,38 @@ def testInsideFalseViaY(self):

self.assertFalse(actualAnswer, 'The x-coordinate IS NOT inside the rectangle')

def testOnLeftXBoundary(self):

rectangleShape: RectangleShape = RectangleShape(x=120, y=60, width=66, height=60)

isInside: bool = rectangleShape.Inside(x=120, y=90)

self.assertTrue(isInside, 'Left boundary point should be inside')

def testOnRightXBoundary(self):

rectangleShape: RectangleShape = RectangleShape(x=120, y=60, width=66, height=60)

isInside: bool = rectangleShape.Inside(x=120+66, y=90)

self.assertTrue(isInside, 'Right boundary point should be inside')

def testOnTopYBoundary(self):

rectangleShape: RectangleShape = RectangleShape(x=120, y=60, width=66, height=60)

isInside: bool = rectangleShape.Inside(x=120, y=60)

self.assertTrue(isInside, 'Top Y boundary point should be inside')

def testOnBottomYBoundary(self):

rectangleShape: RectangleShape = RectangleShape(x=120, y=60, width=66, height=60)

isInside: bool = rectangleShape.Inside(x=120, y=60+60)

self.assertTrue(isInside, 'Bottom Y boundary point should be inside')


def suite() -> TestSuite:
"""You need to change the name of the test class here also."""
Expand Down

0 comments on commit b64e92c

Please sign in to comment.