How to draw right angled triangle with python - python

def drawTri(a):
b = (a*math.tan(45))
c = (a/math.cos(45))
t.forward(a)
t.left(135)
t.forward(c)
t.left(135)
t.forward(b)

import turtle
def drawTri(a):
hyp = a * 2**0.5
s = turtle.Screen()
t = turtle.Turtle()
t.forward(a)
t.left(135)
t.forward(hyp)
t.left(135)
t.forward(a)

The problem here is close to that described in Basic trigonometry isn't working correctly in python
The turtle module uses degrees for angles, the math module uses radians
To calculate the cosine of 45 degrees you can use
math.cos(math.radians(45))

i made a relay simple way of making a right angle triangle i also added some other helpful things to know about python turtle that is you didn't already know them they should be helpful(i know you already have an answer but i just think that this is a much simpler way)
import turtle
t = turtle
f = t.forward
r = t.right
t.color('blue','yellow')
t.begin_fill()
f(70)
r(135)
f(100)
r(135)
f(70)
r(135)
t.end_fill()
t.penup()
t.setposition(-50,30)
t.pendown()
t.color('blue','yellow')
t.begin_fill()
f(70)
r(135)
f(100)
r(135)
f(70)
r(135)
t.end_fill()
there are two triangles there and its the extra stuff that makes it so bulky

Who needs angles?
def drawTri(a):
x, y = turtle.position()
turtle.setx(x + a)
turtle.sety(y + a)
turtle.goto(x, y)

Related

Drawing a triangle using only a grid of circles, python

I have to make a change to this specific code, which produces a square grid of circles, I have to change the code to make a triangle grid of circles.
import turtle
window = turtle.Screen()
my_boi = turtle.Turtle()
my_boi.speed(0)
for y in range(-200,200,50):
for x in range(-200,200,50):
my_boi.penup()
my_boi.setposition(x,y)
my_boi.pendown()
my_boi.circle(20)
window.exitonclick()
I'm sure there is a smarter approach, but this is one way to do it:
import turtle
window = turtle.Screen()
my_boi = turtle.Turtle()
my_boi.speed(0)
for (i,y) in enumerate(range(-200,200,50)):
for x in range(-200+(25*i),200-(25*i),50):
my_boi.penup()
my_boi.setposition(x,y)
my_boi.pendown()
my_boi.circle(20)
window.exitonclick()
turtle.done()
In the second for-loop the range is iteratively decreased by 1/2 of the circle diameter in each side.
I'd simplify things somewhat:
from turtle import Screen, Turtle
window = Screen()
my_boi = Turtle()
my_boi.speed('fastest')
my_boi.penup()
for y in range(1, 9):
my_boi.setposition(-25 * y + 25, 50 * y - 250)
for x in range(y):
my_boi.pendown()
my_boi.circle(20)
my_boi.penup()
my_boi.forward(50)
my_boi.hideturtle()
window.exitonclick()
Only the starting position of each row has to be calculated and placed via setposition(). The column positions can be a simple forward() statement.
I know this is an old post, but I was looking for the answer and managed to figure it out, so to help everyone else out, All you need to do is change the stop range in the nested loop to be y. I switched the x and y variables because I wanted the triangle to be flat, but if you need it the other way that also works.
import turtle
window = turtle.Screen()
my_boi = turtle.Turtle()
my_boi.speed(0)
for x in range(-200,200,50):
for y in range(-200,x,50):
my_boi.penup()
my_boi.setposition(x,y)
my_boi.pendown()
my_boi.circle(20)
window.exitonclick()

Section 4.3 Exercise #5 - "Think Python" by Allen Downey

Link to the Exercises can be accessed here - Case Study: Interface Design, Exercise Section 4.3
Quoting the question, it seems I have to implement an arc() function:
Make a more general version of circle called arc that takes an additional parameter angle, which determines what fraction of a circle to draw. angle is in units of degrees, so when angle=360, arc should draw a complete circle.
The code I've written so far:
import turtle
import math
bob = turtle.Turtle()
def polygon(t, n, length):
for i in range(n):
t.fd(length)
t.lt(360/n)
def circle(t, r):
circumference = 2 * math.pi * r
n = int(circumference/3) + 1
length = circumference/n
polygon(t, n, length)
def arc(t, r, angle):
arc_length = 2 * math.pi * r * (angle/360)
n = (arc_length/4) + 1
arc(bob, 1000, 45)
turtle.mainloop()
I'm intending to call the circle() function within arc() just as polygon() was called within circle(), but I'm confused on how I should do that. Apart from that, the arc() function does not draw anything, rather just shows me a stationary Turtle.
I believe that the Turtle object bob isn't receiving any of the movement instructions assigned within polygon(). Thus all that it's doing is displaying the Turtle object!
I could be wrong, and this where I need clarification. Should I call circle() within arc() as well as make the Turtle object move? Are there easier alternatives? Calling functions within a function is still confusing for me, so more resources to learn about them would be great as well!
import turtle
bob=turtle.Turtle()
import math
def arc(t,radius,angle):
circumference = 2.0*math.pi*radius
frac = angle/360.0
arclength = circumference*frac
n = 50 # pick a number
len = arclength/n;
turnang = angle/n
for i in range(n):
t.fd(len)
t.lt(turnang)
arc(bob, 130,360)
turtle.done()
I'm trying to ... call the circle() function
within arc() just as polygon() was called within circle()
You've got this backward. The problem states:
Make a more general version of circle called arc
Just as you could draw a circle with the more general function polygon(), you should be able to draw a circle with the more general function arc(). Here's a skeletal program for thinking about this:
from turtle import Screen, Turtle
from math import pi
def polygon(turtle, sides, length):
outside_angle = 360 / sides
for _ in range(sides):
turtle.forward(length)
turtle.left(outside_angle)
def circle_using_polygon(turtle, radius):
circumference = 2 * pi * radius
sides = min(60, int(circumference / 3))
length = circumference / sides
polygon(turtle, sides, length)
def arc(turtle, radius, angle):
# implement arc not by calling *circle() nor by
# calling polygon() but rather by borrowing code
# from both and adding one more step to reduce
# the number of sides based on the arc angle
def circle_using_arc(turtle, radius):
arc(turtle, radius, 360)
bob = Turtle(visible=False)
# Draw overlapping circles three different ways:
bob.color("green")
circle_using_polygon(bob, 100)
for color in ['cyan', 'magenta', 'yellow', 'black']:
bob.color(color)
arc(bob, 100, 90)
bob.color("blue")
circle_using_arc(bob, 100)
screen = Screen()
screen.mainloop()
import tkinter
import swampy
from swampy.TurtleWorld import *
def polygon(n, t, length, angle):
print(t)
k= angle/360
for i in range(0,int(n*k)):
fd(t, length)
p= 360
lt(t,p/n)
t.delay
world = TurtleWorld()
bob = Turtle()
#def circle(r):
#l1= 2*3.14*r
#l= l1/60
#polygon(30, bob, l)
polygon(60, bob, 10, 180)

Turning simple polygons about a point in Python Turtle

I need help turning polygon shapes (triangle and square) in Python turtle to match a picture.
Below I am trying to copy the image.
I specifically need help on what to add to my code given the triangle and square to have them repeat outwards like the picture. Because as of now the triangles and squares look like this (pentagon code is correct and works) All help is appreciated. Thank you.
import turtle
def polygon(turtle, side, length):
turtle.color("Blue")
for i in range(4):
turtle.backward(length)
turtle.left(side)
def polygon1(turtle, side1, length):
turtle.color("Green")
for i in range(3):
turtle.left(side1)
turtle.forward(length)
def polygon2(turtle, side2, length):
turtle.color("Red")
for i in range(5):
turtle.forward(length)
turtle.left(side2)
def main():
my_turtle = turtle.Turtle()
wn = turtle.Screen()
Bill = turtle.Turtle()
length = 100
side = 90
side1 = 120
side2 = 72
Bill.pensize(5)
Bill.speed(0)
#Pentagons
Bill.pu()
Bill.right(180)
y = -45
for i in range(5):
Bill.pu()
Bill.goto(60, y)
Bill.pd()
polygon2(Bill, side2, length)
y -= 20
#Triangle
Bill.pu()
Bill.left(240)
x = 45
for j in range(5):
Bill.pu()
Bill.goto(10, x)
Bill.pd()
polygon1(Bill, side1, length)
x += 20
#Square
Bill.pu()
Bill.left(240)
b = 6
for b in range(5):
Bill.pu()
Bill.goto(148, b)
Bill.pd()
polygon(Bill, side, length)
b -= 20
wn.exitonclick()
if __name__ == '__main__':
main()
pentagon code is correct and works
I don't believe the pentagon code is correct nor that you're approaching this in the correct way. The inner three shapes should form an equilateral triangle -- yours don't as you're eyeballing instead of calculating. Instead of trying to get the turtle to be in the right spot, why not have the turtle move forward in the direction of the sides of this central triangle, drawing polygons as it goes.
That is, embrace the drawing as a whole rather than trying to divide and conquer.
We'd need to make sure the polygon drawing code restores the turtle's state when it's done, so it can simply move forward to the next polygon. We'll need to make explicit which numbers are arbitrary, and which are calculable. Although the original diagram appears to use at least three turtles to achieve it's result, we'll do it with one as you attempted:
from turtle import Turtle, Screen
SHAPES = [(5, "Red"), (3, "Green"), (4, "Blue")]
LENGTH = 100
DELTA = 20
REPLICATIONS = 5
THICKNESS = 5
HEIGHT = (3 ** 0.5 / 2) * LENGTH # assumes 3 shapes, should fix!
DIVISIONS = 360 / len(SHAPES)
def polygon(turtle, sides, color):
turtle.color(color)
turtle.left(90)
turtle.forward(LENGTH / 2)
for _ in range(sides):
turtle.right(360 / sides)
turtle.forward(LENGTH)
turtle.backward(LENGTH / 2) # restore turtle to original state
turtle.right(90)
wn = Screen()
bill = Turtle()
bill.speed('fastest')
bill.pensize(THICKNESS)
bill.penup()
for offset, (sides, color) in enumerate(SHAPES):
bill.setheading(-DIVISIONS * offset - 90)
bill.forward(HEIGHT / 3) # assumes 3 shapes, should fix!
for _ in range(REPLICATIONS):
bill.pendown()
polygon(bill, sides, color)
bill.penup()
bill.forward(DELTA)
bill.home()
wn.exitonclick()

Write a function called circle that takes a turtle, t, and radius, r, as param

import turtle
bob = turtle.Turtle()
def polygon(t,length,n):
for i in range(n):
t.fd(length)
t.lt(360/n)
print(t)
polygon(bob,30,15)
turtle.mainloop()
How can I make a circle by calling polygon function?
You already have written the correct code to produce a circle. In the view of turtle's own circle() method, a circle is just a polygon with 60 sides (fewer if the circle is small.) I.e. it's about perception and how many sides do you need before you can't tell the difference.
import turtle
def polygon(t, length, n):
for _ in range(n):
t.fd(length)
t.lt(360 / n)
bob = turtle.Turtle()
bob.penup()
bob.sety(-270)
bob.pendown()
polygon(bob, 30, 60)
turtle.mainloop()
Your problem now is to control the drawing of the polygon/circle to produce it with a specific radius. Your length parameter doesn't map to a radius as the circle is coming out way too large. Here length represents 1/60 (1/n) of the circumference and we know that:
circumference = 2 * math.pi * radius
We can calculate, in our circle(t, radius) function, what length needs to be given radius (i.e. circumference/n), and call polygon(t, length, n) with these parameters. Here's a visual comparison drawing a radius 100 circle with turtle's circle() method (red) and drawing it with the solution I just described (blue):
import turtle
bob=turtle.Turtle()
bob.color('green', 'cyan')
bob.begin_fill()
def polygon(t,length, n):
for i in range(n):
bob.forward(length)
bob.left(360/n)
import math
def circle(t, r):
circum= 2*math.pi*r
n= int(circum/10)+1
length= circum/n
polygon(t,length, n)
circle(bob, 100)
bob.end_fill()
turtle.done()

How Can I Fill These Squares in Turtle - Python

I am trying to fill the color in these squares:
Right now the turtle only fills the corners of theses squares, not the entire square.
Here is my code:
import turtle
import time
import random
print ("This program draws shapes based on the number you enter in a uniform pattern.")
num_str = input("Enter the side number of the shape you want to draw: ")
if num_str.isdigit():
squares = int(num_str)
angle = 180 - 180*(squares-2)/squares
turtle.up
x = 0
y = 0
turtle.setpos(x,y)
numshapes = 8
for x in range(numshapes):
turtle.color(random.random(),random.random(), random.random())
x += 5
y += 5
turtle.forward(x)
turtle.left(y)
for i in range(squares):
turtle.begin_fill()
turtle.down()
turtle.forward(40)
turtle.left(angle)
turtle.forward(40)
print (turtle.pos())
turtle.up()
turtle.end_fill()
time.sleep(11)
turtle.bye()
I've tried moving around turtle.begin_fill() and end_fill() in numerous locations with no luckā€¦ Using Python 3.2.3, thanks.
I haven't really used turtle, but it looks like this may be what you want to do. Correct me if I've assumed the wrong functionality for these calls:
turtle.begin_fill() # Begin the fill process.
turtle.down() # "Pen" down?
for i in range(squares): # For each edge of the shape
turtle.forward(40) # Move forward 40 units
turtle.left(angle) # Turn ready for the next edge
turtle.up() # Pen up
turtle.end_fill() # End fill.
You're drawing a series of triangles, using begin_fill() and end_fill() for each one. What you can probably do is move your calls to begin_fill() and end_fill() outside the inner loop, so you draw a full square and then ask for it to be filled.
Use fill
t.begin_fill()
t.color("red")
for x in range(4):
t.fd(100)
t.rt(90)
t.end_fill()
Along with moving begin_fill() and end_fill() outside the loop, as several folks have mentioned, you've other issues with your code. For example, this is a no-op:
turtle.up
I.e. it doesn't do anything. (Missing parentheses.) This test:
if num_str.isdigit():
Doesn't do much for you as there is no else clause to handle the error. (I.e. when it isn't a number, the next statement simply uses the string as a number and fails.) This calculation seems a bit too complicated:
angle = 180 - 180*(squares-2)/squares
And finally there should be a cleaner way to exit the program. Let's address all these issues:
from turtle import Screen, Turtle
from random import random
NUMBER_SHAPES = 8
print("This program draws shapes based on the number you enter in a uniform pattern.")
num_str = ""
while not num_str.isdigit():
num_str = input("Enter the side number of the shape you want to draw: ")
sides = int(num_str)
angle = 360 / sides
delta_distance = 0
delta_angle = 0
screen = Screen()
turtle = Turtle()
for x in range(NUMBER_SHAPES):
turtle.color(random(), random(), random())
turtle.penup()
delta_distance += 5
turtle.forward(delta_distance)
delta_angle += 5
turtle.left(delta_angle)
turtle.pendown()
turtle.begin_fill()
for _ in range(sides):
turtle.forward(40)
turtle.left(angle)
turtle.forward(40)
turtle.end_fill()
screen.exitonclick()

Categories

Resources