Cse536 Functional Programming 1 6/11/2015 Lecture #5, Oct 11, 2004 Reading Assignments –Finish Chapter 3, Simple Graphics –Read Chapter 4, Shapes II, Drawing.

Slides:



Advertisements
Similar presentations
1 Line replacement approach Koch Star (Snowflake) Area replacement approach Sierpenski Triangle Iterated Function System approach Heighway Dragon CSE
Advertisements

Cse536 Functional Programming 1 1/13/2015 Lecture #13, Nov. 08, 2004 Today’s Topics –Simple animations –Buffered graphics –Animations in Haskell –Complex.
18/2/00SEM107 - © Kamin & Reddy Class 7 - LineList - 1 Class 7 - Line Drawings  The LineList data type r Recursive methods to construct line drawings.
Graphics You draw on a Graphics object The Graphics object cannot directly be created by your code, instead one is generated when the method paintComponent.
Working with images and scenes CS 5010 Program Design Paradigms “Bootcamp” Lesson 2.5 TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
Processing Lecture. 1 What is processing?
PHY-102 SAPIntroductory GraphicsSlide 1 Introductory Graphics In this section we will learn how about how to draw graphics on the screen in Java:  Drawing.
Chapter 6 Shapes III: Perimeters of Shapes. The Perimeter of a Shape  To compute the perimeter we need a function with four equations (1 for each Shape.
Chapter 5 Creating an Image Map.
Cse536 Functional Programming 1 6/10/2015 Lecture #6, Oct 13, 2004 Reading Assignments –Read chapter 5 of the Text Polymorphic and Higher-Order Functions.
Cse536 Functional Programming 1 6/10/2015 Lecture 10, Oct. 29, 2004 Special Rescheduled Friday Lecture Today’s Topics – Pictures – Low level graphics bit-maps:
Cse536 Functional Programming 1 6/10/2015 Lecture #8, Oct. 20, 2004 Todays Topics –Sets and characteristic functions –Regions –Is a point in a Region –Currying.
Chapter 3 Simple Graphics. Side Effects and Haskell  All the programs we have seen so far have no “side-effects.” That is, programs are executed only.
Shapes and the Coordinate System TEKS 8.6 (A,B) & 8.7 (A,D)
Advanced Programming Handout 8 Drawing Regions (SOE Chapter 10)
Chapter 4 Shapes II: Drawing Shapes. Recall the Shape Datatype data Shape = Rectangle Side Side | Ellipse Radius Radius | RtTriangle Side Side | Polygon.
Chapter 10 Drawing Regions. Pictures  Drawing Pictures Pictures are composed of Regions o Regions are composed of shapes Pictures add Color data Picture.
Advanced Programming Handout 4. Introductions  Me: Benjamin C. Pierce (known as Benjamin, or, if you prefer, Dr. Pierce, but not Ben or Professor) 
Insight Through Computing 8. More Practice with Iteration and Conditionals Through Graphics For-Loop Problems Introduce While-Loops.
Cse536 Functional Programming 1 6/28/2015 Lecture #3, Oct 4, 2004 Reading Assignments –Finish chapter 2 and begin Reading chapter 3 of the Text Today’s.
Copyright 2006 by Pearson Education 1 Building Java Programs Supplement 3G: Graphics.
Four simple expressions in meta. Data objects Pieces of data in a computer are called objects Today, we’ll talk about four kinds of objects Numbers Pictures.
Chapter 5 Polymorphic and Higher-Order Functions.
CS123 | INTRODUCTION TO COMPUTER GRAPHICS Andries van Dam © Scan Conversion CS123 1 of 44Scan Conversion - 10/14/2014.
Translations, Rotations, Reflections, and Dilations M7G2.a Demonstrate understanding of translations, dilations, rotations, reflections, and relate symmetry.
TEKS 8.6 (A,B) & 8.7 (A,D) This slide is meant to be a title page for the whole presentation and not an actual slide. 8.6 (A) Generate similar shapes using.
CS CS1321: Introduction to Programming Georgia Institute of Technology College of Computing Lecture 5 Sept 4th, 2001 Fall Semester.
1 Excursions in Modern Mathematics Sixth Edition Peter Tannenbaum.
Log on and Download from website:
Motion Geometry Part I Geometry Solve Problems Organize Model Compute
Today we will derive and use the formula for the area of a triangle by comparing it with the formula for the area of a rectangle. derive = obtain or receive.
Chapter 5 Creating an Image Map.
Preparing for FRP Shapes, Regions, and Drawing. Shape types from the Text data Shape = Rectangle Float Float | Ellipse Float Float | RtTriangle Float.
CS 325 Introduction to Computer Graphics 02 / 01 / 2010 Instructor: Michael Eckmann.
12-1 Reflections Warm Up Lesson Presentation Lesson Quiz Holt Geometry.
L-1 Lecture 13: Functions and Design © 2000 UW CSE University of Washington Computer Programming I.
ACT MATH TEST You are given 60 minutes to answer 60 questions. That’s 60 seconds or less per question. You should memorize the instructions for the Math.
Art 321 Lecture 7 Dr. J. Parker. Programming In order to ‘make things happen’ on a computer, you really have to program it. Programming is not hard and.
My Star POW By, Andrea Wilkinson A2. Question: *H*H ow many triangles and polygons can I find in a six sided star? *P*P lan: *I*I plan to make a six sided.
Programming for Artists ART 315 Dr. J. R. Parker Art/Digital Media Lab Lec 10 Fall 2010.
Graphic Basics in C ATS 315. The Graphics Window Will look something like this.
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.
Translations, Rotations, Reflections, and Dilations.
Reflection Yes No. Reflection Yes No Line Symmetry.
Lecture 2: Some definitions of terms 1  Principles of Interactive Graphics  CMSCD2012  Dr David England, Room 711,  ex 2271 
Digital Media Dr. Jim Rowan ITEC So far… We have compared bitmapped graphics and vector graphics We have discussed bitmapped images, some file formats.
CS559: Computer Graphics Lecture 8: 3D Transforms Li Zhang Spring 2008 Most Slides from Stephen Chenney.
CS324e - Elements of Graphics and Visualization Fractals and 3D Landscapes.
CS 325 Introduction to Computer Graphics 02 / 03 / 2010 Instructor: Michael Eckmann.
CS COMPUTER GRAPHICS LABORATORY. LIST OF EXPERIMENTS 1.Implementation of Bresenhams Algorithm – Line, Circle, Ellipse. 2.Implementation of Line,
1 A Simple Applet. 2 Applets and applications An applet is a Java program that runs on a web page Applets can be run within any modern browser To run.
GEOMETRY UNIT 1 Transformations.
CS305j Introduction to Computing Simple Graphics 1 Topic 11 Simple Graphics "What makes the situation worse is that the highest level CS course I've ever.
Cornell Notes What are Cornell Notes?? Who came up with Cornell notes? Why take Cornell notes instead of my own style of notes? How do Cornell notes improve.
L-1 Lecture 12: Functions and Design © 2000 UW CSE University of Washington Computer Programming I.
CS 551 / 645: Introductory Computer Graphics Viewing Transforms.
Functions. functions: a collection of lines of code with a name that one can call. Functions can have inputs and outputs.
A Primer on Chaos and Fractals Bruce Kessler Western Kentucky University as a prelude to Arcadia at Lipscomb University.
CompSci 4 Java 4 Apr 14, 2009 Prof. Susan Rodger.
Introduction to 3-D Viewing Glenn G. Chappell U. of Alaska Fairbanks CS 381 Lecture Notes Monday, October 27, 2003.
CSE / ENGR 142 Programming I
TAKING CORNELL STYLE NOTES
Basic Graphics Drawing Shapes 1.
CSc 110, Spring 2018 Lecture 9: Parameters, Graphics and Random
The Wonderful World of Fractals
Cornell Notes What are Cornell Notes?? Who came up with Cornell notes?
CSE 142 Lecture Notes Graphics with DrawingPanel Chapter 3
Simple Animations Today’s Topics Reading Assignment Simple animations
Working with images and scenes
Presentation transcript:

Cse536 Functional Programming 1 6/11/2015 Lecture #5, Oct 11, 2004 Reading Assignments –Finish Chapter 3, Simple Graphics –Read Chapter 4, Shapes II, Drawing Shapes Today’s Topics –Using the graphics primitives –Drawing Primitive Shapes –Sierpinski’s Triangle –Snow-flake –Coordinate Systems –Position independent shapes –Drawing the Shape Data-type

Cse536 Functional Programming 2 6/11/2015 Turning in Homework In order to facilitate my grading homework I would ask the following me a copy of the homework. Include the code right in the body of the . Put all directions in comments. I will try and run this , so don’t clutter it with stuff that will make this hard. Put your name and the homework # in the Subject heading. i.e. Subject: Tim Sheard #2 The is expected to be received before or on the due-date and will be acknowledged if you ask for acknowledgement. The address to use is: You are also expected to turn in a paper copy in class on the due date. This way I don’t have to print it, and I have something to write comments on.

Cse536 Functional Programming 3 6/11/2015 Drawing Primitive Shapes The Graphics libraries contain primitives for drawing a few primitive shapes. We will build complicated drawing programs from these primitives ellipse :: Point -> Point -> Draw () shearEllipse :: Point -> Point -> Point -> Draw () line :: Point -> Point -> Draw () polygon :: [Point] -> Draw () polyline :: [Point] -> Draw ()

Cse536 Functional Programming 4 6/11/2015 Coordinate Systems (0,0) Increasing x-axis Increasing y-axis

Cse536 Functional Programming 5 6/11/2015 Example Program main2 = runGraphics( do { w <- openWindow "Draw some shapes" (300,300) ; drawInWindow w (ellipse (0,0) (50,50)) ; drawInWindow w (shearEllipse (0,60) (100,120) (150,200)) ; drawInWindow w (withColor Red (line (200,200) (299,275))) ; drawInWindow w (polygon [(100,100),(150,100),(160,200)]) ; drawInWindow w (withColor Green (polyline [(100,200),(150,200), (160,299),(100,200)])) ; spaceClose w } )

Cse536 Functional Programming 6 6/11/2015 The Result ; drawInWindow w (ellipse (0,0) (50,50)) ; drawInWindow w (shearEllipse (0,60) (100,120) (150,200)) ; drawInWindow w (withColor Red (line (200,200) (299,275))) ; drawInWindow w (polygon [(100,100), (150,100), (160,200)]) ; drawInWindow w (withColor Green (polyline [(100,200),(150,200), (160,299),(100,200)]))

Cse536 Functional Programming 7 6/11/2015 Building Programs We’d like to build bigger things from these small pieces Perhaps things such as fractals –Example: Sierpinski’s Triangle a repeated drawing of a triangle at repeatedly smaller sizes. Key Idea Separate pure computation from action

Cse536 Functional Programming 8 6/11/2015 size / 2 Geometry Isosceles Right Triangles (x,y-size) size hyp size size * size + size * size = hyp * hyp (x+ (size / 2), y - (size / 2)) (x, y - (size / 2)) (x, y)(x+ size, y) Remember y increases as we go down the page

Cse536 Functional Programming 9 6/11/2015 Draw 1 Triangle fillTri x y size w = drawInWindow w (withColor Blue (polygon [(x,y), (x+size,y), (x,y-size)])) minSize = 8 (x,y) size

Cse536 Functional Programming 10 6/11/2015 Sierpinski’s Triangle sierpinskiTri w x y size = if size <= minSize then fillTri x y size w else let size2 = size `div` 2 in do { sierpinskiTri w x y size2 ; sierpinskiTri w x (y-size2) size2 ; sierpinskiTri w (x + size2) y size2 } main3 = runGraphics( do { w <- openWindow "Sierpinski's Tri" (400,400) ; sierpinskiTri w ; spaceClose w } ) (x,y-size) (x+ (size / 2), y - (size / 2)) (x, y - (size / 2)) (x, y)(x+ size, y)

Cse536 Functional Programming 11 6/11/2015 Question? What’s the largest triangle sierpinskiTri ever draws? How come the big triangle is drawn?

Cse536 Functional Programming 12 6/11/2015 Abstraction Drawing a polygon in a particular window, with a particular color is a pretty common thing. Lets give it a name. drawPoly w color points = drawInWindow w (withColor color (polygon points))

Cse536 Functional Programming 13 6/11/2015 Draw a snowflake

Cse536 Functional Programming 14 6/11/2015 side Geometry of Snow flakes Snow flakes are six sided They have repeated patterns An easy six sided figure is the Star of David –Constructed from two equilateral triangles side height radius Radius = 2/3 * height height = sqrt(side*side - (side*side)/4)

Cse536 Functional Programming 15 6/11/ triangles with common center

Cse536 Functional Programming 16 6/11/2015 Compute the corners eqTri side (x,y) = let xf = fromInt x yf = fromInt y sideDiv2 = side / 2.0 height = sqrt( side*side - (sideDiv2 * sideDiv2) ) h1third = height / 3.0 h2third = h1third * 2.0 f (a,b) = (round a,round b) in (map f [(xf, yf - h2third), (xf - sideDiv2,yf + h1third), (xf + sideDiv2,yf + h1third)], map f [(xf - sideDiv2,yf - h1third), (xf + sideDiv2,yf - h1third), (xf,yf + h2third)]) (x,y) side

Cse536 Functional Programming 17 6/11/2015 Now repeat twice and draw drawStar color1 color2 w side (x,y) = do { let (a,b) = eqTri side (x,y) ; drawPoly w color1 a ; drawPoly w color2 b } main4 = runGraphics( do { w <- openWindow "Star of david” (400,400) ; drawStar Red Green w 243 (200,200) ; spaceClose w } )

Cse536 Functional Programming 18 6/11/2015 For a snowflake repeat many times snow1 w color size (x,y) = if size <= minSize then return () else do { drawStar color color w (fromInt size) (x,y) ; sequence (map smaller allpoints) } where (triangle1,triangle2) = eqTri (fromInt size) (x,y) allpoints = (triangle1 ++ triangle2) smaller x = snow1 w color (size `div` 3) x

Cse536 Functional Programming 19 6/11/2015 To Draw pick appropriate sizes main5 = runGraphics( do { w <- openWindow "SnowFlake 1” (400,400) ; snow1 w Red 243 (200,200) ; spaceClose w } ) Why 243?

Cse536 Functional Programming 20 6/11/2015 Multiple Colors snow2 w colors size (x,y) = if size <= minSize then return () else do { drawPoly w (colors !! 0) triangle2 ; drawPoly w (colors !! 1) triangle1 ; sequence (map smaller allpoints) } where (triangle1,triangle2) = eqTri (fromInt size) (x,y) allpoints = (triangle1 ++ triangle2) smaller x = snow2 w (tail colors) (size `div` 3) x main6 = runGraphics( do { w <- openWindow "Snowflake" (400,400) ; snow2 w [Red,Blue,Green,Yellow] 243 (200,200) ; spaceClose w } )

Cse536 Functional Programming 21 6/11/2015 What Happened? The list of colors was too short for the depth of the recursion main7 = runGraphics( do { w <- openWindow "Snowflake 2" (400,400) ; snow2 w [Red,Blue,Green,Yellow,White] 243 (200,200) ; spaceClose w } ) main8 = runGraphics( do { w <- openWindow "Snowflake" (400,400) ; snow2 w (cycle [Red,Blue,Green,Yellow]) 243 (200,200) ; spaceClose w } )

Cse536 Functional Programming 22 6/11/2015 Lets make it better snow3 w colors size (x,y) = if size <= minSize then return () else do { drawPoly w (colors !! 0) triangle2 ; drawPoly w (colors !! 1) triangle1 ; snow3 w colors (size `div` 3) (x,y) ; sequence (map smaller allpoints) } where (triangle1,triangle2) = eqTri (fromInt size) (x,y) allpoints = (triangle1 ++ triangle2) smaller x = snow3 w (tail colors) (size `div` 3) x main9 = runGraphics( do { w <- openWindow "Snowflake" (400,400) ; snow3 w (cycle [Red,Blue,Green,Yellow,White]) 243 (200,200) ; spaceClose w } )

Cse536 Functional Programming 23 6/11/2015 Recall the Shape Datatype data Shape = Rectangle Side Side | Ellipse Radius Radius | RtTriangle Side Side | Polygon [ Vertex ] deriving Show type Vertex = (Float,Float) -- We call this Vertex (instead of Point) so as -- not to confuse it with Graphics.Point type Side = Float type Radius = Float

Cse536 Functional Programming 24 6/11/2015 Properties of Shape Note that some shapes are position independent –Rectangle Side Side –RtTriangle Side Side –Ellipse Radius Radius But the Polygon [Vertex] Shape is defined in terms of where it appears in the plane. Shape’s Size and Radius components are measured in inches. The Window based drawing mechanism of the last lecture was based upon pixels.

Cse536 Functional Programming 25 6/11/2015 Considerations Where to draw position independent shapes? –Randomly? –In the upper left corner (the window origin) –In the middle of the window We choose to draw them in the middle of the window. We consider this the shape module origin So our model has both a different notion of “origin” and of coordinate system (pixels vs inches) We need to handle this. –Many systems draw about 100 pixels per inch.

Cse536 Functional Programming 26 6/11/2015 Visualize Window Coordinate system Shape Coordinate system (200,200) pixels or (1,-1) inches

Cse536 Functional Programming 27 6/11/2015 Coercion Functions inchToPixel :: Float -> Int inchToPixel x = round (100*x) pixelToInch :: Int -> Float pixelToInch n = (intToFloat n) / 100 intToFloat :: Int -> Float intToFloat n = fromInteger (toInteger n)

Cse536 Functional Programming 28 6/11/2015 Setting up the Shape window xWin, yWin :: Int xWin = 600 yWin = 500 trans :: Vertex -> Point trans (x,y) = ( xWin2 + inchToPixel x, yWin2 - inchToPixel y ) xWin2, yWin2 :: Int xWin2 = xWin `div` 2 yWin2 = yWin `div` 2 ( xWin2, yWin2) ( xWin, yWin )

Cse536 Functional Programming 29 6/11/2015 Translating Points trans :: Vertex -> Point trans (x,y) = ( xWin2 + inchToPixel x, yWin2 - inchToPixel y ) transList :: [Vertex] -> [Point] transList [] = [] transList (p:ps) = trans p : transList ps

Cse536 Functional Programming 30 6/11/2015 Translating Shapes Convert a Shape (Rectangle, Ellipse, …) into a graphic Draw object (using the window functions line, polygon, …) shapeToGraphic :: Shape -> Graphic shapeToGraphic (Rectangle s1 s2) = let s12 = s1/2 s22 = s2/2 in polygon (transList [(-s12,-s22),(-s12,s22), (s12,s22),(s12,-s22)]) shapeToGraphic (Ellipse r1 r2) = ellipse (trans (-r1,-r2)) (trans (r1,r2)) shapeToGraphic (RtTriangle s1 s2) = polygon (transList [(0,0),(s1,0),(0,s2)]) shapeToGraphic (Polygon pts) = polygon (transList pts) Note, first three are position independent, centered about the origin

Cse536 Functional Programming 31 6/11/2015 Define some test Shapes sh1,sh2,sh3,sh4 :: Shape sh1 = Rectangle 3 2 sh2 = Ellipse sh3 = RtTriangle 3 2 sh4 = Polygon [(-2.5,2.5), (-1.5,2.0), (-1.1,0.2), (-1.7,-1.0), (-3.0,0)]

Cse536 Functional Programming 32 6/11/2015 Draw a Shape main10 = runGraphics ( do w <- openWindow "Drawing Shapes" (xWin,yWin) drawInWindow w (withColor Red (shapeToGraphic sh1)) drawInWindow w (withColor Blue (shapeToGraphic sh2)) spaceClose w )

Cse536 Functional Programming 33 6/11/2015

Cse536 Functional Programming 34 6/11/2015 Draw multiple Shapes type ColoredShapes = [(Color,Shape)] shs :: ColoredShapes shs = [(Red,sh1),(Blue,sh2), (Yellow,sh3),(Magenta,sh4)] drawShapes :: Window -> ColoredShapes -> IO () drawShapes w [] = return () drawShapes w ((c,s):cs) = do drawInWindow w (withColor c (shapeToGraphic s)) drawShapes w cs

Cse536 Functional Programming 35 6/11/2015 Make an Action main11 = runGraphics ( do w <- openWindow "Drawing Shapes" (xWin,yWin) drawShapes w shs spaceClose w )

Cse536 Functional Programming 36 6/11/2015 Another Example main12 = runGraphics ( do w <- openWindow "Drawing Shapes" (xWin,yWin) drawShapes w (reverse coloredCircles) spaceClose w ) conCircles = map circle [0.2, ] coloredCircles = zip [Black, Blue, Green, Cyan, Red, Magenta, Yellow, White] conCircles

Cse536 Functional Programming 37 6/11/2015