Turtle

Class Turtle instances represent the dynamic, behavioral element of modeling. Each turtle knows the patch it is on, and interacts with that and other patches, as well as other turtles. Turtles are also the end points of Links.

You do not call new Turtle(), instead class Turtles creates Turtle instances via Turtles#create or Turtles#createOne

I.e. class Turtles is a factory for all of it's Turtle instances. So don't do this:

Constructor

new Turtle()

Members

Returns an array of the Links that have this Turtle as one of the end points

patch

Return the patch this Turtle is on. Return null if Turtle off-world.

heading

Return this Turtle's heading

heading

Sets this Turtle's heading

(readonly) dx

A property for the x-increment if the turtle were to take one step forward in its current heading.

(readonly) dy

A property for the y-increment if the turtle were to take one step forward in its current heading.

Methods

die()

Ask this turtle to "die"

  • Removes itself from the Turtles array
  • Removes itself from any Turtles breeds
  • Removes all my Links if any exist
  • Removes me from my Patch list of turtles on it
  • Set it's id to -1 to indicate to others it's gone

hatch(numopt, breedopt, initopt) → {Array}

Factory method: create num new turtles at this turtle's location.

Parameters:
NameTypeAttributesDefaultDescription
numnumber<optional>
1

The number of new turtles to create

breedAgentSet<optional>
this.agentSet

The type of turtles to create, defaults to my type

initfunction<optional>
turtle => {}

A function to initialize the new turtles, defaults to no-op

Returns:

An Array of the new Turtles, generally ignored due to the init function

Type: 
Array

subtractHeading(heading) → {Angle}

Computes the difference between the my heading and the given heading, the smallest angle by which t could be rotated to produce heading.

Parameters:
NameTypeDescription
headingAngle

The heading I wish to be roated to.

Returns:
Type: 
Angle

setxy(x, y, zopt)

Set Turtles x, y position. If z given, override default z of 0.

Parameters:
NameTypeAttributesDescription
xnumber

Turtle's x coord, a Float in patch space

ynumber

Turtle's Y coord, a Float in patch space

znumber | undefined<optional>

Turtle's Z coord if given

handleEdge(x, y, zopt)

Handle turtle x,y,z if turtle off-world. Uses the Turtle's atEdge property to determine how to manage the Turtle. Defaults to 'wrap', wrapping the x,y,z to the opposite edge.

atEdge can be:

  • 'die'
  • 'wrap'
  • 'bounce'
  • 'clamp'
  • 'random'
  • a function called with the Turtle as it's argument
Parameters:
NameTypeAttributesDescription
xnumber

Turtle's x coord

ynumber

Turtle's y coord

znumber | undefined<optional>

Turtle's z coord if not undefined

moveTo(agent)

Place the turtle at the given patch/turtle location

Parameters:
NameTypeDescription
agentPatch | Turtle

A Patch or Turtle who's location is used

forward(d)

Move forward, along the Turtle's heading d units in Patch coordinates

Parameters:
NameTypeDescription
dnumber

The distance to move

rotate(angle)

Change Turtle's heading by angle

Parameters:
NameTypeDescription
anglenumber

The angle to rotate by

Turn Turtle right by angle

Parameters:
NameTypeDescription
anglenumber

The angle to rotate by

left(angle)

Turn Turtle left by angle

Parameters:
NameTypeDescription
anglenumber

The angle to rotate by

face(agent)

Turn turtle so at to be facing the given Turtle or Patch

Parameters:
NameTypeDescription
agentPatch | Turtle

The agent to face towards

facexy(x, y)

Turn turtle so at to be facing the given x, y patch coordinate

Parameters:
NameTypeDescription
xnumber

The x coordinate

ynumber

The y coordinate

patchAhead(distance) → {Patch|undefined}

Return the patch ahead of this turtle by distance. Return undefined if the distance puts the patch off-world

Parameters:
NameTypeDescription
distancenumber

The distance ahead

Returns:

The patch at the distance ahead of this Turtle

Type: 
Patch | undefined

patchRightAndAhead(angle, distance) → {Patch|undefined}

Return the patch angle to the right and ahead by distance Return undefined if the distance puts the patch off-world

Parameters:
NameTypeDescription
anglenumber

The angle to the right

distancenumber

The distance ahead

Returns:

The patch found, or undefined if off-world

Type: 
Patch | undefined

patchLeftAndAhead(angle, distance) → {Patch|undefined}

Return the patch angle to the left and ahead by distance Return undefined if the distance puts the patch off-world

Parameters:
NameTypeDescription
anglenumber

The angle to the left

distancenumber

The distance ahead

Returns:

The patch found, or undefined if off-world

Type: 
Patch | undefined

canMove(distance) → {Boolean}

Can I move forward by distance and not be off-world?

Parameters:
NameTypeDescription
distancenumber

The distance ahead

Returns:

True if moving forward by distance is on-world

Type: 
Boolean

distanceXY(x, y, zopt) → {number}

Distance from this turtle to x, y No off-world test done.

2.5D: use z too if both z & this.z exist.

Parameters:
NameTypeAttributesDefaultDescription
xnumber
ynumber
znumber | undefined<optional>
null
Returns:

distance in patch coordinates.

Type: 
number

distance(agent) → {number}

Return distance from me to the Patch or Turtle

2.5D: use z too if both agent.z and this.z exist

Parameters:
NameTypeDescription
agentPatch | Turtle
Returns:

distance in patch coordinates.

Type: 
number

towards(agent) → {number}

Return the heading towards the Patch or Turtle given.

Parameters:
NameTypeDescription
agentPatch | Turtle

The agent who's angle from this Turtle we use

Returns:

The angle towards the agent

Type: 
number

towardsXY(x, y) → {number}

Return the heading towards the given x,y coordinates.

Parameters:
NameTypeDescription
xnumber

The x coordinarte

ynumber

The y coordinarte

Returns:

The angle towards x,y

Type: 
number

patchAt(dx, dy) → {Patch|undefined}

The patch at dx, dy from my current position. Return undefined if off-world

Parameters:
NameTypeDescription
dxnumber

The delta x ahead

dynumber

The delta y ahead

Returns:

The patch dx, dy ahead; undefined if off-world

Type: 
Patch | undefined

patchAtHeadingAndDistance(heading, distance) → {Patch|undefined}

Return the patch at the absolute, not relative heading and distance from this turtle. Return undefined if off-world

Use the Left/Right versions for relative heading.

Parameters:
NameTypeDescription
headingnumber

The absolute angle from this turtle

distancenumber

The distance ahead

Returns:

The Patch, or undefined if off-world

Type: 
Patch | undefined

otherEnd(l) → {Turtle}

Return the other end of this link from me. Link must include me!

See links property for all my links, if any.

Parameters:
NameTypeDescription
lLink
Returns:

The other turtle making this Link

Type: 
Turtle

linkNeighbors() → {Array}

Return all turtles linked to me. Basically me.otherEnd of all my links.

Returns:

All the turtles linked to me

Type: 
Array

isLinkNeighbor(t) → {Boolean}

Is the given Turtle linked to me?

Parameters:
NameTypeDescription
tTurtle
Returns:
Type: 
Boolean