Reputation: 25
I need to make this triangle (https://i.sstatic.net/2PlIv.png) in turtle, but just can't quite get the dimensions right. In addition to this, I also need each quadrant of the triangle to display in a different color according to a list I have set... I can do that later. But I am having trouble getting the length/widths of the triangle to be correct. I will link a picture of something similar to what It needs to be here (https://i.sstatic.net/LuOgu.png) This is the closest thing I could find that resembles that I need it to do. How do I use something like width, width * math.sqrt(2), and width
to get the lengths and widths of the triangle? Any help is greatly appreciated. Sorry if my English is bad, it is not my first language. Below is what I have written so far.
turtle.setup(575,575)
pen = turtle.Turtle()
pen.speed(0)
pen.pensize(2)
pen.hideturtle()
width = random.randint(25,100)
def draw_art_cube(x,y):
draw_triangle(width, width * math.sqrt(2) + width)
def draw_triangle(x,y):
pen.up()
pen.setpos(x,y)
pen.down()
pen.setheading(0)
pen.forward(width)
pen.left(width)
turtle.onscreenclick(draw_art_cube)
turtle.done()
Upvotes: 1
Views: 1394
Reputation: 41872
Your draw_triangle()
function is woefully underwritten. You need to draw a minimum of half a dozen lines but your code only draws one! Let's flesh out this function to make it draw the example image:
from turtle import Screen, Turtle
from random import randint
def draw_art_cube(x, y):
screen.onscreenclick(None) # Disable handler inside handler
pen.up()
pen.setposition(x, y)
pen.down()
width = randint(25, 100)
draw_triangle(width)
screen.onscreenclick(draw_art_cube) # Reenable handler on exit
def draw_triangle(short):
long = short * 2**0.5
pen.setheading(0)
for _ in range(4):
pen.forward(short)
pen.left(135)
pen.forward(long)
pen.left(135)
pen.forward(short)
pen.left(180)
screen = Screen()
screen.setup(575, 575)
pen = Turtle()
pen.hideturtle()
pen.speed('fastest')
pen.pensize(2)
screen.onscreenclick(draw_art_cube)
screen.mainloop()
This drawing code is inefficient as it draws 12 (somewhat redundant) lines when we can get away with half that. But it should give you a working starting point to think through the best way to draw this figure.
Upvotes: 2