Skip to content

vectorio contains(x, y) #5784

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

Merged
merged 1 commit into from
Dec 31, 2021
Merged

vectorio contains(x, y) #5784

merged 1 commit into from
Dec 31, 2021

Conversation

kvc0
Copy link

@kvc0 kvc0 commented Dec 27, 2021

new utility function for all vectorio shape specializations for testing
whether a screen-space x,y point falls within a shape's x,y.

This respects the current orientation of the screen in the manner of
displayio and vectorio - so your x,y requests are in the same coordinate
domain as your x,y locations and your width/height etc. properties that
ou set on other shapes. I.e., if you're using this for touch points then
you will need to make sure the touch events are in the same x,y domain as
your display.

contains(2, 4) -> true
------------------
|                |
|                |
| --             |
| | \            |
| |. \           |
| |   \          |
| |____\         |
|                |
------------------

contains(5, 4) -> false
------------------
|                |
|                |
| --             |
| | \            |
| |  \.          |
| |   \          |
| |____\         |
|                |
------------------

This helps provide low overhead introspection of shape coverage on screen.
It's envisioned that this will be used for things like touch-and-drag
widget controls, touch "areas" and may help with random ornament placement
on toy Christmas trees.

I am out of state for a few more days and do not have a microcontroller with me - and I can't seem to make displayio compile into the unix port or I'd give this a try myself. It compiles but not 100% sure about it - I'll be able to try in a few days! Feel free to review & see if anything stands out as problematic.

@FoamyGuy merry Christmas and I hope your next tree can have a little more random cheer 🍻

new utility function for all vectorio shape specializations for testing
whether a screen-space x,y point falls within a shape's x,y.

This respects the current orientation of the screen in the manner of
displayio and vectorio - so your x,y requests are in the same coordinate
domain as your x,y locations and your width/height etc. properties that
ou set on other shapes. I.e., if you're using this for touch points then
you will need to make sure the touch events are in the same x,y domain as
your display.

```
contains(2, 4) -> true
------------------
|                |
|                |
| --             |
| | \            |
| |. \           |
| |   \          |
| |____\         |
|                |
------------------

contains(5, 4) -> false
------------------
|                |
|                |
| --             |
| | \            |
| |  \.          |
| |   \          |
| |____\         |
|                |
------------------
```

This helps provide low overhead introspection of shape coverage on screen.
It's envisioned that this will be used for things like touch-and-drag
widget controls, touch "areas" and may help with random ornament placement
on toy Christmas trees.
Copy link
Collaborator

@FoamyGuy FoamyGuy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested the functionality successfully on a pyportal with this test script:

import board
import vectorio
import displayio

display = board.DISPLAY
main_group = displayio.Group()

_palette = displayio.Palette(1)
_palette[0] = 0x4400dd
_palette2 = displayio.Palette(1)
_palette2[0] = 0xaa3344

# rectangle = vectorio.Rectangle(
#     pixel_shader=_palette,
#     width=200, height=100,
#     x=10, y=10)
# main_group.append(rectangle)

circle = vectorio.Circle(
    pixel_shader=_palette,
    radius=60,
    x=100, y=100
)
main_group.append(circle)

triangle = vectorio.Polygon(
    pixel_shader=_palette,
    points=[(0, 0), (100, 0), (70, 40)],
    x=170, y=100
)
main_group.append(triangle)

point = (238, 139)

point_vec = vectorio.Circle(
    pixel_shader=_palette2,
    radius=1,
    x=point[0], y=point[1]
)
main_group.append(point_vec)

display.show(main_group)

print(triangle.contains(*point))
# print(rectangle.contains(10, 109))
print("running")
while True:
    pass

Rectangle, Circle, and polygon all seem to report correct values for points passed to contains.

Thanks @WarriorOfWire this is great!

@jepler jepler merged commit 8a94d9a into adafruit:main Dec 31, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants