Python and Turtle Difficulty Level 3,math Diamond Suit Shape with Python and Turtle (Source Code)

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)
Tags: ,

Related Post