On 6/29/06, Gregor Lingl <[EMAIL PROTECTED]> wrote:


>
I've thought about this,but abandoned it for the moment. The xturtle API
is already
rather 'fat' as the core developers complained.

I'll appreciate your proposal. So do it, for now as an experiment.
At the moment I'm very busy (and have to concentrate on my Europython talk),
but I've several points to say on this. Please wait until end of next
week, then
I'll be ready to discuss this in depth.


Hi Gregor,
 
I finally got around to implementing world coordinates for xturtle.  My first goal was 'do no harm', and so even after my modifications all the demos continue to run just fine.  I've been testing it with some simple examples and things seem to work fine.

If you want to have a look at the code you can grab it from here:

http://knuth.luther.edu/~bmiller/downloads/xturtle.py

Basically What I did was the following:

  1. Add new method to TurtleScreenBase called setWorldCoordinates(llx,lly,urx,ury) This method lets the user set the canvas coordinates to whatever they want specifying the lowerleft point and the upper right point of the canvas.  This method is meant to be called once at the beginning of your program.
  2. Modified the _drawline and _drawpoly methods to scale the the points on the line appropriately.  Also added a keyword parameter called scale
  3. Modified the _drawturtle function to create a turtle in the new coordinate system and then have _drawpoly draw the turtle unscaled.  This is kind of a nasty interdependency between the screen and the turtle and the canvas.
  4. Added a Turtle class that only adds one method (setWorldCoordinates) which passes through the call to set the coordinates to the screen.  This method could probably be done in RawPen, but I wanted to instantiate a Turtle not a Pen.
If you have any suggestions for a better implementation strategy I'd be happy to here them!

Here is a really simple example of just using the turtle to plot some points inside the upper right quadrant of the unit circle.  A monte carlo simulation for estimating pi.  I will, of course, be working on some additional tests and demos.

import random
import math
import xturtle

def inCircle(x,y):
   d = math.sqrt(x**2 + y**2)
   if d <= 1:
      return True
   else:
      return False

def main(): 
    t = xturtle.Turtle()
    t.setWorldCoordinates(-1.0,-1.0,1.0,1.0)

    total = 30000
    circle = 0
    t.up()
    for i in range(total):
        x = random.random()
        y = random.random()
        t.goto(x,y)
        if inCircle(x,y):
            t.color("blue")
            t.dot()
            circle = circle + 1
        else:
            t.color("red")
            t.dot()      
    pi = float(circle)/total * 4  
    print pi   
main()

Brad


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "edupython" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/edupython
-~----------~----~----~----~------~----~------~--~---

Reply via email to