Re: Simple question



On Oct 17, 1:07 am, Logan Shaw <lshaw-use...@xxxxxxxxxxxxx> wrote:
guitarstru...@xxxxxxxxx wrote:
I need some code to detect the collision between a square and a
circle. Almost any language will be acceptable. Thanks!

Break this up into three cases.

Case 1: the center of the circle is inside the square.
-> You always have a collision.

Case 2: the center of the circle is within the vertical range of
the square or within its horizontal range. In other words, you
could draw a horizontal or vertical line through the center of
the circle and it would pass through the square.
-> Measure the distance from the center of the circle to
the nearest (perpendicular) edge; if it's less than the
radius of the circle, you have a collision.

Case 3: neither #1 or #2 is true, so the center of the circle is
off at a diagonal.
-> Measure the distance from the center of the circle to
the closest corner of the square; if it is less than the
radius of the circle, you have a collision.

Note the difference between #2 and #3 is that in #2 you are finding
a distance along a horizontal or vertical line, so you only need to
subtract two X coordinates or subtract two Y coordinates, whereas in
#3 you are using the Pythagorean Theorem.

To see why all this works, imagine rolling the circle around the
square. At each point, where is the center of the circle, and what
is the closest point on the square? It seems fairly easy to see
that the time when the corner becomes the closest point is the time
when the center of the circle is on the line formed by one of the
edges of the square.

- Logan

Excellent, except OP did not say the square was axis-aligned. You can
either transform the problem so it is, or do the tests in problem
space. You can determine if a point P is left of a line segment from
A to B by testing

(P-A) dot perp(B-A) > 0 and (P-B) dot (A-B) > 0 and (P-A) dot (B-A) >
0

Where perp([x,y]) = [-y,x].

This is enough to determine the cases you've described very well.

One other thing is that the idea of sliding one convex polygon around
the border of another (without rotating, though in this case it
doesn't matter) and considering the area enclosed by some reference
point on the sliding polygon (in your case the center of the circle),
is a good one. It's called a Minkowski difference. The problem of
determining if two convex polygons intersect is equivalent to
determining if the reference point lies in the Minkowski difference.
For this problem, the difference is the square "blown" up by the
circle radius on all sides, with corners rounded. We need to test if
the circle center lies inside this oval shape, which is equivalent to
what you've described.






.



Relevant Pages

  • Re: Help a poor FORTRAN programmer with member functions?
    ... void* should be avoided, and is ... the base class for you other shapes. ... class Square: public Shape ... Circle to give their own version of Printthat does what they need it ...
    (comp.lang.cpp)
  • Re: Can I make something circular?
    ... Everything in Publisher is square. ... Keep your design inside the circle, ... I have used Neato labels and program. ... I've been looking into printing labels directly on discs (my Canon pixma ...
    (microsoft.public.publisher)
  • Re: Any one know of a circle cutting jig for bandsaw, or table saw?
    ... Cut a square from your stock on the table saw so that its side is ... slightly longer than the diameter of your circle. ... that fits snugly in the hole but turns freely, screw the square to ... Raise the blade and cut the corners off the square by running ...
    (rec.woodworking)
  • Re: Connecting semi-circles
    ... 'squaring the circle', or its inverse 'circling the square' ... continuous surface area and then moving it to a square with a perfect, ... There is no way to get a 'smooth corner.' ... Is it possible to create two semicircles so that: ...
    (sci.math)
  • Re: flattening/rolling up/aggregating a large sorted text file
    ... combination of dimension level, and summing up measures over all ... 001, blue, square, 4 ... 001, red, circle, 5 ... def bitwise_categorize: ...
    (comp.lang.python)