pturtle
Class Turtle

java.lang.Object
  extended by pturtle.Turtle
Direct Known Subclasses:
HistoryTurtle

public class Turtle
extends java.lang.Object

Turtle class, making ideas from the LOGO teaching language available in Processing.

Author:
George Bashi, Ollie Glass

Field Summary
 processing.core.PApplet applet
          The PApplet to render to.
 int drawColor
          Colour of line drawn by Turtle (as a Processing color).
 boolean drawing
          If false, the Turtle moves but does not leave a trail.
 int x
          x location on screen.
 int y
          y location on screen.
 
Constructor Summary
Turtle(processing.core.PApplet applet)
          Standard constructor, creates a Turtle in the middle of the screen which draws in white.
Turtle(Turtle t)
          "Copy" constructor, creates an identical Turtle to the one passed in.
 
Method Summary
 void backward(int amount)
          Move Turtle backward.
 void forward(int amount)
          Move Turtle forward.
 int getDistance(int otherX, int otherY)
          Get the distance between this Turtle and point (x,y).
 int getDistance(Turtle t)
          Get the distance between this Turtle and another.
 Turtle getNearest(java.util.List<Turtle> turtles)
          Get the nearest (Euclidean distance) Turtle from a List.
 int getRotation()
          Get the angle that the Turtle is facing.
 void left(int amount)
          Turn the Turtle left.
 void moveToward(int toX, int toY, float amount)
          Move the Turtle toward point (x,y).
 void moveToward(int toX, int toY, int amount)
          Move the Turtle a specified number of pixels toward point (x,y).
 void moveToward(Turtle t, float amount)
          Move Turtle toward another.
 void moveToward(Turtle t, int amount)
          Move Turtle toward another.
 void randomPenColor()
          Randomise the colour that the Turtle draws with from a set of 16 hard-coded colours.
 void right(int amount)
          Turn the Turtle right.
 void setLocation(int x, int y)
          Move the Turtle to an absolute location.
 void setPenColor(int color)
          Set the colour the Turtle draws with.
 void setPenColor(int r, int g, int b)
          Set the colour the Turtle draws with.
 void setRotation(int rotation)
          Set the direction the Turtle is facing in to an absolute angle.
 void strafeLeft(int amount)
          Strafe the Turtle left.
 void strafeRight(int amount)
          Strafe the Turtle right.
 java.lang.String toString()
          Convert the Turtle to a String representation
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

x

public int x
x location on screen.


y

public int y
y location on screen.


drawColor

public int drawColor
Colour of line drawn by Turtle (as a Processing color).


drawing

public boolean drawing
If false, the Turtle moves but does not leave a trail.


applet

public final processing.core.PApplet applet
The PApplet to render to.

Constructor Detail

Turtle

public Turtle(processing.core.PApplet applet)
Standard constructor, creates a Turtle in the middle of the screen which draws in white.

Parameters:
applet - PApplet to render to.

Turtle

public Turtle(Turtle t)
"Copy" constructor, creates an identical Turtle to the one passed in.

Parameters:
t - Turtle to copy.
Method Detail

backward

public void backward(int amount)
Move Turtle backward.

Parameters:
amount - number of pixels to move by.

forward

public void forward(int amount)
Move Turtle forward.

Parameters:
amount - number of pixels to move by.

getDistance

public int getDistance(int otherX,
                       int otherY)
Get the distance between this Turtle and point (x,y).

Parameters:
otherX - location in x axis.
otherY - location in y axis.
Returns:
distance in pixels.

getDistance

public int getDistance(Turtle t)
Get the distance between this Turtle and another.

Parameters:
t - the other turtle.
Returns:
distance in pixels.

getNearest

public Turtle getNearest(java.util.List<Turtle> turtles)
Get the nearest (Euclidean distance) Turtle from a List.

Parameters:
turtles - the list.
Returns:
the nearest turtle.

getRotation

public int getRotation()
Get the angle that the Turtle is facing.

Returns:
angle in degrees.

left

public void left(int amount)
Turn the Turtle left.

Parameters:
amount - angle in degrees.

moveToward

public void moveToward(int toX,
                       int toY,
                       float amount)
Move the Turtle toward point (x,y).

Parameters:
toX - location in x axis.
toY - location in y axis.
amount - value between 0 and 1 as a ratio of how close to move toward point (x,y); 0 will not move the turtle, 1 will cause it to jump straight to (x,y), 0.5f will cause it to move half way there, etc.

moveToward

public void moveToward(int toX,
                       int toY,
                       int amount)
Move the Turtle a specified number of pixels toward point (x,y).

Parameters:
toX - location in x.
toY - location in y.
amount - number of pixels to move toward (x,y).

moveToward

public void moveToward(Turtle t,
                       float amount)
Move Turtle toward another.

Parameters:
t - Turtle to move towards.
amount - value between 0 and 1 as a ratio of how close to move toward point (x,y); 0 will not move the turtle, 1 will cause it to jump straight to (x,y), 0.5f will cause it to move half way there, etc.
See Also:
moveToward(int, int, float)

moveToward

public void moveToward(Turtle t,
                       int amount)
Move Turtle toward another.

Parameters:
t - Turtle to move towards.
amount - number of pixels to move toward (x,y).
See Also:
moveToward(int, int, int)

randomPenColor

public void randomPenColor()
Randomise the colour that the Turtle draws with from a set of 16 hard-coded colours.


right

public void right(int amount)
Turn the Turtle right.

Parameters:
amount - angle in degrees.

setLocation

public void setLocation(int x,
                        int y)
Move the Turtle to an absolute location. This does not draw.

Parameters:
x - location in x axis.
y - location in y axis.

setPenColor

public void setPenColor(int color)
Set the colour the Turtle draws with.

Parameters:
color - a colour created with PApplet.color(int, int, int).

setPenColor

public void setPenColor(int r,
                        int g,
                        int b)
Set the colour the Turtle draws with.

Parameters:
r - red value, 0-255.
g - green value, 0-255.
b - blue value, 0-255.

setRotation

public void setRotation(int rotation)
Set the direction the Turtle is facing in to an absolute angle.

Parameters:
rotation - angle in degrees.

strafeLeft

public void strafeLeft(int amount)
Strafe the Turtle left.

Parameters:
amount - number of pixels to strafe Turtle.

strafeRight

public void strafeRight(int amount)
Strafe the Turtle right.

Parameters:
amount - number of pixels to strafe Turtle.

toString

public java.lang.String toString()
Convert the Turtle to a String representation

Overrides:
toString in class java.lang.Object
Returns:
"Turtle at 100,100"


processing library PTurtle by George Bashi and Ollie Glass. (c) 2007