I am trying to create a function using Matlab which takes the angle(specifies the rotation angle of the square) and the length of the square and returns a matrix containing 0 and 1 such that the value of each cell is 1 if and only if that cell be on the square, otherwise 0.
Following example helps to make it more clear:
d = 8 = length of square
theta = 0
size of image containing the square = 16
img = zeros(16, 16);
img(o.x-d:o.x+d, o.y-d:o.y+d) = 1
output:
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000111111110000
0000111111110000
0000111111110000
0000111111110000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
Example above is easy to show because the angle is equal to 0.
P.S. A way could be finding a position of each pixel(cell) and verify whether the pixel is inside the square or not, but the goal is finding a simple way.
The fastest way would probably involve a variation of Bresenham’s algorithm, but that won’t really make that much difference as compared to checking whether a particular pixel is inside the square or not. The latter is trivially done by rotating the pixel’s coordinates around the center and checking whether they are within the bounds, something like