Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty - - PowerPoint PPT Presentation

computer programming
SMART_READER_LITE
LIVE PREVIEW

Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty - - PowerPoint PPT Presentation

Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering IIT Bombay Session: Elementary Graphics Guest Lecturer: Dr. Abhiram Ranade Elementary Graphics Elementary Graphics A picture


slide-1
SLIDE 1

Computer Programming

  • Dr. Deepak B Phatak
  • Dr. Supratik Chakraborty

Department of Computer Science and Engineering IIT Bombay Session: Elementary Graphics Guest Lecturer: Dr. Abhiram Ranade

slide-2
SLIDE 2

Elementary Graphics

slide-3
SLIDE 3

Elementary Graphics

”A picture is worth a thousand words.”

slide-4
SLIDE 4

Elementary Graphics

”A picture is worth a thousand words.”

◮ Pictures, graphs, charts, maps, diagrams are often easier to

understand than text.

slide-5
SLIDE 5

Elementary Graphics

”A picture is worth a thousand words.”

◮ Pictures, graphs, charts, maps, diagrams are often easier to

understand than text.

◮ Especially true in science and technology, where the

information is often geometrical.

slide-6
SLIDE 6

Elementary Graphics

”A picture is worth a thousand words.”

◮ Pictures, graphs, charts, maps, diagrams are often easier to

understand than text.

◮ Especially true in science and technology, where the

information is often geometrical.

◮ Animations can also be very useful.

slide-7
SLIDE 7

Elementary Graphics

”A picture is worth a thousand words.”

◮ Pictures, graphs, charts, maps, diagrams are often easier to

understand than text.

◮ Especially true in science and technology, where the

information is often geometrical.

◮ Animations can also be very useful. ◮ Graphical input, e.g. clicking on the screen is also very

convenient.

slide-8
SLIDE 8

Elementary Graphics

”A picture is worth a thousand words.”

◮ Pictures, graphs, charts, maps, diagrams are often easier to

understand than text.

◮ Especially true in science and technology, where the

information is often geometrical.

◮ Animations can also be very useful. ◮ Graphical input, e.g. clicking on the screen is also very

convenient. This is what we study in the next few sessions...

slide-9
SLIDE 9

Outline of this session

◮ Introduction to our graphics package, Simplecpp ◮ Turtle graphics

slide-10
SLIDE 10

Introduction

slide-11
SLIDE 11

Introduction

Simplecpp: A graphics package developed for use with ”An Introduction to Programming through C++”, McGraw Hill Education 2014.

slide-12
SLIDE 12

Introduction

Simplecpp: A graphics package developed for use with ”An Introduction to Programming through C++”, McGraw Hill Education 2014. Simplecpp is available for Unix and Windows at www.cse.iitb.ac.in/∼ranade/simplecpp

slide-13
SLIDE 13

Introduction

Simplecpp: A graphics package developed for use with ”An Introduction to Programming through C++”, McGraw Hill Education 2014. Simplecpp is available for Unix and Windows at www.cse.iitb.ac.in/∼ranade/simplecpp Two kinds of graphics supported

slide-14
SLIDE 14

Introduction

Simplecpp: A graphics package developed for use with ”An Introduction to Programming through C++”, McGraw Hill Education 2014. Simplecpp is available for Unix and Windows at www.cse.iitb.ac.in/∼ranade/simplecpp Two kinds of graphics supported

◮ Turtle graphics

slide-15
SLIDE 15

Introduction

Simplecpp: A graphics package developed for use with ”An Introduction to Programming through C++”, McGraw Hill Education 2014. Simplecpp is available for Unix and Windows at www.cse.iitb.ac.in/∼ranade/simplecpp Two kinds of graphics supported

◮ Turtle graphics ◮ Co-ordinate based graphics

slide-16
SLIDE 16

Turtle Graphics

slide-17
SLIDE 17

Turtle Graphics

Invented in 1960s by Seymour Pappert, as part of the Logo programming language for teaching programming to children.

slide-18
SLIDE 18

Turtle Graphics

Invented in 1960s by Seymour Pappert, as part of the Logo programming language for teaching programming to children. Turtle: a symbolic animal that lives on the screen.

slide-19
SLIDE 19

Turtle Graphics

Invented in 1960s by Seymour Pappert, as part of the Logo programming language for teaching programming to children. Turtle: a symbolic animal that lives on the screen. Moves as per commands issued by the program.

slide-20
SLIDE 20

Turtle Graphics

Invented in 1960s by Seymour Pappert, as part of the Logo programming language for teaching programming to children. Turtle: a symbolic animal that lives on the screen. Moves as per commands issued by the program. Has a pen, which draws on the screen as the turtle moves.

slide-21
SLIDE 21

Turtle Graphics

Invented in 1960s by Seymour Pappert, as part of the Logo programming language for teaching programming to children. Turtle: a symbolic animal that lives on the screen. Moves as per commands issued by the program. Has a pen, which draws on the screen as the turtle moves. Goal of turtle graphics: Draw interesting pictures on the screen.

slide-22
SLIDE 22

A simple program fragment

turtleSim(); // Start turtle graphics // Turtle appears at the center. forward(100); // Move forward 100 pixels. right(90); // Turn right 90 degrees. forward(100); right(90); forward(100); right(90); forward(100);

slide-23
SLIDE 23

A simple program fragment

turtleSim(); // Start turtle graphics // Turtle appears at the center. forward(100); // Move forward 100 pixels. right(90); // Turn right 90 degrees. forward(100); right(90); forward(100); right(90); forward(100); What does this draw? Pretend you are the turtle!

slide-24
SLIDE 24

The full program

#include <simplecpp> int main(){ turtleSim(); forward(100); wait(0.5); right(90); wait(0.5); forward(100); wait(0.5); right(90); wait(0.5); forward(100); wait(0.5); right(90); wait(0.5); forward(100); wait(5); }

slide-25
SLIDE 25

The full program

#include <simplecpp> int main(){ turtleSim(); forward(100); wait(0.5); right(90); wait(0.5); forward(100); wait(0.5); right(90); wait(0.5); forward(100); wait(0.5); right(90); wait(0.5); forward(100); wait(5); }

◮ #include <simplecpp>: This causes graphics functionality

to be included.

slide-26
SLIDE 26

The full program

#include <simplecpp> int main(){ turtleSim(); forward(100); wait(0.5); right(90); wait(0.5); forward(100); wait(0.5); right(90); wait(0.5); forward(100); wait(0.5); right(90); wait(0.5); forward(100); wait(5); }

◮ #include <simplecpp>: This causes graphics functionality

to be included.

◮ wait: This causes the program to wait for the specified

number of seconds. If we dont put in wait statements, the execution will happen too fast and we will not see anything.

slide-27
SLIDE 27

Demonstration

slide-28
SLIDE 28

Functions to command the turtle

slide-29
SLIDE 29

Functions to command the turtle

◮ forward(double D) Moves turtle forward by D pixels.

D may be negative, in which case the turtle moves back.

slide-30
SLIDE 30

Functions to command the turtle

◮ forward(double D) Moves turtle forward by D pixels.

D may be negative, in which case the turtle moves back.

◮ right(double A) Turns the turtle right by A degrees.

slide-31
SLIDE 31

Functions to command the turtle

◮ forward(double D) Moves turtle forward by D pixels.

D may be negative, in which case the turtle moves back.

◮ right(double A) Turns the turtle right by A degrees. ◮ left(double A) Turns the turtle left by A degrees.

slide-32
SLIDE 32

Functions to command the turtle

◮ forward(double D) Moves turtle forward by D pixels.

D may be negative, in which case the turtle moves back.

◮ right(double A) Turns the turtle right by A degrees. ◮ left(double A) Turns the turtle left by A degrees. ◮ penUp() The pen is raised. Drawing stops until it is lowered

again.

slide-33
SLIDE 33

Functions to command the turtle

◮ forward(double D) Moves turtle forward by D pixels.

D may be negative, in which case the turtle moves back.

◮ right(double A) Turns the turtle right by A degrees. ◮ left(double A) Turns the turtle left by A degrees. ◮ penUp() The pen is raised. Drawing stops until it is lowered

again.

◮ penDown() The pain is lowered. Drawing resumes.

slide-34
SLIDE 34

Functions to command the turtle

◮ forward(double D) Moves turtle forward by D pixels.

D may be negative, in which case the turtle moves back.

◮ right(double A) Turns the turtle right by A degrees. ◮ left(double A) Turns the turtle left by A degrees. ◮ penUp() The pen is raised. Drawing stops until it is lowered

again.

◮ penDown() The pain is lowered. Drawing resumes. ◮ wait(double S) Wait for S seconds.

slide-35
SLIDE 35

Demo: drawing a decorative plate border

slide-36
SLIDE 36

Demo: drawing a decorative plate border

You should try to figure out how this is done.

slide-37
SLIDE 37

Demo: drawing a decorative plate border

You should try to figure out how this is done. Make sure you can repeat the pattern an arbitrary number of times, and yet close it smoothly.

slide-38
SLIDE 38

Demo: drawing a decorative plate border

You should try to figure out how this is done. Make sure you can repeat the pattern an arbitrary number of times, and yet close it smoothly. Make the drawing graceful, e.g. there should be no sharp corners.

slide-39
SLIDE 39

Demo: Drawing a tree

slide-40
SLIDE 40

Demo: Drawing a tree

Key observation: A tree can be viewed as a trunk, on top of which are two trees, at an angle.

slide-41
SLIDE 41

Demo: Drawing a tree

Key observation: A tree can be viewed as a trunk, on top of which are two trees, at an angle. Natural to do this using recursion!

slide-42
SLIDE 42

Demo: Drawing a tree

Key observation: A tree can be viewed as a trunk, on top of which are two trees, at an angle. Natural to do this using recursion! We develop this next. Something similar is discussed in Chapter 10

  • f the book.
slide-43
SLIDE 43

Remarks

slide-44
SLIDE 44

Remarks

◮ Turtle graphics is discussed in chapter 1 of the book.

slide-45
SLIDE 45

Remarks

◮ Turtle graphics is discussed in chapter 1 of the book. ◮ The most interesting use of turtle graphics is for drawing

pictures which have interesting symmetry.

slide-46
SLIDE 46

Remarks

◮ Turtle graphics is discussed in chapter 1 of the book. ◮ The most interesting use of turtle graphics is for drawing

pictures which have interesting symmetry. You have to figure out the symmetry and represent it suitably in your code.

slide-47
SLIDE 47

Remarks

◮ Turtle graphics is discussed in chapter 1 of the book. ◮ The most interesting use of turtle graphics is for drawing

pictures which have interesting symmetry. You have to figure out the symmetry and represent it suitably in your code.

◮ Iterative symmetry: Same pattern is repeated, e.g. decorative

plate.

slide-48
SLIDE 48

Remarks

◮ Turtle graphics is discussed in chapter 1 of the book. ◮ The most interesting use of turtle graphics is for drawing

pictures which have interesting symmetry. You have to figure out the symmetry and represent it suitably in your code.

◮ Iterative symmetry: Same pattern is repeated, e.g. decorative

plate.

◮ Recursive symmetry: Part of the picture is similar to the

whole, e.g. tree.

slide-49
SLIDE 49

Remarks

◮ Turtle graphics is discussed in chapter 1 of the book. ◮ The most interesting use of turtle graphics is for drawing

pictures which have interesting symmetry. You have to figure out the symmetry and represent it suitably in your code.

◮ Iterative symmetry: Same pattern is repeated, e.g. decorative

plate.

◮ Recursive symmetry: Part of the picture is similar to the

whole, e.g. tree.

◮ Drawing highly patterned pictures, where you can supply

parameters to change the amount of iteration or recursion can be a challenging project.