Py5Shape.contains()

Py5Shape.contains()#

Boolean value reflecting if the given coordinates are or are not contained within the Py5Shape object.

Examples#

example picture for contains()

def setup():
    chr_p = py5.create_font('DejaVu Sans', 32).get_shape('p')
    x_vertex_values = [chr_p.get_vertex_x(i) for i in range(chr_p.get_vertex_code_count())]
    y_vertex_values = [chr_p.get_vertex_y(i) for i in range(chr_p.get_vertex_code_count())]
    min_x, max_x = min(x_vertex_values), max(x_vertex_values)
    min_y, max_y = min(y_vertex_values), max(y_vertex_values)

    for _ in range(1000):
        x, y = py5.random(min_x, max_x), py5.random(min_y, max_y)
        if chr_p.contains(x, y):
            py5.point(2 * x, 2 * y + py5.height / 2)

Description#

Boolean value reflecting if the given coordinates are or are not contained within the Py5Shape object. This method will only work for a Py5Shape object that is a PATH shape or a GROUP of PATH shapes. Use Py5Shape.get_family() to determine how a Py5Shape object was defined.

This method uses a coordinate system that is unique to the shape and how the paths were created. To get the range of relevant coordinates, start by finding the minimum and maximum values for the vertices using Py5Shape.get_vertex_x() and Py5Shape.get_vertex_y(). Do not use Py5Shape.get_width() or Py5Shape.get_height().

Underlying Processing method: PShape.contains

Signatures#

contains(
    x: float,  # x-coordinate
    y: float,  # y-coordinate
    /,
) -> bool

Updated on March 06, 2023 02:49:26am UTC