Python Forum

Full Version: Draw isosceles triangle
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi all boys.

So, I have difficulty creating a program that designs an isosceles and scaeno triangle. These are just exercises that I like to perform.

I have no problem in drawing an equilateral triangle, a square, polygon and different geometric shapes. but the two triangles in question just do not succeed.

For example I created this program
import math
import turtle

def square(t, len):
    """Draws a sqare with sides of given length.

    Returns the Turtle to the starting position and location.
    """
    
    for i in range(4):
        t.fd(len)
        t.lt(90)

def polyline(t, n, len, angle):
    """Draws n line sgments.

    t: Turtle object
    n: number of line segments
    len: length of each segment
    angle: degrees betwen segments
    """
    for i in range(n):
        t.fd(len)
        t.lt(angle)

def polygon(t, n, len):
    """Drwaws a polygon with sides.

    t: Turtle object
    n: number of sides
    len: length of each side
    """    
    angle = 360.0 / n
    polyline(t, n, len, angle)

def arc(t, r, angle):
    """Draws an arc with the given radius and angle.

    t: Turtle
    r: radius
    angle: angle subtended by the arc, in degrees
    """
    arc_len = 2 * math.pi * r * abs(angle) / 360
    n = int(arc_len / 3) + 1
    step_len = arc_len / n
    step_angle = float(angle) / n

    # making a slight left turn before starting reduces
    # the error caused by the linear approximation of the arc

    t.lt(step_angle / 2)
    polyline(t, n, step_len, step_angle)
    t.rt(step_angle / 2)

def circle(t, r):
    """Draws a circle with the given radius.

    t: Turtle object
    t: radius
    """
    arc(t, r, 360)

# the following condition checks whther we are
# running as a script, in which cas run the test code
# or being imported, in which cas dont't.

if __name__ == '__main__':
    bob = turtle.Turtle()

    # draw a circle centered on the origin
    radius = 100
    bob.pu()
    bob.fd(radius)
    bob.lt(90)
    bob.pd()
    circle(bob, radius)

    # wait for the user to close the window
    turtle.mainloop()
Give me a hand please ??

Thank you in advance.
Where's your code for drawing the triangles and how is it failing?
import turtle
bob = turtle.Turtle()

def triangle(t, b, h, len):
    p = 2 * b / h
    for i in range(3):
        t.fd(len)
        t.lt(120)

triangle(bob, 50, 25, 100)
turtle.mainloop()

(Jul-07-2018, 08:20 AM)fen1c5 Wrote: [ -> ][import turtle
bob = turtle.Turtle()

def triangle(t, b):
angle = 360
len = 3 * b
p = b + (2 * len) / angle
for i in range(3):
t.fd(len)
t.lt(120)

triangle(bob, 100)
turtle.mainloop()

[/python]
That works fine. Again, where are you having problems? We're not going to write code for you, but we'll help you with code that isn't working.

If you're supposed to do the triangles based on the length of the sides, you'll need trigonometry, specifically the law of cosines. The trig functions are in the math module.
Ok, thanks for your answer. I'm not able to write these programs right now. Not yet.

Thanks again