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


Messages In This Thread
Draw isosceles triangle - by fen1c5 - Jul-06-2018, 10:34 AM
RE: Draw isosceles triangle - by ichabod801 - Jul-06-2018, 07:07 PM
RE: Draw isosceles triangle - by fen1c5 - Jul-07-2018, 08:20 AM
RE: Draw isosceles triangle - by ichabod801 - Jul-07-2018, 09:26 AM
RE: Draw isosceles triangle - by fen1c5 - Jul-07-2018, 10:20 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to write a recursion syntax for Sierpinski triangle using numpy? Bolzano 2 3,954 Apr-03-2021, 06:11 AM
Last Post: SheeppOSU
  Print user input into triangle djtjhokie 1 2,482 Nov-07-2020, 07:01 PM
Last Post: buran
  Tkinter - The Reuleaux Triangle andrewapk 1 2,019 Oct-06-2020, 09:01 PM
Last Post: deanhystad
  Python - networkx - Triangle inequality - Graph Nick_A 0 2,149 Sep-11-2020, 04:29 PM
Last Post: Nick_A
  Triangle function program m8jorp8yne 2 8,993 Dec-13-2019, 05:24 PM
Last Post: Clunk_Head
  Print triangle using while loop tuxandrew 3 5,059 Dec-05-2019, 07:17 PM
Last Post: micseydel
  Intersection of a triangle and a circle Gira 3 3,693 May-19-2019, 06:04 PM
Last Post: heiner55
  Pascal's triangle nvakada 5 4,742 May-01-2018, 02:44 AM
Last Post: Skaperen
  Object oriented area of a triangle xterakojede 2 9,023 Apr-20-2018, 01:42 PM
Last Post: xterakojede
  Turtle drawing Right Triangle Zatoichi 3 5,806 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