Question

Python = Modify this chapter's case study program (the c-curve) so that it draws the line...

Python = Modify this chapter's case study program (the c-curve) so that it draws the line segments using random colors. code to amend:

from turtle import Turtle
def cCurve(t, x1, y1, x2, y2, level):
   def drawLine(x1, y1, x2, y2):
      """Draws a line segment between the endpoints."""
      t.up()
      t.goto(x1, y1)
      t.down()
      t.goto(x2, y2)
   if level == 0:
      drawLine(x1, y1, x2, y2)
   else:
      xm = (x1 + x2 + y1 - y2) // 2
      ym = (x2 + y1 + y2 - x1) // 2
      cCurve(t, x1, y1, xm, ym, level - 1)
      cCurve(t, xm, ym, x2, y2, level - 1)
def main():
   level = input(int("Enter the level (0 or greater): "))
   t = Turtle()
   t.hideturtle()
   cCurve(t, 50, -50, 50, 50, level)

main()

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code

from turtle import Turtle
import random
colors = ["red","green","blue","orange","purple","pink","yellow","black","brown"]
def cCurve(t, x1, y1, x2, y2, level):
def drawLine(x1, y1, x2, y2):
"""Draws a line segment between the endpoints."""
color = random.choice(colors)
t.color(color)
t.up()
t.goto(x1, y1)
t.down()
t.goto(x2, y2)
if level == 0:
drawLine(x1, y1, x2, y2)
else:
xm = (x1 + x2 + y1 - y2) // 2
ym = (x2 + y1 + y2 - x1) // 2
cCurve(t, x1, y1, xm, ym, level - 1)
cCurve(t, xm, ym, x2, y2, level - 1)
def main():
level = int(input("Enter the level (0 or greater): "))
t = Turtle()
t.hideturtle()
cCurve(t, 50, -50, 50, 50, level)
t.getscreen()._root.mainloop()
main()

output

code snaps

If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.

Add a comment
Know the answer?
Add Answer to:
Python = Modify this chapter's case study program (the c-curve) so that it draws the line...
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
  • The Koch snowflake is a fractal shape. At level 0, the shape is an equilateral triangle....

    The Koch snowflake is a fractal shape. At level 0, the shape is an equilateral triangle. At level 1, each line segment is split into four equal parts, producing an equilateral bump in the middle of each segment. Figure 7-15 shows these shapes at levels 0, 1, and 2. Figure 7-15 First three levels of a Koch snowflake Figure 7-15 First three levels of a Koch snowflake At the top level, the script uses a function drawFractalLine to draw three...

  • Using python Here is the code import turtle def draw_triangle(vertex, length, k): ''' Draw a ...

    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...

  • A random walk is a particular kind of probabilistic (pseudo-random) simulation that models certai...

    A random walk is a particular kind of probabilistic (pseudo-random) simulation that models certain statistical systems, such as Brownian motion of particles or molecules. Coin flipping is an example of a one-dimensional random walk--one dimensional because you only can go forward (when you flip heads) or backward (when you flip tails) along a straight line. Suppose you take a random walk of nsteps. How many steps away from your starting point would you expect to end up on average, if...

  • Python 3 Modify the sentence-generator program of Case Study so that it inputs its vocabulary from...

    Python 3 Modify the sentence-generator program of Case Study so that it inputs its vocabulary from a set of text files at startup. The filenames are nouns.txt, verbs. txt, articles.txt, and prepositions.txt. (HINT: Define a single new function, getWords. This function should expect a filename as an argument. The function should open an input file with this name, define a temporary list, read words from the file, and add them to the list. The function should then convert the list...

  • Intro to Python: Q1: Bonnie is writing a Python program for her math class so she can check her answers quickly. She is...

    Intro to Python: Q1: Bonnie is writing a Python program for her math class so she can check her answers quickly. She is having some difficulties, however, because when she enters values like 3.5 or 2.71 her program throws an error. She has heard of your expertise in Python and asks for your help. What error is being thrown and how can she fix the program to accept values like 3.5 or 2.71? from math import sgrt def distance (coorl,...

  • Have been working on this code for awhile and cant get it to work with Glut....

    Have been working on this code for awhile and cant get it to work with Glut. Wondering if there is anything I can fix #include <iostream> #include "graph1.h" using namespace std; //Add Function Prototypes Here int getNoPoints(); void getPoints(int x[], int y[], int no_points); void drawPolyLine(int x[], int y[], int no_points); int main() {    //Variable Declaration/Initialization    int no_points = 0;    const int MAX_POINTS = 10;    int x[MAX_POINTS];    int y[MAX_POINTS];    //Display Graphics Window    displayGraphics();...

  • Game Description: Most of you have played a very interesting game “Snake” on your old Nokia...

    Game Description: Most of you have played a very interesting game “Snake” on your old Nokia phones (Black & White). Now it is your time to create it with more interesting colors and features. When the game is started a snake is controlled by up, down, left and right keys to eat food which appears on random locations. By eating food snake’s length increases one unit and player’s score increases by 5 points. Food disappears after 15 seconds and appears...

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