package org.samcrow.vehicle;
import java.awt.Shape;
import java.awt.geom.AffineTransform;
import java.awt.geom.PathIterator;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.util.LinkedList;
import java.util.List;
import org.lekan.graphics.SGLine;
import org.lekan.graphics.SGObject;
/**
* Class for a wheel of a vehicle.
* @author Sam Crow
*
*/
public class Wheel {
protected static final double WIDTH = 5;
protected static final double LENGTH = 8;
protected double localX;
protected double localY;
/** The Vehicle that this Wheel is attached to */
protected Vehicle parent;
/**
* Construct a Wheel with a given location relative to the Vehicle origin
* @param inLocalX The X-axis (toward the right of the car) locaton relative to the car origin
* @param inLocalY The Y-axis (toward the front of the car) location relative to the car origin
* @param inParent the Vehicle that this Wheel is attached to
*/
public Wheel(double inLocalX, double inLocalY, Vehicle inParent){
localX = inLocalX;
localY = inLocalY;
parent = inParent;
}
/**
* Get this wheel's local X
* @return the X coordinate relative to the car origin
*/
public double getX(){
return localX;
}
/**
* Get this wheel's local Y
* @return the Y coordinate relative to the car origin
*/
public double getY(){
return localY;
}
/**
* Get the SGObjects necessary to draw this object
* @return a Collection of SGObjects to draw
*/
public List<SGObject> getDrawObjects(){
List<SGObject> objects = new LinkedList<SGObject>();
//for regular wheels: rotation is parent.getHeading()
final double heading = parent.getHeading();
//create a rectangle in the center of the vehicle facing north
final Rectangle2D.Double rect = new Rectangle2D.Double(
parent.getX() - WIDTH / 2d,
parent.getY() - LENGTH / 2d,
WIDTH,
LENGTH);
AffineTransform transform = new AffineTransform();
transform.translate(parent.getX(), parent.getY());
transform.rotate(Math.toRadians(heading));//rotate
transform.translate(-parent.getX(), -parent.getY());
transform.translate(localX, localY + LENGTH / 2);
final Shape result = transform.createTransformedShape(rect);
PathIterator iterator = result.getPathIterator(null);
Point2D.Double lastPoint = null;
while(!iterator.isDone()){//iterate through all points
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){
assert(lastPoint != null);//this must be after a point of type 0
SGLine line = new SGLine((int)Math.round(lastPoint.x), (int)Math.round(lastPoint.y), (int)Math.round(coords[0]), (int)Math.round(coords[1]));
objects.add(line);
lastPoint = new Point2D.Double(coords[0], coords[1]);
//System.out.println("Adding line "+line);
}
iterator.next();
}
return objects;
}
}