Message Boards Message Boards

1
|
3640 Views
|
8 Replies
|
6 Total Likes
View groups...
Share
Share this post:

Is there an easy way for multiple turtles analog?

Posted 2 years ago

I am interested in intrinsic geometry. For this reason I work with multiple turtles (turtle geometry). In JULIA this is easy with mutable struct: enter image description here

So it's possible to have t1=Turtle(0,...(1,0,0)) or t2=Turtle(1.0,...,(1,1,0)) etc. Mathematica is not ideal for OOP(!?). Is there an easy way for multiple turtles analog struct? A single turtle is no problem... Thank You.

POSTED BY: Albert Gaechter
8 Replies

Sorry. I have tried surely cardioide[t,a] with Underscore, but in the Reply they are not displayed...

POSTED BY: Albert Gaechter

Thank You very much. Now it works. I have tried with cardioide[t,a], without success (why?, who knows). For me this discussion is terminated. I am satisfied. With kind regards!

POSTED BY: Albert Gaechter

Is there a possibility to define cardioide[t,a] := Table[...]

This is quite simple really, just change your definition from using cardioide[a_] := to cardioide[t_, a_] := and it should work exactly as you describe. Read through this tutorial for information on defining functions in the Wolfram Language.

Looking at your cardioide function, you would do well to remove the list braces inside your table command since the "position" method already returns a list of two coordinates. Something like

cardioide[turtle_, a_ ? NumericQ] := Table[
    turtle["forward",
        (8 / 3) * a * N[Cos[i / 3]] * 0.01745
    ];
    turtle["left", 1 * Degree];
    turtle["position"],
    {i, 0, 9.425, 0.01745}
];
t1 = newTurtle[{0, 0}, {0, 1}];
t2 = newTurtle[{-1, 0}, {0, 1}];
ListLinePlot[
    {cardioide[t1, 2], cardioide[t2, 2]},
    AspectRatio -> Automatic
]
POSTED BY: Jason Biggs

Hi I have still a problem with this approach. I can define a cardioide for example:

t = newTurtle[{0, 0}, {0, 1}];
cardioide[a_] := Table[
   {t["forward", 8/3*a*N[Cos[i/3]]*0.01745]; t["left", 1 Degree]; 
    t["position"]}, {i, 0, 9.425, 0.01745}];
ListLinePlot[cardioide[2], AspectRatio -> Automatic]
![cardioide][1]

With t2 = newTurtle[{0, 0}, {1, 0}]; and a new definition for cardioide[a_] a new cardioide can be drawn. But this is not the meaning of multiple turtles (or OOP). The goal is: define once, use multiple time. Is there a possibility to define cardioide[t,a] := Table[...] and use this definition with cardioide[t1,2] or cardioide[t2,1.5]? In Julia I have function forward(t::Turtle,d) with forward(t1,50) or forward(t2,-20). Kind regards!

POSTED BY: Albert Gaechter

Thank You very much. This is indeed an approach. I will study this. The great deal of multiple turtles are "hunter prey problems" and superimposed movements.

POSTED BY: Albert Gaechter

I can't really figure out what you are trying to do. I don't really understand what the code in your screenshot does. After searching the web for "multiple turtles" it seems like some kind of common exercise for computer science students but again I can't figure out what the big deal is. If the goal is to draw paths in a graphics then there are surely better ways to do this with the Wolfram Language (WL).

But if the goal is to explore object-oriented programming (OOP) in Mathematica then this is a reasonable test case. You can find a few different frameworks for OOP in WL in the answers to this question, which are all rather advanced for this simple application.

Most expressions in the WL are immutable. When you make a list foo = {1,2,3} then that list is static, unchanging. It's full form is List[1, 2, 3]. If you call Append[foo, 4] then you get back a new list. If you call AppendTo[foo, 4] then a new list is created and the symbol foo is set equal to it. But in that example foo is mutable, foo can change its state quite easily. So in this example I will just use a new unique symbol for every turtle created, and assign definitions to it:

turtleImage = Interpreter["Species"]["Turtle"]["Image"];
newTurtle[position_ : {0, 0}, heading_ : {1, 0}] := With[
	{var = Unique @ "turtle"},
	var["position"] = position;
	var["heading"] = heading;
	var["forward", x_ ? NumericQ] := var["position"] += x var["heading"];
	var["backward", x_ ? NumericQ] := var["position"] -= x var["heading"];
	var["left", ang_] := (
		var["heading"] = N @ RotationTransform[ang][var @ "heading"];
	);
	var["right", ang_] := (
		var["heading"] = N[RotationTransform[-ang][var @ "heading"]];
	);
	(* put any more method definitions here *)
	Format[var] := turtleImage @ var @ "position"; (* this is a bit silly but the point is you can format the turtle however you like *)
	var
]

You can use it like this

In[64]:= foo = newTurtle[];
foo["forward", 20];
foo["left", 90 Degree];
foo["forward", 30];

In[68]:= foo["position"]

Out[68]= {20., 30.}

or put it in a table and create a random walk

turt = newTurtle[];
trajectory = Table[
	If[
		RandomChoice @ {True, False},
		turt["forward", RandomReal @ {0, 3.}],
		turt[RandomChoice @ {"left", "right"},
			RandomReal[{0, 50}] * Degree
		]
	];
	turt @ "position",
	{200}
];
ListLinePlot @ trajectory

enter image description here

POSTED BY: Jason Biggs

Thank You. Yes I know Association, but its not clear for me...Have you an example?

POSTED BY: Albert Gaechter
Posted 2 years ago

Take a look at Association.

POSTED BY: Rohit Namjoshi
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract