delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/10/01/08:11:06

Date: Wed, 1 Oct 1997 07:09:51 -0500 (CDT)
From: Andrew Deren <aderen AT eecs DOT uic DOT edu>
To: "Hargreaves, Shawn" <shawnh AT probe DOT co DOT uk>
cc: "'djgpp AT delorie DOT com'" <djgpp AT delorie DOT com>
Subject: Re: collision detection
In-Reply-To: <34327EAC@relay.probe.co.uk>
Message-ID: <Pine.SUN.3.95.971001070727.12351A-100000@ernie.eecs.uic.edu>
MIME-Version: 1.0

On Wed, 1 Oct 1997, Hargreaves, Shawn wrote:

> 
> Andrew Deren writes:
> > Does anyone know how to detect if two rectangles overlap each other at
> > some point. Let's say we have something like this:
> 
> The easiest way to solve this is to consider in what cases the rectangles   
> 
> would _not_ collide. For the range xa1-xa2 not to overlap with the range
> xb1-xb2, it must be either to the left of it or to the right of it (duh   
> :-) So,
> either the rightmost point of the first range must be to the left of the   
> leftmost
> point of the second range, or the leftmost point of the first range must   
> be
> to the right of the rightmost point of the second range. In code:
> 
>     ((xa2 < xb1) || (xb1 > xa2))
> 
> Negate that, and they collide if:
> 
>     ((xa2 >= xb1) && (xb1 <= xa2))
> 
> <I think: this is untested> :-) Repeat for the y coordinates, and you   
> have
> your intersection test...

Thanks a lot it works. Would you by any chance also know how to detect a
rectangle with a circle. Let's say we have:

typedef struct CIR {
   int x1, int y1;
   int radius;
} CIR;

and then do detect in RECT lies in some point in CIR. Any help would be
appreciated.

> 
>  Shawn Hargreaves.
> 


                        ,,,
                       (0 0)
   +-------------oOO----(_)-------------------+
   |                Andrew Deren              |  
   |             aderen AT eecs DOT uic DOT edu          |
   | www.eecs.uic.edu/~aderen/ader/main.html  |
   +-------------------------oOO--------------+
                       || ||
                      ooO Ooo
 


- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019