Question
Using python

Here is the code

import turtle

def draw_triangle(vertex, length, k):
'''
Draw a triangle at depth k given the bottom vertex.
  
vertex: a tuple (x,y) that gives the coordinates of the bottom vertex.
When k=0, vertex is the left bottom vertex for the outside triangle.
length: the length of the original outside triangle (the biggest one).
k: the depth of the input triangle.
As k increases by 1, the triangles shrink to a smaller size.
When k=0, the function will draw the outside triangle.
'''
t.up()
t.goto(vertex)
if k > 0:
t.setheading(60)
t.down()
for i in range(3):
t.forward(length/2**k)
t.left(120)
return(None)

def new_vertex(vertex, length, k):
'''
Find the the bottom vertexes of three surrounding triangles at depth k+1
for the triangle at depth k with the bottom vertex.
  
vertex: a tuple (x,y) that gives the coordinates of the bottom vertex.
When k=0, vertex is the left bottom vertex for the outside triangle.
length: the length of the original outside triangle (the biggest one).
k: the depth of the input triangle.
As k increases by 1, the triangles shrink to a smaller size.
'''
if k==0:
return([(vertex[0]+length/2,vertex[1])])
x = vertex[0]
y = vertex[1]
x1 = x
y1 = y + length/2**(k+1)*(3**0.5)
x2 = x - length/2**(k+1)
y2 = y
x3 = x + length/2**(k+1)
y3 = y
return([(x1,y1),(x2,y2),(x3,y3)])

win = turtle.Screen()
win.setworldcoordinates(-10,-10,110,100)
t = turtle.Turtle(visible=False)
t.speed(2000)

sideLen = 100

draw_triangle((0,0),sideLen,0)
print(new_vertex((0,0),sideLen,0))

draw_triangle((sideLen/2,0),sideLen,1)
print(new_vertex((sideLen/2,0),sideLen,1))

draw_triangle((sideLen/4,0),sideLen,2)
print(new_vertex((sideLen/4,0),sideLen,2))
  
win.exitonclick()

(5 points) Complete the following code to create a Sierpinski triangle with n levels, where n is a positive integer indicatin
0 0
Add a comment Improve this question Transcribed image text
Answer #1

from turtle import *
size=800
min=64
pytho=0.8660255 # Pytho = squareroot of 3/2

def triangle(l,x,y):
if l > min:   
l=l/2   
triangle(l,x,y)
triangle(l,x+l,y)
triangle(l,x+l/2,y+l*pytho)   
else:   
goto(x,y); pendown()
begin_fill()
forward(l); left(120)   
forward(l); left(120)
forward(l)
end_fill()
setheading(0)   
penup(); goto(x,y)

penup()
speed('fastest')
triangle(size,-size/2,-size*pytho/2.0)
done()
# code is using recursion

Add a comment
Know the answer?
Add Answer to:
Using python Here is the code import turtle def draw_triangle(vertex, length, k): ''' Draw a ...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Here is a Python program. import turtle class Box:     def __init__(self, x, y, width, height):...

    Here is a Python program. import turtle class Box:     def __init__(self, x, y, width, height):         self.x = x         self.y = y         self.width = width         self.height = height     def show(self):         turtle.up()         turtle.goto(x, y)         turtle.down()         for _ in range(2):             turtle.forward(width)             turtle.right(90)             turtle.forward(height)             turtle.right(90) bigbox = Box(-100, -150, 200, 300) bigbox.show() turtle.done() If you run the above program, it will produce an error. What is the keyword that has...

  • Answer must be in Python 3 Answer: import turtle def trifecta(size,angle,num): for i in range(num): tri(t,size)...

    Answer must be in Python 3 Answer: import turtle def trifecta(size,angle,num): for i in range(num): tri(t,size) t.right(angle) t = turtle.Turtle() s = turtle.Screen() trifecta(100,25,5) Question 11 Part b (10 points) Write a function named trifecta() that takes three parameters: 1. size 2. angle 3. num trifecta() should call tri() repeatedly so as to draw num triangles, each with sides of length size. Each triangle should be oriented angle degrees clockwise from the preceding triangle. trifecta() should create a turtle and...

  • I need help with this code, I have it started here import sys import turtle menu...

    I need help with this code, I have it started here import sys import turtle menu = "Press 1 for Flower" UserChoice = input(menu) #function to draw squares def draw_square(square): for i in range(0,2): square.forward(100) square.right(70) square.forward(100) square.right(110) #function to draw flower def draw_flower(petalNumber): window = turtle.Screen() window.bgcolor("yellow") pen = turtle.Turtle() pen.shape("triangle") pen.color("red")    for number in range(0,petalNumber): draw_square(pen) pen.right(360/petalNumber) for i in range(0,4): pen.circle(20) pen.right(90)    pen.right(90) pen.forward(300) pen.right(90) draw_square(pen) pen.left(180) draw_square(pen) pen.left(270) pen.forward(200) window.exitonclick() #function for original art...

  • I cannot run this code! Please explain and fix it for me import turtle import random...

    I cannot run this code! Please explain and fix it for me import turtle import random # Approximate size of turtle objects in pixels BODY_SIZE = 80 # Half if the body size for collision detection with the edge of the screen HALF_BODY_SIZE = BODY_SIZE / 2 # if enemies get this close to friend there is collision COLLISION_DISTANCE = 5 # The window size SIZE = 500 # inner boundary within window for reversing the direction of enemies LOWER_BOUND...

  • Dle Edit Search View Encoding Language Settings Tools Macro Run Plugins Window 1 isport turtle 3 ...

    dle Edit Search View Encoding Language Settings Tools Macro Run Plugins Window 1 isport turtle 3 tina turtle.Turtle 4 SCreen-turtle.Screen ) 5 acreen.bgoolor ("white") 6 tina.shape ("turtle" 7tina.pencolor ("blue") SCALE -20 10 12 13 14 15 16 17 18 19 20 21 input: n # output: a drawing of n concentric circles centered at 0,0 # this function is written using iteration (using a loop) Edef concentric loop (n) : for size in range(n) : tina.penup tina goto (O, -SCALE...

  • import random import turtle def isInScreen(win,turt): leftBound = -win.window_width() / 2 rightBound = win.window_width() / 2...

    import random import turtle def isInScreen(win,turt): leftBound = -win.window_width() / 2 rightBound = win.window_width() / 2 topBound = win.window_height() / 2 bottomBound = -win.window_height() / 2 turtleX = turt.xcor() turtleY = turt.ycor() stillIn = True if turtleX > rightBound or turtleX < leftBound: stillIn = False if turtleY > topBound or turtleY < bottomBound: stillIn = False return stillIn def main(): wn = turtle.Screen() # Define your turtles here june = turtle.Turtle() june.shape('turtle') while isInScreen(wn,june): coin = random.randrange(0, 2) if...

  • Python. Just work in the def sierpinski. No output needed. Will give thumbs up for any attempt beginning this code. Your task is to implement this algorithm in Python, returning a random collection of...

    Python. Just work in the def sierpinski. No output needed. Will give thumbs up for any attempt beginning this code. Your task is to implement this algorithm in Python, returning a random collection of inum-100, 000 points. You should then plot the points to see the structure. Please complete the following function: def sierpinski (po, v, f, inum) The four arguments are ·po the initial point. You may assume this is the origin, i.e., po = [0, 0] . v:...

  • 11p Python Language Read the following code for oofraction. import oofraction class OOFraction: def main(): fl...

    11p Python Language Read the following code for oofraction. import oofraction class OOFraction: def main(): fl = oofraction.o0Fraction( 2, 5) f2 = oofraction.o0Fraction ( 1, 3) f3 = f1 + f2 print (str(3)) main() def __init__(self, Num, Den): self.mNum = Num self.mDen = Den return def_add_(self,other): num = self.mNumother.mDen + other.mNum*self.mDen den = self.mDen*other.mDen f = OOFraction(num,den) return f 1. Write the output below. def_str_(self): s = str( self.mNum )+"/" + str( self.mDen) returns 2. Write a class called wholeNum...

  • I need help with my python class. thanks! Question 12 Code Example 4-4 main program: import...

    I need help with my python class. thanks! Question 12 Code Example 4-4 main program: import arithmetic as a def multiply(num1, num2):     product = num1 * num2     result = a.add(product, product)     return result     def main():     num1 = 4     num2 = 3     answer = multiply(num1, num2)     print("The answer is", answer) if __name__ == "__main__":     main() arithmetic module: def add(x, y):     z = x + y     return z Refer to Code...

  • convert the following code from python to java. def topkFrequent(nums, k): if not nums: return [...

    convert the following code from python to java. def topkFrequent(nums, k): if not nums: return [ ] if len(nums) == 1: return nums [0] # first find freq freq dict d = {} for num in nums: if num in d: d[num] -= 1 # reverse the sign on the freq for the heap's sake else: d[num] = -1 h = [] from heapq import heappush, heappop for key in di heappush(h, (d[key], key)) res = [] count = 0...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT