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

Isosceles Triangle Madness

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

50 Random Isosceles Triangles

Hints:

  • Make a function that draws isosceles triangle given the center of the base, length of the base, height, and the direction from the base to the top of the triangle.
  • Use random library’s uniform function to generate positions, size, direction, and color.

Source Code:

from turtle import *
import math
import random

title('Isosceles Triangle Madness - 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()

for _ in range(50):
    IsoscelesTriangle(random.uniform(-400,400),random.uniform(-400,400),
                      random.uniform(30,300), random.uniform(30,300),
                      random.uniform(0,360),
                      (random.uniform(0,1),random.uniform(0,1),random.uniform(0,1)))
              
update()

Related Projects

Diamond Suit Shape with Python and Turtle (Source Code)

Diamond Shape

Write a program that draw a diamonds suit shape with Python and Turtle. Please note that four sides are curves not straight lines.

A more challenging task is to make a function that draws a diamond shape at any center location , width, height, and arc angle. For example the diamond shape above has width/height ratio of 3/4 and four sides are arcs with 20 degree angle. You may need to apply basic trigonometry knowledge to solve this problem.

Source Code:

import turtle
import math

screen = turtle.Screen()
screen.title('Diamond Shape - PythonTurtle.Academy')
screen.setup(1000,1000)
screen.setworldcoordinates(-1000,-1000,1000,1000)
turtle.speed(0)
turtle.hideturtle()

def diamond(x,y, width, height, angle):
    turtle.up()
    turtle.color('red')
    turtle.goto(x,y-height/2)
    d = ((width/2)**2 + (height/2)**2)**0.5
    radius = d*0.5/math.sin(math.radians(angle/2))
    turtle.down()
    turtle.begin_fill()
    turtle.seth(turtle.towards(x-width/2,y)-angle/2)
    turtle.circle(radius, angle,20)
    turtle.seth(turtle.towards(x,y+height/2)-angle/2)
    turtle.circle(radius, angle,20)
    turtle.seth(turtle.towards(x+width/2,y)-angle/2)
    turtle.circle(radius, angle,20)
    turtle.seth(turtle.towards(x,y-height/2)-angle/2)
    turtle.circle(radius, angle,20)
    turtle.end_fill()    

diamond(0,0,1200,1600,20)

Throbbing Heart Animation with Python and Turtle (Source Code)

https://pythonturtle.academy/wp-content/uploads/2021/11/heart.mp4

Animate a throbbing heart. Check out this simple heart drawing tutorial if you need help.

Source Code:

import turtle
import math

screen = turtle.Screen()
screen.title('Heart Animation - PythonTurtle.Academy')
screen.setup(1000,1000)
screen.setworldcoordinates(-1000,-1000,1000,1000)
turtle.speed(0)
turtle.hideturtle()

screen.tracer(0,0)
turtle.color('red')

def draw_heart(alpha,d):
  r = d/math.tan(math.radians(180-alpha/2))
  turtle.up()
  turtle.goto(0,-d*math.cos(math.radians(45)))
  turtle.seth(90-(alpha-180))
  turtle.down()
  turtle.begin_fill()
  turtle.fd(d)
  turtle.circle(r,alpha)
  turtle.left(180)
  turtle.circle(r,alpha)
  turtle.fd(d)
  turtle.end_fill()

a = 220
sign = -1
def animate():
  global a,sign
  turtle.clear()
  draw_heart(a,1000)
  screen.update()
  a += sign
  if a < 215:
    sign = -sign
  elif a > 220:
    sign = -sign
  screen.ontimer(animate,50)

animate()

Tutorial: How to Draw a Simple Heart Shape with Python and Turtle

Simple Heart Shape with 4 Segments

The figure above shows, we can draw a simple heart shape with 4 segments: 2 lines and 2 arcs. We can continuously draw these 4 segments without lifting up the pen.

We start from the bottom tip of the heart. The heading for the blue line is 45 degrees. The second segment is 225 degree arc. After initial 45 degree heading and 225 degree turn, the heading of the turtle will be a 45+225=270 degrees, which is facing down perfectly. The rest of the two segments are symmetric to the first two. All we need to do is just turn the Turtle by 180 degrees before drawing the 3rd segment.

The next step is to figure out the ratio of blue segment and the radius of the arc. In the figure above, a triangle formed by two black lines and the blue line is a right triangle. The angle between two black lines is (360-225)/2 = 67.5 degrees, where 225 is the degree of the arc. Therefore, the ratio of blue line segment and the radius equals to tangent(67.5).

Here is the code:

import turtle
import math

turtle.color('red')
d = 800
r = d/math.tan(math.radians(67.5))
turtle.up()
turtle.goto(0,-600)
turtle.seth(45)
turtle.down()
turtle.fd(d)
turtle.circle(r,225)
turtle.left(180)
turtle.circle(r,225)
turtle.fd(d)

You can easily draw a filled heart by calling begin_fill() and end_fill() functions:

turtle.color('red')
d = 800
r = d/math.tan(math.radians(67.5))
turtle.up()
turtle.goto(0,-600)
turtle.seth(45)
turtle.begin_fill()
turtle.down()
turtle.fd(d)
turtle.circle(r,225)
turtle.left(180)
turtle.circle(r,225)
turtle.fd(d)
turtle.end_fill()

Related Projects: