Note that there are some explanatory texts on larger screens.

plurals
  1. POFinding the overlapping area of two rectangles (in C#)
    primarykey
    data
    text
    <p>Edit:</p> <p>Simple code I used to solve the problem in case anyone is interested (thanks to Fredrik):</p> <pre><code> int windowOverlap(Rectangle rect1, Rectangle rect2) { if (rect1.IntersectsWith(rect2)) { Rectangle overlap = Rectangle.Intersect(rect1, rect2); if (overlap.IsEmpty) return overlap.Width * overlap.Height; } return 0; } </code></pre> <p>Original Question:</p> <p>I'd like to know a quick and dirty way to check if two rectangles overlap and if they do calculate the area of the overlap. For curiosities sake I'm interested in the case where 1) all the lines in both rectangles are either vertical or horizontal or 2) the general case for any two rectangles, but the only answer I really need is case 1.</p> <p>I'm thinking along the lines of:</p> <pre><code>double areaOfOverlap( Rect A, Rect B) { if ( A.Intersects(B) ) { // calculate area // return area } return 0; } </code></pre> <p>For A.Intersects() I was thinking of using the separating axis test, but if the rectangles have only horizontal and vertical lines is there an even simpler (faster) way to check?</p> <p>And for calculating the area where they intersect is there an quick way to do it if the rectangles only horizontal and vertical lines?</p> <p>Finally, this is unrelated to the question but I'd appreciate any advice someone may have on a good book / webpage where I could review the math for computer graphics. I've been out of college for a while and feel like I'm forgetting everything :)! Anyone else have that problem?</p> <p>( NOTE: I found this question different than <a href="https://stackoverflow.com/questions/244452/what-is-an-efficient-algorithm-to-find-area-of-overlapping-rectangles">this</a> which seems more complicated and doesn't directly answer the question. )</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload