Find the intersection of two rectangles

C

cai_rongxi

Hi,

Can some body share the code to find the intersection of two
rectangles?

Thanks in advance
 
T

Thomas Matthews

Hi,

Can some body share the code to find the intersection of two
rectangles?

Thanks in advance

You may want to look at alt.sources.wanted.

If two rectangles intersect, then at least one line
segment from one rectangle must have a point in common
with another line segment. This can be found algebraically.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
 
I

Ivan Vecerina

Can some body share the code to find the intersection of two
rectangles?
Unless the rectangles are axis-aligned, you're pretty much in
the general case of Convex Polygon Intersection (--> Google).

If your rectangles are given in 2D as Top,Left,Bottom,Right
coordinates, as typical for GUI programming, then it's simply:
intersect.Left = max(a.Left, b.Left);
intersect.Top = max(a.Top, b.Top);
intersect.Right = min(a.Right, b.Right );
intersect.Bottom = min(a.Bottom, b.Bottom);
And the intersection is empty unless
intersect.Right > intersect.Left
&& intersect.Bottom > intersect.Top


hth-Ivan
 
P

prabhat143

Assume :

Rect A and Rect B. Upper Left(ul) and Lower Right(lr) co-ords are
given. The following function will return true if they intersect.

int areIntersecting(Rect A, Rect B) {
return ( ! ( (A.ul.x > B.lr.x) || (B.ul.x > A.lr.x) || (A.ul.y
< B.lr.y) || (B.ul.y < A.lr.y))
}

You can obviously use DeMorgan's law to remove the negation operator.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top