In this section we'll be looking at ways to detect intersection between various geometric objects.

This section will NOT include techniques to reacting to such intersections. The main goal of the functions found on this page will be to return, at most, two types of information.

- A True / False confirmation of whether or not there is intersection between two objecst.
- A value indicating to what degree / measure there is intersection.

## Line / Line

Remember, a "line" is a just a direction that extends infinitely, while a "line segment" is a section of a line that has a definitive beginning and end upon a line.

Detecting intersections of lines is easy enough. Any two lines that are **not parallel** to each other **must intersect** at some point in space.

## Line / Ray

In some cases you may wish to check for intersection on a line, but only for lines eminating from a singular origin, AKA a "ray".

There are a few reasons you might want this. If you are making an action game and need to track bullets fired from a gun you don't want to test for intersection that are *behind* the gun; you only want to test what is in *front* of the gun. In such a case a ray is the answer.

## Line / Square

Under construction.

## Line / Circle

Under construction.

## Circle / Circle

Testing for the intersection between two circles is another easy operation. Consider that a perfect circle has a constant radius, meaning the distance from the center point to the outer rim is constant no matter what direction you measure for that distance, we will only need the position and radius of each circle to get a distance for them.

At that point we ask a simple question:

Writing out such code is as easy as it sounds.

## Circle / Square

All this example does is test a circle against an axis aligned bounding box. AABB, meaning that the boxes are most likely straight up and down, left and right, or otherwise aligned with the world axis and not any local rotation.

```
var intersection1 = hit_circle_AABB( c1, r1, p1 );
// The actual intersection function...
function hit_circle_AABB ( circle_,
rectangle_,
point_
){
// Even if we weren't drawing a small point to show the intersection as
// seen in the interactive example we would need to store the point of
// intersection / proximity to compare it for later operations.
// Copy center point to work with...
point_.x = circle_.x;
point_.y = circle_.y;
// This code actually treats the circle as a square. We don't take
// the curvature into account until the difference check below.
if (circle_.x < rectangle_.x)
{
point_.x = rectangle_.x;
}
else if ( circle_.x > rectangle_.x + rectangle_.w )
{
point_.x = rectangle_.x + rectangle_.w ;
}
if ( circle_.y < rectangle_.y)
{
point_.y = rectangle_.y;
}
else if ( circle_.y > rectangle_.y + rectangle_.h )
{
point_.y = rectangle_.y + rectangle_.h;
}
// Now not unlike a distance check we need to find the difference
// in positions.
// Find the difference...
var difference_x = point_.x - circle_.x;
var difference_y = point_.y - circle_.y;
// And check for actual intersection.
// This is the part that accounts for the curavture of the circle.
if ( difference_x * difference_x +
difference_y * difference_y >
circle_.r * circle_.r ){
return false;
} else {
return true;
}
}
```

This code can be made much more concise if you have a Math library with "Max" and "Min" functions because they'll replace the if statements at the beginning of our original function. If you don't need the point information that can be removed as well.

The code below will return a simple "true" or "false" bool.

```
function hit_circle_AABB ( circle_, rectangle_ ){
var difference_x = circle_.x -
Math.max(rectangle_.x, Math.min(circle_.x, rectangle_.x + rectangle_.w));
var difference_y = circle_.y -
Math.max(rectangle_.y, Math.min(circle_.y, rectangle_.y + rectangle_.h));
return (difference_x * difference_x + difference_y * difference_y)
< (circle_.r * circle_.r);
}
```

## Square / Square

Under construction.

## Polygon / Polygon

Testing between two polygonal objects of either a convex or concave nature is without a doubt the most complex and taxing of all two-dimensional intersection checks.

Because of this, especially in a game, be careful about using such a check upon every update of the logic. Depending on the complexity of the polygon set it can easily slow down even fairly powerful and well optimized systems.