<!doctype html public "-//W3C//DTD W3 HTML//EN">
<html><head><style type="text/css"><!--
blockquote, dl, ul, ol, li { margin-top: 0 ; margin-bottom: 0 }
 --></style><title>Re: Function Newbie
question...</title></head><body>
<blockquote type="cite" cite><br>
--- Jim Hurley <jhurley@infostations.com> wrote:<br>
<br>
> >I wrote a small function that returns the<br>
> distance between two Points.<br>
</blockquote>
<blockquote type="cite" cite>does this pertain to Turtle Graphics?<br>
<br>
=====</blockquote>
<blockquote type="cite" cite>erik@erikhansen.org   
http://www.erikhansen.org</blockquote>
<div><br></div>
<div>Eric, there is a function "distance" in Turtle Graphics
but it relates to the  distance between the current location of
the turtle (cursor element) and some chosen point.  For example,
if the turtle were at the origin (center of the screen), the function
"distance(30,40)" would return 50--the hypotenuse of a right
triangle whose legs are 30, 40  is 50.</div>
<div><br></div>
<div>In some versions of TG there are multiple turtles--called
sprites. And so in a game with multiple, moving objects, one would
need a function to determine distance between each of the
turtles.</div>
<div><br></div>
<div>The very basics in TG might be the following set of commands and
functions, which I hope are self explanatory:</div>
<div><br></div>
<div>Commands:</div>
<blockquote>forward 100</blockquote>
<blockquote>back 100</blockquote>
<blockquote>right 30</blockquote>
<blockquote>left 30</blockquote>
<blockquote>setHeading 45</blockquote>
<blockquote>setXY 30,40</blockquote>
<blockquote>incXY 3,4 --increment the x and y 
coordinates</blockquote>
<blockquote><br></blockquote>
<div>Functions:</div>
<blockquote>xCor()</blockquote>
<blockquote
>yCor()          <span
></span>     </blockquote>
<blockquote>heading ( )<x-tab>    
</x-tab>           
</blockquote>
<blockquote>distance (xTemp, yTemp)<x-tab> </x-tab></blockquote>
<div><br></div>
<div>This is all quite simple to implement in Transcript. But the
beauty of TG, like Transcript itself, is that it is extensible.</div>
<div><br></div>
<div>I know that RunRev is looking eventually to expand its market in
education. When it does, I think it will be helpful to include
examples of how Transcript and Turtle Graphics might be employed by
students as problem solving tools. For example, with sufficient
background, the students would write their own program to simulate
planetary motion using only the TG defined above:</div>
<div><br></div>
<blockquote>constant G = 8000</blockquote>
<div><br></div>
<blockquote>on orbitTheSun</blockquote>
<blockquote>  forward 90 -- Move the plant 90 units from the
sun</blockquote>
<blockquote>  put 0 into vx -- Initial horizontal velocity of
planet</blockquote>
<blockquote>  put 12 into vy -- Initial vertical velocity of
planet</blockquote>
<blockquote><br></blockquote>
<blockquote>  repeat until mouseClick() -- My apologies for
polling the mouse.</blockquote>
<blockquote>    incXY vx, vy -- Move the turtle
(planet) a distance v (distance/second) in each second</blockquote>
<blockquote>    add accx() to vx</blockquote>
<blockquote>    add accy() to vy</blockquote>
<blockquote>  end repeat</blockquote>
<blockquote><br></blockquote>
<blockquote>end orbitTheSun</blockquote>
<blockquote><br></blockquote>
<blockquote>function accx</blockquote>
<blockquote>  -- Newton's inverse square law of
gravity</blockquote>
<blockquote>  return -G * xcor()/ distance(0,0)^3 -- The sun is
at 0,0</blockquote>
<blockquote>end accx</blockquote>
<blockquote><br></blockquote>
<blockquote>function accy</blockquote>
<blockquote>  return -G * ycor()/ distance(0,0)^3</blockquote>
<blockquote>end accy</blockquote>
<div><br></div>
<div>The result is an ellipse.</div>
<div><br></div>
<div>The emphasis would not necessarily be programing for its own
sake, but for programing (Transcript + TG) as a problem solving tool
in a variety of disciplines--in fact, getting back to the original
function of programming.</div>
<div><br></div>
<div>Sorry Eric: That's probably a lot more than your wanted to know
about the distance function.</div>
<div><br></div>
<div>Jim Hurley</div>
</body>
</html>