unit UCollide;

interface

uses
  JEN_Math;

function LineVsCircle(const lp1, lp2, cp: TVec2f; Rad: Single): Boolean;
function CircleVsCircle(const cp1: TVec2f; Rad1: Single;const cp2: TVec2f; Rad2: Single): Boolean;

implementation


function LineVsCircle(const lp1, lp2, cp: TVec2f; Rad: Single): Boolean;
var
  p1, p2  : TVec2f;
  d  : TVec2f;
  a, b, c : Single;
begin
  p1 := lp1 - cp;
  p2 := lp2 - cp;

  d := p2- p1;

  a := d.LengthQ;
  b := 2.0 * (p1.x * d.x + p1.y * d.y);
  c := p1.LengthQ - Sqr(Rad);

  if -b < 0 Then
    Result := c < 0
  else
    if -b < (2.0 * a) Then
      Result := 4.0 * a * c - b * b  < 0
    else
      Result := a + b + c < 0;
end;

function CircleVsCircle(const cp1: TVec2f; Rad1: Single;const cp2: TVec2f; Rad2: Single): Boolean;
begin
    Result := (cp1 - cp2).Length <= Rad1 + Rad2;
end;


end.
