Creating Turtle Graphics in IPython/Jupyter with ipycanvas
Project description
Creating Turtle Graphics in IPython/Jupyter with ipycanvas
This is inspired by https://github.com/gkvoelkl/ipython-turtle-widget. Thanks.
I am using ipycanvas (https://github.com/martinRenou/ipycanvas). Thanks to Martin Renou.
If you like it, use it. If you have some suggestions, tell me (hebi@python-ninja.com).
Install
To install use pip:
` $ pip install ipyturtlenext `
Examples
The most examples are inspired by Harold Abelson, Andrea diSessa: Turtle Geometry, MIT Press 1986
ipyturtlenext should work in every jupyter environment, where ipycanvas works.
Start
from ipyturtlenext import Turtle
t = Turtle(width=200, height=200)
t
With width and height you can change the extension of the drawing canvas.
In Jupyterlab you can create with new view for outout a separeted tab.
First Steps
t.right(90)
t.heading()
t.forward(150)
t.left(45)
t.back(100)
t.left(45)
t.penup()
t.forward(100)
Square
t.reset() #clear canvas and start again
t.back(40)
t.forward(100)
t.position()
def square(size):
for i in range(4):
t.forward(size)
t.right(90)
square(20)
Triangel
t.reset()
def triangle(size):
for i in range(3):
t.forward(size)
t.right(120)
triangle(100)
House
t.reset()
def house(size):
square(size)
t.forward(size)
t.right(30)
triangle(size)
t.back(100)
house(100)
Circle
t.reset()
def circle():
for i in range(360):
t.forward(1)
t.right(1)
circle()
Poly
t.reset()
def poly(side, angle):
turn = 0
while turn == 0 or turn % 360 != 0:
t.forward(side)
t.right(angle)
turn += angle
poly(44,135)
Color
Return the current pen color as RGB tuple or web color name
t.reset()
t.pencolor()
Set pen color as web color name
t.pencolor('Green')
Set pen color with RGB value
t.pencolor(255,0,0)
t.forward(40)
t.right(120)
t.pencolor('Blue')
t.forward(40)
t.pencolor('Red')
t.pendown()
t.forward(100)
Branch
def lbranch(length, angle, level):
t.pencolor('Green')
t.forward(2*length)
node(length, angle, level)
t.back(2*length)
def rbranch(length, angle, level):
t.pencolor('Brown')
t.forward(length)
node(length, angle, level)
t.back(length)
def node(length, angle, level):
if level==0:
return
t.left(angle)
lbranch(length, angle, level-1)
t.right(2*angle)
rbranch(length, angle, level-1)
t.left(angle)
t.reset()
node(8,24,7)
Nested Triangle
def nested_triangle(size):
if size < 10:
return
for i in range(3):
nested_triangle(size/2)
t.forward(size)
t.right(120)
t.reset()
nested_triangle(100)
Snowflake
def snowflake(size, level):
for i in range(3):
side(size, level)
t.right(120)
def side(size, level):
if level == 0:
t.forward(size)
return
side(size/3, level - 1)
t.left(60)
side(size/3, level - 1)
t.right(120)
side(size/3, level - 1)
t.left(60)
side(size/3, level - 1)
t.reset()
snowflake(100,4)
Nested squares
t.reset()
sideLength = 40
for square in range(5):
for side in range(4):
t.forward(sideLength)
t.left(90)
sideLength += 10
Changelog
0.1.0 First published version
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file ipyturtlenext-0.1.1.tar.gz
.
File metadata
- Download URL: ipyturtlenext-0.1.1.tar.gz
- Upload date:
- Size: 5.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f9d962a458d6308f737671c2b2e7b92aa26aecf6a1c19275ee50d72e2203f187 |
|
MD5 | fe9ed73c99d4aee1cec056ab6aaf1378 |
|
BLAKE2b-256 | 410b9f03be9c1707f93f03582983baa35c2dd4837a60e85b2a18e0089f9fb802 |
File details
Details for the file ipyturtlenext-0.1.1-py3-none-any.whl
.
File metadata
- Download URL: ipyturtlenext-0.1.1-py3-none-any.whl
- Upload date:
- Size: 5.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 296cc51092b29b9b1da2d6ab52a9a7bbd46caf7d2925ef968843886784022f28 |
|
MD5 | 4fb8483141f0a0aacb6563547d57c7fd |
|
BLAKE2b-256 | 4a85b144210fbba2a423f2fab5203ae90b13a6587cae059404f3c19e34afc9d5 |