airesources/Ruby/hlt/map.rb
# Test whether a line segment and circle intersect.
# alpha: The start of the line segment. (Needs x, y attributes)
# omega: The end of the line segment. (Needs x, y attributes)
# circle: The circle to test against. (Needs x, y, r attributes)
# fudge: A fudge factor; additional distance to leave between the segment and circle.
#        (Probably set this to the ship radius, 0.5.)
# return: True if intersects, False otherwise
Test whether a line segment and circle intersect.
alpha: The start of the line segment. (Needs x, y attributes)
omega: The end of the line segment. (Needs x, y attributes)
circle: The circle to test against. (Needs x, y, r attributes)
fudge: A fudge factor; additional distance to leave between the segment and circle.
(Probably set this to the ship radius, 0.5.)
return: True if intersects, False otherwise
Test whether a line segment and circle intersect . alpha : The start of the line segment . ( Needs x y attributes ) omega : The end of the line segment . ( Needs x y attributes ) circle : The circle to test against . ( Needs x y r attributes ) fudge : A fudge factor ; additional distance to leave between the segment and circle . ( Probably set this to the ship radius 0 . 5 . ) return : True if intersects False otherwise
def intersect_segment_circle(alpha, omega, circle, fudge=0.5)
    dx = omega.x - alpha.x
    dy = omega.y - alpha.y
    a = dx**2 + dy**2
    b = -2 * (alpha.x**2 - alpha.x*omega.x - alpha.x*circle.x + omega.x*circle.x +
              alpha.y**2 - alpha.y*omega.y - alpha.y*circle.y + omega.y*circle.y)
    c = (alpha.x - circle.x)**2 + (alpha.y - circle.y)**2
    if a == 0.0
      return alpha.calculate_distance_between(circle) <= circle.radius + fudge
    end
    t = [-b / (2 * a), 1.0].min
    if t < 0
      return false
    end
    closest_x = alpha.x + dx * t
    closest_y = alpha.y + dy * t
    closest_distance = Position.new(closest_x, closest_y).calculate_distance_between(circle)
    return closest_distance <= circle.radius + fudge
  end
def intersect_segment_circle ( alpha , omega , circle , fudge = 0.5 ) dx = omega . x - alpha . x dy = omega . y - alpha . y a = dx ** 2 + dy ** 2 b = - 2 * ( alpha . x ** 2 - alpha . x * omega . x - alpha . x * circle . x + omega . x * circle . x + alpha . y ** 2 - alpha . y * omega . y - alpha . y * circle . y + omega . y * circle . y ) c = ( alpha . x - circle . x ) ** 2 + ( alpha . y - circle . y ) ** 2 if a == 0.0 return alpha . calculate_distance_between ( circle ) <= circle . radius + fudge end t = [ - b / ( 2 * a ) , 1.0 ] . min if t < 0 return false end closest_x = alpha . x + dx * t closest_y = alpha . y + dy * t closest_distance = Position . new ( closest_x , closest_y ) . calculate_distance_between ( circle ) return closest_distance <= circle . radius + fudge end
Test whether a line segment and circle intersect.
Test whether a line segment and circle intersect .
# Start and end are the same point # Time along segment when closest to the circle (vertex of the quadratic)
alpha
<not_specific>
omega
<not_specific>
circle
<not_specific>
fudge
<not_specific>