I am trying to define a function that draws a triangle of a given size using the turn method. I've never used turtle graphics before and am not sure how to go about this.
Thank you

Recommended Answers

All 6 Replies

In simple terms, to form the triangle you can draw 3 lines of the same length. To connect the lines, you turn the turtle by 360/3 = 120 degrees after each line has been drawn.

Example ...

import turtle as tu

tu.forward(200)
tu.right(120)

tu.forward(200)
tu.right(120)

tu.forward(200)
tu.right(120)

tu.done()

You can simplify this with a for loop. Also start out the turtle at some other point, default is the center of the canvas.

So this is what i have so far:

import turtle

def drawTriangle(x,y,z):
    tu.forward(x)
    tu.right(120)
    tu.forward(y)
    tu.right(120)
    tu.forward(z)
    tu.right(120)
    tu.done()

However this does not work if all sides of the triangle are not the same. And also, how would it be done using the turn method?

tu.done should be in main body of your code.
Use these functions:
xcor and ycor
gives x and y positions of mouse
goto(x,y)
going to given absolute cordinates

define triangle by giving only two sides and then recording starting point before starting, draw given side lengths and turn angle between them (for example)
def drawTriangle(x,turn,y):

Here is test for your main program

for i in range (0,360,20):
    print 
    drawTriangle(250,250, 67) ## first side length, turn, second side length
    tu.right(20)
tu.done()

Output attached

Animal mixup, cordinates of turtle, not mouse :-)


And right() and left() are turn methods.

If you want to draw a triangle knowing only it's three unequal sides, then things get a bit more complex. You have to apply the law of sines, which states that the ratio of the length of a side to the sine of its corresponding opposite angle is constant.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.