Skip to content

Commit

Permalink
Add 2D ray-circle intersection methods
Browse files Browse the repository at this point in the history
  • Loading branch information
hyazinthh committed Nov 13, 2024
1 parent a1dc671 commit e5914a6
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 0 deletions.
1 change: 1 addition & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
- Added Ray3.Normalized
- Added Vec.AngleBetweenSigned for 2D vectors
- Added Fun.WrapToPi
- Added 2D ray-circle intersection methods

### 5.3.5
- [Base] added IsEmpty/IsEmptyOrNull overloads for Array/ICollection with efficient implementation
Expand Down
72 changes: 72 additions & 0 deletions src/Aardvark.Base/Geometry/IntersectionTests_auto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -848,6 +848,42 @@ public static bool Intersects(this Ray2f r0, Ray2f r1, out float t)

#endregion

#region Ray2f intersects Circle2f

/// <summary>
/// Computes the intersection between the given <see cref="Ray2f"/> and <see cref="Circle2f"/>.
/// </summary>
/// <param name="ray">The ray to intersect.</param>
/// <param name="circle">The circle to intersect the ray with.</param>
/// <param name="t0">The ray parameter of the first intersection. Set to infinity if there is no intersection.</param>
/// <param name="t1">The ray parameter of the second intersection. Set to infinity if there is no intersection.</param>
/// <returns>True if there is an intersection, false otherwise.</returns>
public static bool Intersects(this Ray2f ray, Circle2f circle, out float t0, out float t1)
{
var p = ray.Origin - circle.Center;
var a = ray.Direction.X.Square() + ray.Direction.Y.Square();
var b = 2 * ray.Direction.X * p.X + 2 * ray.Direction.Y * p.Y;
var c = p.X.Square() + p.Y.Square() - circle.Radius.Square();
var d = b.Square() - 4 * a * c;

if (d < 0)
{
t0 = float.PositiveInfinity;
t1 = float.PositiveInfinity;
return false;
}
else
{
var div = 1 / (a + a);
d = d.Sqrt();
t0 = (-b + d) * div;
t1 = (-b - d) * div;
return true;
}
}

#endregion


#region Plane2f intersects Line2f

Expand Down Expand Up @@ -5024,6 +5060,42 @@ public static bool Intersects(this Ray2d r0, Ray2d r1, out double t)

#endregion

#region Ray2d intersects Circle2d

/// <summary>
/// Computes the intersection between the given <see cref="Ray2d"/> and <see cref="Circle2d"/>.
/// </summary>
/// <param name="ray">The ray to intersect.</param>
/// <param name="circle">The circle to intersect the ray with.</param>
/// <param name="t0">The ray parameter of the first intersection. Set to infinity if there is no intersection.</param>
/// <param name="t1">The ray parameter of the second intersection. Set to infinity if there is no intersection.</param>
/// <returns>True if there is an intersection, false otherwise.</returns>
public static bool Intersects(this Ray2d ray, Circle2d circle, out double t0, out double t1)
{
var p = ray.Origin - circle.Center;
var a = ray.Direction.X.Square() + ray.Direction.Y.Square();
var b = 2 * ray.Direction.X * p.X + 2 * ray.Direction.Y * p.Y;
var c = p.X.Square() + p.Y.Square() - circle.Radius.Square();
var d = b.Square() - 4 * a * c;

if (d < 0)
{
t0 = double.PositiveInfinity;
t1 = double.PositiveInfinity;
return false;
}
else
{
var div = 1 / (a + a);
d = d.Sqrt();
t0 = (-b + d) * div;
t1 = (-b - d) * div;
return true;
}
}

#endregion


#region Plane2d intersects Line2d

Expand Down
36 changes: 36 additions & 0 deletions src/Aardvark.Base/Geometry/IntersectionTests_template.cs
Original file line number Diff line number Diff line change
Expand Up @@ -881,6 +881,42 @@ public static bool Intersects(this __ray2t__ r0, __ray2t__ r1, out __rtype__ t)

#endregion

#region __ray2t__ intersects __circle2t__

/// <summary>
/// Computes the intersection between the given <see cref="__ray2t__"/> and <see cref="__circle2t__"/>.
/// </summary>
/// <param name="ray">The ray to intersect.</param>
/// <param name="circle">The circle to intersect the ray with.</param>
/// <param name="t0">The ray parameter of the first intersection. Set to infinity if there is no intersection.</param>
/// <param name="t1">The ray parameter of the second intersection. Set to infinity if there is no intersection.</param>
/// <returns>True if there is an intersection, false otherwise.</returns>
public static bool Intersects(this __ray2t__ ray, __circle2t__ circle, out __rtype__ t0, out __rtype__ t1)
{
var p = ray.Origin - circle.Center;
var a = ray.Direction.X.Square() + ray.Direction.Y.Square();
var b = 2 * ray.Direction.X * p.X + 2 * ray.Direction.Y * p.Y;
var c = p.X.Square() + p.Y.Square() - circle.Radius.Square();
var d = b.Square() - 4 * a * c;

if (d < 0)
{
t0 = __rtype__.PositiveInfinity;
t1 = __rtype__.PositiveInfinity;
return false;
}
else
{
var div = 1 / (a + a);
d = d.Sqrt();
t0 = (-b + d) * div;
t1 = (-b - d) * div;
return true;
}
}

#endregion


#region __plane2t__ intersects __line2t__

Expand Down

0 comments on commit e5914a6

Please sign in to comment.