# Random figures in the matrix

Consider the matrix 'm1' :

m1 = RandomReal[{0, 1}, {100, 100}];


Let's pick one of the elements of this matrix. It will be the center (point of intersection of the diagonals) of the rectangle with sides (a, b) and the slope angle of the side 'b' alpha (0, 90 degrees):

For a -> 20, b -> 10, m -> 50, n -> 30, [Alpha] -> Pi/6:

The length of the side of the rectangle is the number (0, Dimensions [m1] [[1]]). The question is what elements of the matrix 'm1' are in the area of this rectangle, setting the result in the form of a matrix of numbers (rows and columns). If there are not all elements of columns or rows, the element in the matrix will be empty. We do not include rectangles that are outside the matrix 'm1':

For a -> 20, b -> 10, m -> 50, n -> 30, \[Alpha] -> Pi/6

,where gray dots are blank spaces in the matrix. By the way. Why are the corners not completely filled with red dots? Does it just seem so? In addition, the code takes into account incomplete rectangles, e.g. a -> 20, b -> 10, m -> 10, n -> 3, \ [Alpha] -> Pi / 6. Maybe such a limitation can be done by counting the area of a rectangle?

For better visualization, I will use an image of a house instead of a random matrix.

m1 = ImageData[
ColorConvert[
ImageResize[ExampleData[{"TestImage", "House"}], {100, 100}],
"Grayscale"]];

params = {a -> 30, b -> 60, m -> 30, n -> 50, α -> Pi/6};

pts = Catenate@Table[{i, j}, {i, 100}, {j, -1, -100, -1}];
reg = TranslationTransform[{m, -n}][
RotationTransform[α][Rectangle[{-a/2, -b/2}, {a/2, b/2}]]];

regMem = RegionMember[reg /. params];
ptsInside = Select[pts, regMem];
span = Span @@@ Sort@*Abs@*MinMax /@ Transpose@ptsInside;

m2 = ReplacePart[m1, {{i_, j_}} :> 0 /; Not@regMem[{i, -j}]];
m3 = Part[m2, Sequence @@ span];

Row[ArrayPlot[ReleaseHold@#, ImageSize -> 200, PlotRange -> All,
PlotLabel -> #] & /@ {HoldForm@m1, HoldForm@m2, HoldForm@m3}]


• >>>>> However, they are now presented simply as a 1D list, not yet as a matrix ...>>> That's not good. But, thanks for help! Commented Feb 10, 2022 at 16:44
• @ralph, honestly, I do not quite understand how you want torepresent this rectangle as a matrix. The elements are obviously not aligned into rows and columns – your image with orange rectangle and black dots is not an accurate representations. Take my image, and please show us how the red dots should be put in a matrix. Commented Feb 10, 2022 at 16:47
• Please see the update. Commented Feb 10, 2022 at 17:42
• @ralph, first of all, there are no missing corners in my image. Stare for a while at this image to see where the "illusion of missing corners" come from. Secondly, according to your update, you do not want your final matrix to be somehow re-rotated, but you just want to get the "bounding rectangle" (orange), keep the red elements, and set the grey elements to zero (see this image). This is now the matrix you want – am I correct? Commented Feb 10, 2022 at 18:14
• Yes. I give up the matrix rotation. My last image is ok. The border of the matrix where the gray elements are eg zeros, are the extreme red points of the rectangle. Commented Feb 10, 2022 at 18:56