MATLAB - generating a 2d triangle aperture for diffraction

51 views Asked by At

I have a logical square array such that a value of 1 signifies that light is allowed to pass through the array at those points, and a value of 0 means that light is blocked.

How do I create a triangular aperture shape in this array? I am genuinely stumped and have racked my brain quite a bit but cannot come up with a solution.

1

There are 1 answers

1
Peter On

Consider the logical array as the result of many evaluations of a function, with input x and y. Then your job is to create the function that returns true for points inside the triangle, and false for points outside.

And what would that function be? For that, see the many answers provided on this (unfortunately and inappropriately closed question): How to determine if a point is in a 2D triangle?

Or, since this is MATLAB, use the more general (and therefore less performant) inpolygon. Express the triangle as a list of x coordinates and a list of y coordinates.

Then, test all your points by generating a grid of x and y over your logical matrix, and pass all those points to inpolygon.

The following is untested:

N = 256;  % size of logical array
x = linspace(-1, 1, N);  % let's define the coordinate space of your array as -1 to 1
y = linspace(-1, 1, N);
[X,Y] = meshgrid(x,y);

tri_x = [-.75 0 .75];  % strange triangle
tri_y = [.25 .75 .25];

in_triangle = inpolygon(X, Y, tri_x, tri_y);