Last active
March 5, 2016 16:51
-
-
Save foresmac/ee2431e46b80db2d610c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# just some sample code I made for demonstrating the turtle lib in Python | |
import turtle | |
import math | |
import random | |
turtle.colormode(255) | |
RGBCMY = [ | |
(255, 0, 0), | |
(255, 255, 0), | |
(0, 255, 0), | |
(0, 255, 255), | |
(0, 0, 255), | |
(255, 0, 255)] | |
RAINBOW = [ | |
(255, 0, 0), | |
(255, 128, 0), | |
(255, 255, 0), | |
(128, 255, 0), | |
(0, 255, 0), | |
(0, 255, 128), | |
(0, 255, 255), | |
(0, 128, 255), | |
(0, 0, 255), | |
(128, 0, 255), | |
(255, 0, 255), | |
(255, 0, 128)] | |
def fractal(length, depth): | |
if depth == 0: | |
turtle.forward(length) | |
else: | |
fractal(length / 3, depth - 1) | |
turtle.right(60) | |
fractal(length / 3, depth - 1) | |
turtle.left(120) | |
fractal(length / 3, depth - 1) | |
turtle.right(60) | |
fractal(length / 3, depth - 1) | |
def polygon(sides, size): | |
angle = 360 / sides | |
for i in range(sides): | |
turtle.forward(size) | |
turtle.left(angle) | |
def polygon2(sides, size): | |
radius = size | |
angle = 360 / sides | |
length = 2 * radius * math.sin(math.radians(angle / 2)) | |
turtle.reset() | |
turtle.penup() | |
turtle.left(90) | |
turtle.forward(radius) | |
turtle.right(90 + (angle / 2)) | |
turtle.pendown() | |
for i in range(sides): | |
turtle.forward(length) | |
turtle.right(angle) | |
def cool_pattern(colors): | |
turtle.reset() | |
turtle.colormode(255) | |
for color in colors: | |
turtle.pencolor(color) | |
polygon(6, 100) | |
turtle.right(360/len(colors)) | |
def star(points=5, radius=100, direction=90, color=(0, 0, 0)): | |
angle = 180-(360/points) if points % 2 == 0 else 180-(180/points) | |
length = 2 * radius * math.sin(math.radians(angle/2)) | |
turtle.speed(10) | |
turtle.penup() | |
turtle.home() | |
turtle.setheading(direction) | |
turtle.forward(radius) | |
turtle.right(90 + angle/2) | |
turtle.pendown() | |
turtle.pencolor(color) | |
if points % 2 == 0 and (points//2) % 2 == 1: | |
points = points // 2 | |
for i in range(points): | |
turtle.forward(length) | |
turtle.right(angle) | |
turtle.penup() | |
turtle.home() | |
turtle.setheading(direction+180 if direction+180 < 360 else direction-180) | |
turtle.forward(radius) | |
turtle.right(90 + angle/2) | |
turtle.pendown() | |
for i in range(points): | |
turtle.forward(length) | |
turtle.right(angle) | |
else: | |
for i in range(points): | |
turtle.forward(length) | |
turtle.right(angle) | |
def rainbow_pattern(points, colors, radius=250): | |
turtle.reset() | |
for i, color in enumerate(colors): | |
angle = 360 / points / len(colors) | |
direction = 90 - (i * angle) | |
star(points, color=color, direction=direction, radius=radius) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment