package org.samcrow.environment;
import java.awt.Shape;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.PathIterator;
import java.awt.geom.Point2D;
import org.lekan.graphics.SGEllipse;
import org.lekan.graphics.SGObject;
/**
* An obstacle in the shape of a circle
* @author Sam Crow
*
*/
public class CircleObstacle extends Obstacle {
private int radius;
private Ellipse2D.Double ellipse;
/**
* Construct a new circle obstacle.
* @param inX The X-axis location of the center
* @param inY The Y-axis location of the center
* @param inRadius The radius in pixels
*/
public CircleObstacle(int inX, int inY, int inRadius){
super(inX, inY);
ellipse = new Ellipse2D.Double(inX - inRadius, inY - inRadius, inRadius * 2, inRadius * 2);
radius = inRadius;
}
/* (non-Javadoc)
* @see org.samcrow.environment.Obstacle#collides(int, int)
*/
@Override
public boolean collides(double inX, double inY) {
return ellipse.contains(new Point2D.Double(inX, inY));
}
/* (non-Javadoc)
* @see org.samcrow.environment.Obstacle#drawObject()
*/
@Override
public SGObject drawObject() {
return new SGEllipse(x, y, radius, radius);
}
/* (non-Javadoc)
* @see org.samcrow.environment.Obstacle#collides(java.awt.Shape)
*/
@Override
public boolean collides(Shape shape) {
PathIterator iterator = shape.getPathIterator(null);
Point2D.Double lastPoint = null;
while(!iterator.isDone()){
double[] coords = new double[8];
int type = iterator.currentSegment(coords);
if(type == PathIterator.SEG_MOVETO){
lastPoint = new Point2D.Double(coords[0], coords[1]);
}else if(type == PathIterator.SEG_LINETO){
Point2D.Double thisPoint = new Point2D.Double(coords[0], coords[1]);
Line2D.Double line = new Line2D.Double(lastPoint, thisPoint);
if(line.ptSegDist(x, y) < radius){
return true;
}
lastPoint = thisPoint;
}
iterator.next();
}
return false;
}
}