oh yeah, haven't really though of that case

... but I still think it can be done faster then with shape intersection. I'll take a look at the formula.
Circle.contains(Line):
Code:
/**
* Check if circle contains the line
* @param line Line to check against
* @return True if line inside circle
*/
private boolean contains(Line line) {
return contains(line.getX1(), line.getY1()) && contains(line.getX2(), line.getY2());
}
edit:
I've did the formula, it's much faster.
Circle.intersects(Line):
Code:
/**
* Check if circle touches a line.
* @param other The line to check against
* @return True if they touch
*/
private boolean intersects(Line other) {
// put it nicely into vectors
Vector2f lineSegmentStart = new Vector2f(other.getX1(), other.getY1());
Vector2f lineSegmentEnd = new Vector2f(other.getX2(), other.getY2());
Vector2f circleCenter = new Vector2f(getCenterX(), getCenterY());
// calculate point on line closest to the circle center and then
// compare radius to distance to the point for intersection result
Vector2f closest;
Vector2f segv = lineSegmentEnd.copy().sub(lineSegmentStart);
Vector2f ptv = circleCenter.copy().sub(lineSegmentStart);
float segvLength = segv.length();
float projvl = ptv.dot(segv) / segvLength;
if (projvl < 0)
{
closest = lineSegmentStart;
}
else if (projvl > segvLength)
{
closest = lineSegmentEnd;
}
else
{
Vector2f projv = segv.copy().scale(projvl / segvLength);
closest = lineSegmentStart.copy().add(projv);
}
boolean intersects = circleCenter.copy().sub(closest).lengthSquared() <= getRadius()*getRadius();
return intersects;
}
}
Line.intersects() override:
Code:
public boolean intersects(Shape shape)
{
if (shape instanceof Circle)
{
return shape.intersects(this);
}
return super.intersects(shape);
}