Main Points: - Python Turtle - Fractals

Slides:



Advertisements
Similar presentations
Graphics Shapes. Setup for using graphics You have to import the graphics library You can use either “import graphics” or “from graphics import *” or.
Advertisements

CS 100: Roadmap to Computing Fall 2014 Lecture 02: Fun With Turtles.
Python Basics: Statements Expressions Loops Strings Functions.
18/2/00SEM107 - © Kamin & Reddy Class 7 - LineList - 1 Class 7 - Line Drawings  The LineList data type r Recursive methods to construct line drawings.
PYTHON FRUITFUL FUNCTIONS CHAPTER 6 FROM THINK PYTHON HOW TO THINK LIKE A COMPUTER SCIENTIST.
ENS 207 engineering graphics
COMPSCI 105 S Principles of Computer Science
Chapter 16 Recursion: Another Control Mechanism. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. a.
Cosc 1P02 Week 2 Lecture slides
Recursion.
FRACTALS. WHAT ARE FRACTALS? Fractals are geometric figures, just like rectangles, circles, and squares, but fractals have special properties that those.
Recursion Definition: A method that calls itself. Recursion can be direct or indirect. Indirect recursion involves a method call that eventually recalls.
L systems (Aristid Lindenmayer)
Simulating Trees with Fractals and L-Systems Eric M. Upchurch CS 579.
Fundamentals of Python: From First Programs Through Data Structures
CS4395: Computer Graphics 1 Fractals Mohan Sridharan Based on slides created by Edward Angel.
Definition of Trigonometric Functions With trigonometric ratios of acute angles in triangles, we are limited to angles between 0 and 90 degrees. We now.
Turtle Graphics  Turtle graphics provide a simple way to draw pictures in a window.  The name suggests the way in which we can think about the drawing.
Log on and Download from website:
Recursive Methods Noter ch.2. QUIZ What is the result of the method call foo(4) ? 1.Prints 4 2.Prints 1 3.Prints Prints Compiler error:
Programming Training Main Points: - Lists / Arrays in Python. - Fundamental algorithms on Arrays.
Programming Training kiddo Main Points: - Python Statements - Problems with selections.
Algorithmic Recursion. Recursion Alongside the algorithm, recursion is one of the most important and fundamental concepts in computer science as well.
Introduction to Computing Using Python Recursion and Algorithm Development  Introduction to Recursion  Recursion Examples  Run Time Analysis  Search.
L-Systems and Procedural Plants CSE 3541 Matt Boggus.
Math 2 Geometry Based on Elementary Geometry, 3 rd ed, by Alexander & Koeberlein 1.2 Informal Geometry and Measurement.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-
Microsoft® Small Basic
Python Programming in Context Chapter 9. Objectives To introduce functional programming style To practice writing recursive functions To introduce grammars.
Ch 9 Infinity page 1CSC 367 Fractals (9.2) Self similar curves appear identical at every level of detail often created by recursively drawing lines.
Examining the World of Fractals. Myles Akeem Singleton Central Illinois Chapter National BDPA Technology Conference 2006 Los-Angeles, CA.
Computer Science 111 Fundamentals of Programming I Introduction to Graphics.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 7 The Game Loop and Animation Starting Out with Games & Graphics.
1 CSC 221: Computer Programming I Fall 2011 Fun with turtle graphics  turtle module  relative motion (setup, reset, left, right, forward, backward) 
Class 2 Introduction to turtle graphics
Turtle Graphics  Turtle graphics provide a simple way to draw pictures in a window.  The name suggests the way in which we can think about the drawing.
1 Turtle Graphics and Math Functions And how you can use them to draw cool pictures!
Loops & Graphics IP 10 Mr. Mellesmoen Recall Earlier we wrote a program listing numbers from 1 – 24 i=1 start: TextWindow.WriteLine(i) i=i+1 If.
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Session 4 Conditionals Turtles.
Programming Training kiddo Main Points: - Python Statements - Problems with selections.
Descriptive Geometry. Introduction  What is Descriptive Geometry? →It is the study of points, lines, and planes in space to determine their locations.
Let’s Learn 3. Modules Saenthong School, January – February 2016
Pablo Revelo. Birds: to 3birds pu home pd lt 80 pu fd 250 pd setpensize 2 setpc "white st bird pu bk random 100 bird pu lt 90 fd random 40 bird ht.
Increase the number of lines before coming back to the origin … triangle square Draw a circle.
Visual C++ Programming: Concepts and Projects Chapter 10A: Recursion (Concepts)
Turtle Graphics Lesson 2 1. There are 3 homeworks to complete during the six lessons of this unit. Your teacher will let you know when a homework has.
Functions. functions: a collection of lines of code with a name that one can call. Functions can have inputs and outputs.
Programming Training Main Points: - More Fundamental algorithms on Arrays. - Reading / Writing from files - Problem Solving.
3. Drawing Let’s Learn Saengthong School, June – August 2016 Teacher: Aj. Andrew Davison, CoE, PSU Hat Yai Campus
Experimenting with Grammars to Generate L-Systems – in JFLAP March 31, 2011 Prof. Susan Rodger Computer Science Dept.
Using the Python Turtle
Python Turtle Graphics
Graphics CIS 40 – Introduction to Programming in Python
For vs. While loop count=0 while count< 5: for count in range(5):
Main Points: - Python Turtle - Fractals
General Form for a Conditional
Example: Card Game Create a class called “Card”
Frozen Graphics Lesson 3.
CS 100: Roadmap to Computing
البرمجة مع لغة PYTHON TURTLE
CSc 110, Spring 2018 Lecture 9: Parameters, Graphics and Random
CS 100: Roadmap to Computing
Chap. 3 Functions Start Python IDLE (Shell) and open a new file.
Programming Training Main Points:
Main Points: - Python Turtle - Fractals
Recursion Think ESCHER.
Introduction to Turtle Graphics
Computer Graphics - Lecture 6
From Barb Ericson Georgia Institute of Technology June 2008
Using Modules.
Presentation transcript:

Main Points: - Python Turtle - Fractals Programming Training Main Points: - Python Turtle - Fractals

Arrays / Lists Python lists represent sequence of [similar] elements - between [ ] - separated by , - which are indexed. [ ]  represents lists / arrays. Examples: x = [1, 2, 3, 1, 0]  list of 5 int numbers name = [‘s’, ’a’, ’b’, ’i’, ’n’]  list of 5 string elements list = [1, 2, ‘sabin’, [0, 1, 3]]  list with elements of different nature

Arrays / Lists Indexing Python lists elements can be indexed using indices 0, 1, 2, …. A list l with n elements has the elements l[0], l[1], l[2],…, l[n-1]. l represents the list while l[i] is an element of the list. the first element of the list is indexed with 0. it is possible to have negative indexing. For a list l, the number of elements is given by the function len len(l)  number of elements in l. ANY COMPUTATION WITH ARRAYS MUST USE THE LOOP FOR.

Screen The screen is a rectangle of pixels on which we can draw. The screen origin is usually in the centre. Each pixel will have two coordinates which are float numbers.

Python Turtle turtle – a Python module / library for drawing. What can turtles (as animals) do? Move forward or backward. Turn left or right. Retreat the head into the shell. Turtle-like Graphics Pens have: x, y  give the turtle pen coordinates. dir  the direction to go in radians. pen  the pen’s status (pen=1 draws line)

Python Turtle turtle – a Python module / library for drawing. Function to Move and Draw forward(d) – pen moves forward for distance d on the direction to go backward(d) – pen moves backward for distance on the direction to go right(angle) – pen turns right with angle (degrees) left(angle) – pen turns left with angle (degrees) goto(x, y) – pen moves to the point (x,y) drawing a line dot(size, color) – pen draws a dot of specified size with the given color circle(radius) – pen draws a circle with the specified radius circle(radius)

Python Turtle turtle – a Python module / library for drawing. Function to Control the pen penup() – pen moves up so no drawing pendown() – pen goes down so it can draw width(w) – sets the thickness of the line color(colorname) – pen color is set as colorname color(r, g, b) – the color is set based on the r, g, b values.

Python Turtle turtle – a Python module / library for drawing. Function to Control the Screen bgcolor(color) – the screen background color is set to color. clear() – the screen is cleared. screensize() – it returns the width and the height of the screen

Python Turtle How to work with: Make a pen object pen = Pen() Set the pen features like color, width, etc pen.color(‘red’) pen.width(3) Make you drawing using the move/draw functions pen.forward(100) pen.left(90)

Python Turtle What is the outcome of Can be re written as pen.forward(100) pen.left(90) Can be re written as for i in range(4) : pen.forward(100) pen.left(90)

Python Turtle – Draw a Triangle Make a pen and set some features Draw the triangle A(x1, y1)B(x2, y2)C(x3,y3) - move the pen to A(x1, y1) - Draw AB using goto(x2, y2) - Draw BC using goto(x3, y3) - Draw CA using goto(x1, y1)

Python Turtle – Draw a Random Circle random a new module to generate random numbers random.randint(a,b)  generate a random integer between a and b random.random()  generate a random float between [0, 1) Drawing a random circle - get a random radius - get a random color - get a random width - draw the circle

Turtle Random Art Develop methods to draw random shapes Drawing few similar random shapes in your screen Draw 30 random circles + 20 random triangles

Python Recursive Functions A Python function can call any known / seen function from the imported modules or from the file. A function is recursive when it calls itself. Important Rule: A recursive method must have - a termination step that solves directly the problem - a recursion step.

Python Recursive Functions Fibonacci sequence: 1, 1, 2, 3, 5, 8, 13, 21, … fib(0) = fib(1) = 1 and fib(n) = fib(n-1)+ fib(n-2) def fib(n): # termination if n== 0 or n==1 : return 1 # endif return fib(n-1) + fib(n-2) # end fib

Recursive / Turtle Graphics 1. Define Recursively the figure Fn. - Termination: give the shape of F0 (point, line, etc) - Recursion: define Fn based on Fn-1. 2. Use a Turtle object to draw the figure. - The direction of the Turtle object must be preserved. Turtle Geometry represents the simplest way to construct geometrical fractals. Important Fractals: Trees, Koch’s curves, Sierpinski’s curves etc.

The Binary Tree The Binary Tree T(n,l) n is the order or age l is the length

The Binary Tree The Binary Tree T(n,l) T(0,l)= nothing n is the order or age l is the length T(0,l)= nothing T(n,l) is defined as follows: - construct the trunk - left 45 (PI/4) - construct T(n-1,l/2) - right 90 (PI/2) - go back at the root

The Binary Tree

The Fern F(n,l) The Fern Tree F(n,l) n - the order or the age l – the length of the curve

The Fern F(n,l) The Fern F(n,l) n - the order or the age l – the length of the curve The Fern F(n,l) is a tree with 3 asymmetric branches. The Fern depends the braches angles and the branches trunks

The Fern F(n,l) The Fern F(n,l) is defined by: F(0,l) is a dot or nothing. F(n,l) is recursively defined by: Forward (0.3*l) Left 55; Construct F(n-1, l/2);Right 55 Forward (0.7*l) Right 40; Construct F(n-1, l/2); Left 40 Forward l Left 5; Construct F(n-1, 0.8*l); Right 5 Backward 2*l

The Fern F(n,l) def fern(n,l): } if n==0 or l<2: return # endif CONCEPT 4 def fern(n,l): if n==0 or l<2: return # endif pen.forward (0.3*l); pen.left(55); fern(n-1, l/2);pen.right(55); pen.forward (0.7*l); pen.right(40); fern(n-1, l/2); pen.left(40); pen.forward(l); pen.left(5); fern(n-1, 0.8*l); pen.right(5); pen.backward(2*l); }

The Koch Curve The Koch Curve K(n,l) n - the order or the age l – the length of the curve

The Koch Curve The Koch Curve K(n,l) CONCEPT 5 The Koch Curve K(n,l) n - the order or the age l – the length of the curve K(n,l) is defined as follows: The Koch Curve K(n,l) is defined by - construct K(n-1,l/3); - left 60 (PI/3); construct K(n-1,l/3) - right 120 (2PI/3); construct K(n-1,l/3)

The Koch Curve The snow flake F(n,k) - construct K(n,l); left 120 (2PI/3); F(n,k) is an infinite curve bounding a finite area.

The Koch Curve def koch(n, l): if l<2 or n==0: def flake(n, l): t.forward(l) return #endif koch(n-1,l/3) pen.left(60) pen.right(120) koch(n-1,l/3); # end koch def flake(n, l): for in range(3): koch(n,l,g) t.left(120) # endfor # end flake

The Sierpinski Curve S(n,l) n - the order or the age l – the length of the curve S(n,l) is formed with 4 S(n-1,l)

S(n,l) is recursively defined by The Sierpinski Curve S(0,l) is nothing S(n,l) is recursively defined by Construct S(n-1,l) Right 45; Forward d; Right 45; Construct S(n-1,l); Left 90;Forward l; Left 90;

To do List Solve the HW problems. Read more about the turtle module