Rhombus Madness

Use Python and Turtle and Random library to draw the 50 random Rhombus shapes as shown in the following figure.

50 Random Rhombus

Hints:

  • Make a function that draws a rhombus given the center, width, height, and the direction.
  • Use random library’s uniform function to generate positions, size, direction, and color

Related Projects:

Projects with similar difficulty

Geometry

Circle of Isosceles Triangles with Python and Turtle

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()

Related Projects:

Geometry

Loops

N Overlapping Circle with Python and Turtle (Source Code)

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

One to Ten (Source Code)

Draw from a single point to ten points that fall into a single line. Also draw dots on the end points.

One to Ten

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')

Real Stock Price Chart with Moving Averages (Source Code)

In a previous project we draw real stock price charts by reading data from files. We also draw stock moving averages in this project. Download historical stock prices for GoogleApple, and SPY and draw 20-day and 50-day moving averages for the last 1000 trading days.

20-day and 50-day moving averages for AAPL
20-day and 50-day moving averages for GOOGL
20-day and 50-day moving averages for SPY

Source Code:

import turtle

with open('googl.us.txt','r') as fin:
    lines = fin.readlines()
n = min(len(lines)-1,1000)
lines = lines[-n:]
prices = [None]*n
mva1 = [None]*n
mva2 = [None]*n
p1 = 20
p2 = 50

for i in range(1,n+1):
    row = lines[i-1].split(',')
    prices[i-1] = float(row[4])
    mva1[i-1] = sum(prices[max(0,i-p1):i])/min(p1,i)
    mva2[i-1] = sum(prices[max(0,i-p2):i])/min(p2,i)
minprice, maxprice = min(prices),max(prices)

screen = turtle.Screen()
screen.title('Stock Price and Moving Averages From File - PythonTurtle.Academy')
screen.setup(1000,1000)
screen.setworldcoordinates(0,minprice*0.9,n,maxprice*1.1)

turtle.speed(0)
turtle.hideturtle()
turtle.color('gray')
turtle.up()
turtle.goto(0,prices[0])
turtle.down()
tm1 = turtle.Turtle()
tm2 = turtle.Turtle()
tm1.hideturtle()
tm2.hideturtle()
tm1.color('red')
tm2.color('blue')
tm1.up()
tm1.goto(0,mva1[0])
tm1.down()
tm2.up()
tm2.goto(0,mva2[0])
tm2.down()
for i in range(1,n):
    turtle.goto(i,prices[i])
    tm1.goto(i,mva1[i])
    tm2.goto(i,mva2[i])

Real Stock Price Chart (GOOG, AAPL, SPY) with Source Code

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:

Stock Price Chart of GOOG from 2004 to 2016
Stock Price Chart of AAPL from 1984 to 2016
Stock Price Chart of SPY from 2005 to 2016

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])