# Finding the area of the different portions of a rectangle that lie in a grid.

I am an undergraduate student working on a large research project and one part involves calculating the portions of a rectangle that lie in different parts of a cartesian grid. In the figure below, I am trying to finding the portion of the grey rectangle that lies in each of the cells. Assume $\phi \in (0, \pi/2)$, and the width $W \le 1$, and $Q \in [0,1)$ (where $Q$ is the point at half the width $W/2$). While $\phi$ is given here as an angle, I am not using it as an angle because this is has to be coded and trig is expensive (it is given as such for easy descriptions in the paper I am working on). Instead I use the normal, $\vec n = [x, y]: \| \vec n \| = 1$.

I have been working on this for a while, and I have a solution using many cases about where the corners lie and trigonometry to find these areas. However, this seems inefficient in retrospect (for example the image below which is a figure to describe finding the area in cell 2 given certain conditions - only the upper half of the rectangle is displayed for simplicity). I was wondering if anybody else had any ideas of methods that would be more elegant.

A couple other notes: The length is the distance from the entry point to the exit point of the center cell. I have a calculator programmed to give that, so in solutions you can just address it as $L$. Let $W$ be the entire width of the rectangle.

ADDITION: As addressed in the comments, the total area of the rectangle is $W \times L$. The ray is the at the center the of the rectangle with length $L$ and rectangles of area $LW/2$ on either side. Also, the middle-right cell is supposed to be cell 6; the centermost cell is cell 5.
The rectangle is developed by the following: Pick a point on the left hand boundary of the center cell. Now draw a line from that point given that the angle between the line and the $x$ axis is between 0 and $π/2$. Now where that line hits the boundary of the cell is where it stops. This is $L$. Now on either side on this line, there are 2 rectangles of size $LW/2$. I want the area of these rectangles that lies in each cell
• The width must be $\le 1$ but what is max height? Must part of the rectangle always be in the center square? – Jens May 5 '16 at 17:19
• Yes. $Q$ is the vertical distance from the entry point to the bottom of cell 5 (upper boundary of cells 7, 8, 9), and $Q \in [0,1)$. The max height is given by point where the vector exits the cell 5. – Meecolm May 5 '16 at 17:53
• I'm still confused. Does the rectangle not have some fixed width $W$ and some fixed height $H$? I saw the problem as randomly placing such a rectangle on your grid, with some part of the rectangle in the central cell, and then calculating how much of the rectangle's area was in each cell. – Jens May 5 '16 at 18:16
• The rectangle is developed by the following: Pick a point on the left hand boundary of the center cell. Now draw a line from that point given that the angle between the line and the $x$ axis is between 0 and $\pi/2$. Now where that line hits the boundary of the cell is where it stops. This is $L$. now on either side on this line, there are 2 rectangles of size $LW/2$. I want the area of these rectangles that lies in each cell. – Meecolm May 5 '16 at 18:26