Search code examples
pythonmask

Array mask boundary given by polar equation


I have the following polar equation: r(theta) = R + a*Sin(n*theta) which makes this kind of plot (for which I have used R=1, a=0.1, and n=5):

enter image description here

I want to end up with a 2D Cartesian array which is 0 inside this boundary, and 1 outisde of it (red pen marks).

Does anyone know of an "elegant" and simple way to do this?

My attempts so far (and ongoing) are just trying to convert a polar meshgrid to a cartesian grid...


Solution

  • Whatever form your array takes, the value of each location is simply a test against the functional value:

    import math
    
    def array_val(x, y):
        # Compute the function value for the proper angle;
        # Compare to the actual radius; return 0 or 1
    
        theta = math.atan(y/x)    # Adjust for proper quadrant
        r = math.sqrt(x*x + y*y)
        return int(r <= (R + a * math.sin(5 * theta)))