A little introduction to Turtle in the python Library

Source: Internet
Author: User

Turtle is the meaning of turtles. It originally belongs to the logo language (not played ).. however, other languages are supported later. one is smallbasic, and the other is Python. I never knew this library was actually built in Python. it is based on the TK library.

It is easy to use.

from turtle import *

You can.

Simple Line Drawing.

Here, reset () is to clear the entire canvas and tell us that the tortoise (arrow) is placed at the origin (center of the canvas ). goto () is based on the current State (speed, color, whether to draw a line ...) motion to (70, 0) Point.

The turtle coordinate system is the same as the coordinate system we learned.

Let's draw a square. It's very simple.

That is, the distance between the forward (FD) and the right (right) is 90 degrees. You just need to go back and forth four times.

Of course, you can change the angle of any side. Of course, you can change the length by the way, as shown below:

Now, I will briefly introduce it here. You can check the manual for more functions.

Or more demos to see http://code.google.com/p/python-turtle-demo/

Let's look at a small example. Fractal. Of course, the classic Mandelbrot is broken. Change the leaf structure.

The method is very simple. Multiplication iteration. Instead of a single iteration, it is based on probability iteration. Each result is drawn on the image with coordinates.

The Code comes from. http://www.mathworks.cn/moler/intro.pdf, but the original code is written in MATLAB. I changed it to Python here. It uses the numpy library, which is mainly used for Matrix Multiplication. In fact, it is not difficult to write it myself.

During debugging, it is found that a * X has a different meaning in numpy. You need to use dot (A, x). The Code is as follows:

from  numpy import *from random import randomimport turtleturtle.reset()x = array([[.5],[.5]])p = [0.85,0.92,0.99,1.00]A1 = array([[.85,  0.04],            [-0.04,.85]])b1 = array([[0],[1.6]])A2 = array([[0.20,-0.26],            [0.23,0.22]])b2 = array([[0],[1.6]])A3 = array([[-0.15,0.28],            [0.26,0.24]])b3 = array([[0],[0.44]])A4 = array([[0,0],            [0,0.16]])turtle.color("blue")cnt = 1while True:    cnt += 1    if cnt == 2000:        break        r = random()    if r < p[0]:        x = dot(A1 , x) + b1    elif r < p[1]:        x = dot(A2 , x) + b2    elif r < p[2]:        x = dot(A3 , x) + b3    else:        x = dot(A4 , x)    #print x[1]    turtle.up()    turtle.goto(x[0][0] * 50,x[1][0] * 40 - 240)    turtle.down()        turtle.dot()

Note that the import. numpy. Dot conflicts with Turtle. Dot.

The result is as follows.

Teeth, ugly ..

Matrix67 sentence also learned about this article http://www.matrix67.com/blog/archives/500

Okay, go to bed ..

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.