|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.ignotus.aifile.Turtle
public class Turtle
Implements a basic 2D TurtleGraphics command set that can be used to draw shapes in Processing and export them to Adobe Illustrator.
A turtle is an imaginary denizen of computer graphics that had its origins in the LOGO language, which was a subset of LISP used for teaching programming. LOGO was designed for children, but was a powerful language. Its model of a "local geometry" implemented through the orientation and location of the turtle offered many intuitive advantages over the "global geometry" used in most computer graphics packages.
A turtle is created facing right (east) at the center of the screen or at user-supplied coordinates.
It moves across the display upon receiving forward(double)
, backward(double)
,
move(double)
or moveTo(double, double)
commands, and changes its orientation
in response to left(double)
, right(double)
and turn(double)
commands.
It carries with it a "pen" that may be up or down. When the pen is down, the turtle leaves a
mark that can be displayed by calling the turtle's draw()
command from Processing's draw() method.
A shape (also referred to here as a "trail" or a "turtle trail") is created by moving the turtle
while the turtle pen is down (penDown()
command). A shape is finished by a penUp()
command.
Each shape is stored into an array, the turtleTrails array, which is also drawn by the draw()
method. Shapes in the turtleTrails array and the current trail can be deleted with the clear()
command.
The graphics state of trails/shapes is controlled through the commands in the ColorableINF interface,
which allow you to get and set fill color, stroke color, and stroke weight. The setNoFill()
and setNoStroke()
commands turn off filling and stroking of shapes. Commands apply
to the current trail or to new trails, but not to previously stored trails.
The location, orientation, pen state and visibility of the turtle are stored in a TurtleState object. The
turtle x-coordinate, turtle y-coordinate, orientation, pen state and visibility can be set directly, by
"divine intervention," bypassing the move, moveTo and turn commands. Turtle states can be pushed onto the
turtleStack (push(TurtleState)
, examined via peek()
and retrieved and deleted
with pop()
. The clearStack()
command clears the stored states.
Shapes can be written to an Adobe Illustrator 7.0 file with the write(PrintWriter)
command; however,
a preferred and simpler techniques is to call getTrails()
, which returns a GroupComponent that can be
added to a DocumentComponent or a LayerComponent. A DocumentComponent can handle the creation of an Illustrator
document and export geometry to it with its DocumentComponent.write(PrintWriter)
command.
DocumentComponent
,
LayerComponent
,
ColorableINF
Field Summary | |
---|---|
float |
r
turtle radius |
static double |
TWOPI
useful constant |
Constructor Summary | |
---|---|
Turtle(PApplet parent)
Instantiates a Turtle facing right in the center of Processing's display, ready to draw (pen down). |
|
Turtle(PApplet parent,
double x,
double y)
Instantiates a Turtle facing right at coordinates (x, y), ready to draw (pen down). |
Method Summary | |
---|---|
void |
backward(double distance)
Moves the turtle in the direction opposite to the current direction (i.e., -turtle angle) by a specified distance. |
void |
clear()
Clears all trails, both those saved in turtleTrails and the current trail. |
void |
clearStack()
Clears the turtleStack of all stored states. |
void |
draw()
Draws all trails, both those saved in turtleTrails and the current trail, to the display. |
void |
draw(PGraphics pg)
Draws all trails, both those saved in turtleTrails and the current trail, to an instonce of PGraphics used as an offscreen buffer. |
void |
drawCurrent()
Draws the current trail only to the display. |
int |
fillColor()
Returns the fill color of the current trail. |
int |
fillOpacity()
Returns opacity component of current fill color, in the range 0..255. |
void |
forward(double distance)
Moves the turtle in the current direction (turtle angle) by a specified distance. |
BezShape |
get(int index)
Gets trail (BezShape) at suppled index. |
BezShape |
getCurrentTrail()
|
int |
getMaxTrails()
|
int |
getTrailIndex()
|
GroupComponent |
getTrails()
Returns a GroupComponent with all the trails created by this Turtle instance. |
double |
getTurtleAngle()
Returns the current turtle angle. |
ArrayList<TurtleState> |
getTurtleStack()
Returns the turtle stack, an array of turtle states. |
ArrayList<BezShape> |
getTurtleTrails()
Returns the list of all stored turtle trails, not including the current trail if it is still being draw (i.e., no penUp() command has been issued to store it). |
PVector |
getTurtleVector()
Returns current turtle angle as a normalized PVector |
double |
getTurtleX()
Returns x-coordinate of turtle location. |
double |
getTurtleY()
Returns y-coordinate of turtle location. |
boolean |
hasFill()
Returns true if current trail is filled, false otherwise. |
boolean |
hasStroke()
Returns true if the current trail is stroked. |
void |
hide()
Gets ready to hide the visible representation of the turtle on the next draw command. |
void |
home()
returns the turtle to its original position with a turtle angle of 0 (facing right). |
boolean |
isClosed()
Returns current value of isClosed, true if current trail is a closed shape. |
boolean |
isEmpty()
Returns true if there are no shapes stored in turtleTrails array. |
boolean |
isFull()
Returns true if maximum number of trails have been stored in turtleTrails array.false otherwise. |
boolean |
isPenDown()
Returns true if the pen is down (ready to draw), false otherwise. |
boolean |
isTurtleVisible()
Returns true if turtle is visible, false otherwise. |
Iterator<BezShape> |
iterator()
Returns an iterator over the stored turtle trails. |
void |
left(double angle)
Turns the turtle to the left (counterclockwise) a specified number of radians from its current direction (turtle angle). |
void |
move(double distance)
Moves the turtle by a specified distance in the current direction (turtle angle). |
void |
moveTo(double x2,
double y2)
Moves the turtle to a specified point (x2, y2). |
TurtleState |
peek()
Retrieves, but does not remove, the most recently pushed turtle state, or returns null if turtleStack is empty. |
void |
penDown()
Prepares the turtle to create a trail on the next forward, backward, or move command. |
void |
penUp()
Stops drawing by the turtle and adds the current trail (if there is one) to the turtleTrails array. |
TurtleState |
pop()
Retrieves and removes the most recently pushed turtle state. |
void |
push(TurtleState state)
Pushes a turtle state onto the stack. |
void |
right(double angle)
Turns the turtle to the right (clockwise) a specified number of radians from its current direction (turtle angle). |
void |
setFillColor(int newFillColor)
Sets fill color of this Turtle instance. |
void |
setFillOpacity(int opacity)
Sets fill color opacity of this Turtle instance. |
void |
setIsClosed(boolean newIsClosed)
Sets isClosed of this Turtle instance to supplied value. |
void |
setMaxTrails(int maxTrails)
|
void |
setNoFill()
Sets hasFill of this Turtle instance to false. |
void |
setNoStroke()
Sets hasStroke of this Turtle instance to false. |
void |
setStrokeColor(int newStrokeColor)
Sets stroke color of this Turtle instance. |
void |
setStrokeOpacity(int opacity)
Sets opacity value of stroke color of this Turtle instance. |
void |
setTurtleAngle(double turtleAngle)
Sets the orientation of the turtle by divine intervention. |
void |
setTurtleAngle(PVector vec)
Sets the orientation of the turtle from a supplied PVector. |
void |
setTurtleX(double turtleX)
Sets the x-coordinate of the turtle by divine intervention. |
void |
setTurtleY(double turtleY)
Sets the y-coordinate of the turtle by divine intervention. |
void |
setWeight(float newWeight)
Sets value of stroke weight of this Turtle instance. |
void |
show()
Gets ready to show the turtle as an oriented triangle on the next draw command. |
int |
size()
Returns number of elements in the turtleTrails array. |
int |
stackSize()
Returns the number of elements in the turtle state stack. |
int |
strokeColor()
Returns current stroke color. |
int |
strokeOpacity()
Returns opacity component of current stroke color, in the range 0..255. |
int |
trailSize()
Returns number of vertices in the current trail. |
void |
turn(double angle)
Turns the turtle a specified number of radians from its current direction (turtle angle). |
float |
weight()
Returns weight of current stroke. |
void |
write(PrintWriter pw)
Writes the geometry of all trails, both those saved in turtleTrails and the current trail, in Adobe Illustrator 7.0 format, to the file or output stream specified. |
Methods inherited from class java.lang.Object |
---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public float r
public static double TWOPI
Constructor Detail |
---|
public Turtle(PApplet parent)
parent
- a PApplet, typically a reference to the host applet, as in Turtle t = new Turtle(this);
public Turtle(PApplet parent, double x, double y)
parent
- a PApplet, typically a reference to the host applet, as in Turtle t = new Turtle(this, x, y);
Method Detail |
---|
public void show()
public void hide()
public void penUp()
public void penDown()
public void move(double distance)
distance
- public void moveTo(double x2, double y2)
x2
- the x-coordinate of the point to move toy2
- the y-coordinate of the point to move topublic void forward(double distance)
distance
- public void backward(double distance)
distance
- public void turn(double angle)
angle
- public void left(double angle)
angle
- public void right(double angle)
angle
- public void home()
public void clear()
public void draw()
public void draw(PGraphics pg)
pg
- a PGraphics instance.public void drawCurrent()
public void write(PrintWriter pw)
getTrails
to a
Document component, as shown in the examples.
pw
- the output stream (file) to write to.public GroupComponent getTrails()
LayerComponent.add(DisplayComponent)
,
DocumentComponent.add(DisplayComponent)
public ArrayList<TurtleState> getTurtleStack()
TurtleState
public ArrayList<BezShape> getTurtleTrails()
public int getMaxTrails()
public void setMaxTrails(int maxTrails)
maxTrails
- the maxTrails to setpublic int getTrailIndex()
public BezShape getCurrentTrail()
public double getTurtleAngle()
TurtleState.getTurtleAngle()
public PVector getTurtleVector()
TurtleState.turtleVector()
public void setTurtleAngle(double turtleAngle)
turtleAngle
- the angle the turtle is facingTurtleState.setTurtleAngle(double)
public void setTurtleAngle(PVector vec)
vec
- a PVectorTurtleState.setTurtleAngle(PVector)
public boolean isPenDown()
TurtleState.isPenDown()
public boolean isTurtleVisible()
TurtleState.isTurtleVisible()
public double getTurtleX()
TurtleState.getTurtleX()
public void setTurtleX(double turtleX)
moveTo
instead to do drawing.
turtleX
- x-coordinate to assign to the turtle's locationTurtleState.setTurtleX(double)
public double getTurtleY()
TurtleState.getTurtleY()
public void setTurtleY(double turtleY)
moveTo
instead to do drawing.
turtleY
- y-coordinate to assign to the turtle's locationTurtleState.setTurtleY(double)
public TurtleState peek()
ArrayList.get(int)
public TurtleState pop()
ArrayList.remove(int)
public void push(TurtleState state)
state
- the state to pushArrayList.add(java.lang.Object)
public int stackSize()
ArrayList.size()
public void clearStack()
public boolean isClosed()
BezShape.isClosed()
public void setIsClosed(boolean newIsClosed)
newIsClosed
- BezShape.setIsClosed(boolean)
public boolean hasFill()
hasFill
in interface ColorableINF
BezShape.hasFill()
public int fillColor()
fillColor
in interface ColorableINF
BezShape.fillColor()
public void setFillColor(int newFillColor)
setFillColor
in interface ColorableINF
newFillColor
- BezShape.setFillColor(int)
public void setNoFill()
setNoFill
in interface ColorableINF
BezShape.setNoFill()
public boolean hasStroke()
hasStroke
in interface ColorableINF
BezShape.hasStroke()
public int strokeColor()
strokeColor
in interface ColorableINF
BezShape.strokeColor()
public void setStrokeColor(int newStrokeColor)
setStrokeColor
in interface ColorableINF
newStrokeColor
- BezShape.setStrokeColor(int)
public void setNoStroke()
setNoStroke
in interface ColorableINF
BezShape.setNoStroke()
public int fillOpacity()
fillOpacity
in interface ColorableINF
BezShape.fillOpacity()
public void setFillOpacity(int opacity)
setFillOpacity
in interface ColorableINF
opacity
- opacity to set (0..255)BezShape.setFillOpacity(int)
public int strokeOpacity()
strokeOpacity
in interface ColorableINF
BezShape.strokeOpacity()
public void setStrokeOpacity(int opacity)
setStrokeOpacity
in interface ColorableINF
opacity
- opacity to set (0..255)BezShape.setStrokeOpacity(int)
public float weight()
weight
in interface ColorableINF
public void setWeight(float newWeight)
setWeight
in interface ColorableINF
newWeight
- BezShape.setWeight(float)
public int trailSize()
public BezShape get(int index)
index
-
ArrayList.get(int)
public boolean isEmpty()
ArrayList.isEmpty()
public boolean isFull()
public Iterator<BezShape> iterator()
AbstractList.iterator()
public int size()
ArrayList.size()
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |