Use the Isosceles triangle function created in this project, draw the circle of isosceles triangles as shown below. Your code should easily change the number of triangles, and radius and height of the triangles.
Source Code:
from turtle import *
import math
import random
title('Isosceles Triangle Circle - PythonTurtle.Academy')
setup(1000,1000)
setworldcoordinates(-500,-500,500,500)
hideturtle()
tracer(0,0)
# x,y is the center of the base, width: length of base, height: height of triangle from the top to base
# direction:direction from the center of base to top
def IsoscelesTriangle(x,y,width,height,direction,c):
up()
goto(x,y)
seth(direction-90)
fd(width/2)
p1x, p1y = xcor(), ycor() # first point: bottom right
back(width)
p2x, p2y = xcor(), ycor() # second point: bottom left
goto(x,y)
seth(direction)
fd(height)
p3x, p3y = xcor(), ycor() # third point: top
goto(p1x,p1y)
down()
fillcolor(c)
begin_fill()
goto(p2x,p2y)
goto(p3x,p3y)
goto(p1x,p1y)
end_fill()
n=12
r=300
width=2*r*math.sin(math.radians(180/n))
height=200
for i in range(n):
IsoscelesTriangle(r*math.cos(math.radians(180/n))*math.cos(math.radians(i*360/n)),
r*math.cos(math.radians(180/n))*math.sin(math.radians(i*360/n)),width,height,i*360/n,'blue')
update()
Generalize the three overlapping circles project by letting it draw any number of overlapping circles. The following is the overlapping circles from size 2 to size 7.
Solution
import turtle
import math
screen = turtle.Screen()
screen.title('N Overlapping Circles - PythonTurtle.Academy')
screen.setup(1000,1000)
turtle.hideturtle()
turtle.speed(0)
turtle.pensize(2)
def draw_circle(x,y,radius):
turtle.up()
turtle.goto(x,y-radius)
turtle.seth(0)
turtle.down()
turtle.circle(radius,steps=360)
r = 150
n = 3
r2 = r/2/math.sin(math.radians(180/n))
angle = 90
for _ in range(n):
draw_circle(r2*math.cos(math.radians(angle)),
r2*math.sin(math.radians(angle)),
r)
angle += 360/n
Draw from a single point to ten points that fall into a single line. Also draw dots on the end points.
Source Code:
import turtle
turtle.setup(1000,1000)
turtle.title('One to Ten - PythonTurtle.Academy')
turtle.speed(0)
turtle.hideturtle()
turtle.color('green')
turtle.up()
turtle.goto(0,250)
turtle.dot('red')
for x in range(-450,460,100):
turtle.up()
turtle.goto(0,250)
turtle.down()
turtle.goto(x,-250)
turtle.dot('blue')
A complete bipartite graph is a graph with two sets of vertices where there is no edge between vertices belonging to the same set; but all vertices of different sets are fully connected. Draw the K(10,10) where each set have 10 vertices.
Draw a regular hexagon that have vertices connected to the center. Use for loop to draw this shape.
Source Code:
hideturtle()
for angle in range(0,360,60):
up()
goto(0,0)
seth(angle)
down()
fd(300)
up()
goto(300,0)
seth(120)
down()
for _ in range(6):
fd(300)
left(60)
Download historical stock prices for Google, Apple, and SPY. Read the file and draw the stock charts for these companies. The following figures show the stock prices for these companies:
Source Code:
import turtle
with open('spy.us.txt','r') as fin:
lines = fin.readlines()
n = len(lines)-1
prices = [None]*n
for i in range(1,len(lines)):
row = lines[i].split(',')
prices[i-1] = float(row[4])
minprice, maxprice = min(prices),max(prices)
screen = turtle.Screen()
screen.title('Stock Price From File - PythonTurtle.Academy')
screen.setup(1000,1000)
screen.setworldcoordinates(0,minprice*0.9,n,maxprice*1.1)
turtle.speed(0)
turtle.hideturtle()
turtle.up()
turtle.goto(0,prices[0])
turtle.down()
for i in range(1,n):
turtle.goto(i,prices[i])