import java.lang.Math;
// TurtleGraphics now works entirely with TGPoints - points in
// TurtleSpace.
//
// Early versions of TG used int primitive values for points.
// This approach led to too many visual problems due to
// propagation of round offs. Class TGPoint isolates actual
// implementation of points to one place which can easily be
// changed.
//
// A TGPoint is not bound to the current TGCanvas display. It
// is in TurtleSpace, which can be out of the bounds of what
// is currently viewable in the graphics sub-window of TG.
public class TGPoint implements Cloneable
{
private float x, y;
//
// constructors
//
TGPoint() { x = 0.0F; y = 0.0F; }
TGPoint( double x, double y ) { this.x = (float)x; this.y = (float)y; }
TGPoint( float x, float y ) { this.x = x; this.y = y; }
TGPoint( int x, int y ) { this.x = (float)x; this.y = (float)y; }
public int canvasX( int canvasWidth )
{
int xCenter = canvasWidth / 2;
int xInt =xIntValue();
return xInt + xCenter;
} // end canvasX()
public int canvasX( float adj, int canvasWidth )
{
int xCenter = canvasWidth / 2;
int xInt = (int) Math.rint( x + adj );
return xInt + xCenter;
} // end canvasX()
public int canvasY( int canvasHeight )
{
int yCenter = canvasHeight / 2;
int yInt = yIntValue();
return yCenter - yInt;
} // end canvasY()
public int canvasY( float adj, int canvasHeight )
{
int yCenter = canvasHeight / 2;
int yInt = (int) Math.rint( y + adj );
return yCenter - yInt;
} // end canvasY()
public Object clone()
{
try { return super.clone(); }
catch (CloneNotSupportedException e) { /* impossible error */ }
return new TGPoint( x, y );
}
// given one end point of a line, its length and heading (in radians),
// return its other end point
//
public TGPoint otherEndPoint( double radians, double length )
{
//System.out.print("otherEndPoint: p1=" + toString() );
//System.out.println(", radians=" + radians + ", len=" + length);
double sine = Math.sin(radians);
double cosine = Math.cos(radians);
double deltaX = cosine * length;
double deltaY = sine * length;
TGPoint p2 = new TGPoint( x + deltaX, y + deltaY );
//System.out.println(" p2=" + p2);
return p2;
} // end otherEndPoint()
public float xFloatValue()
{ return x; }
public int xIntValue()
{ return (int) Math.rint( x ); }
public float yFloatValue()
{ return y; }
public int yIntValue()
{ return (int) Math.rint( y ); }
public String toString()
{ return "{" + x + "," + y + "}"; }
} // end class TGPoint