Python Forum
Draw isosceles triangle
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Draw isosceles triangle
#1
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.
Reply
#2
Where's your code for drawing the triangles and how is it failing?
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
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]
Reply
#4
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
Ok, thanks for your answer. I'm not able to write these programs right now. Not yet.

Thanks again
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to write a recursion syntax for Sierpinski triangle using numpy? Bolzano 2 3,845 Apr-03-2021, 06:11 AM
Last Post: SheeppOSU
  Print user input into triangle djtjhokie 1 2,361 Nov-07-2020, 07:01 PM
Last Post: buran
  Tkinter - The Reuleaux Triangle andrewapk 1 1,927 Oct-06-2020, 09:01 PM
Last Post: deanhystad
  Python - networkx - Triangle inequality - Graph Nick_A 0 2,080 Sep-11-2020, 04:29 PM
Last Post: Nick_A
  Triangle function program m8jorp8yne 2 8,848 Dec-13-2019, 05:24 PM
Last Post: Clunk_Head
  Print triangle using while loop tuxandrew 3 4,903 Dec-05-2019, 07:17 PM
Last Post: micseydel
  Intersection of a triangle and a circle Gira 3 3,584 May-19-2019, 06:04 PM
Last Post: heiner55
  Pascal's triangle nvakada 5 4,599 May-01-2018, 02:44 AM
Last Post: Skaperen
  Object oriented area of a triangle xterakojede 2 8,925 Apr-20-2018, 01:42 PM
Last Post: xterakojede
  Turtle drawing Right Triangle Zatoichi 3 5,711 Feb-26-2018, 12:24 AM
Last Post: Zatoichi

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020