Check if points are inside ellipse faster than contains_point method

后端 未结 2 1704
南笙
南笙 2021-02-06 00:32

I use matplotlib 1.15.1 and I try to generate scattergrams like this:

\"example\"

The ellipses have fixes s

2条回答
  •  我在风中等你
    2021-02-06 01:13

    Your current implementation should only be calling contains_point 25,000 to 50,000 times, which isn't a lot. So, I'm guessing that the implementation of contains_point is targeted toward precision rather than speed.

    Since you have a distribution of points where only a small percentage will be in any given ellipse, and therefore most will rarely be anywhere near any given ellipse, you can easily use rectangular coordinates as a short-cut to figure out whether the point is close enough to the ellipse to be worth calling contains_point.

    Compute the left and right x coordinates and top and bottom y coordinates of the ellipse, possibly with a bit of padding to account for rendering differences, then check if the point is within those, such as the following pseudo-code:

    if point.x >= ellipse_left and point.x <= ellipse_right and _
       point.y >= ellipse_top and point.y <= ellipse_bottom:
        if ellipse.contains_point(point, radius=0):
            ... use the contained point here
    

    This approach eliminates expensive calculations for most of the points, allowing simple comparisons instead to rule out the obvious mismatches, while preserving the accuracy of the computations where the point is close enough that it might be in the ellipse. If e.g. only 1% of your points are anywhere near a given ellipse, this approach will eliminate 99% of your calls to contains_point and instead replace them with much faster comparisons.

提交回复
热议问题