コード例 #1
0
bool 
collides( const Circle & c, const LineSegment & l)
{
  
  Vector2 posToCenter = c.GetCenter() - l.GetStart();
  Vector2 dirVec      = l.GetEnd()    - l.GetStart();

  float segmentLength = dirVec.Length();
  // direction vector must be unit vector (ie. length = 1) in this case!
  // otherwise we would need another formula for scalar projection.
  dirVec = dirVec / segmentLength; 

  // scalar projection of posToCenter to direction vector.
  float d = dirVec.Dot(posToCenter);

  // if d value exceeds original segment length, then we put a cap on it.
  // if these two lines are dismissed, then algorithm sees line segment 
  // as a infinite line.
  if ( d >  segmentLength ) d = segmentLength;
  if ( d < -segmentLength ) d = -segmentLength;
  
  // compute closest point to circle center from line start 
  // along direction vector.
  Vector2 closest_point =l.GetStart() + dirVec * d;

  // vectorfrom circle center to closest point on line
  Vector2 S = closest_point - c.GetCenter();

  return (S.Length() <= c.GetRadius());
}
コード例 #2
0
void
draw_line( SDL_Renderer *renderer, LineSegment & l, int r, int g, int b, int a )
{
  // just to make compiler happy
  int x1 = static_cast<int>(l.GetStart().GetX());
  int y1 = static_cast<int>(l.GetStart().GetY());
  int x2 = static_cast<int>(l.GetEnd().GetX());
  int y2 = static_cast<int>(l.GetEnd().GetY());
  
  lineRGBA( renderer, x1,y1,x2,y2, r,g,b,a);
}