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.

Slides:



Advertisements
Similar presentations
Yes you do need to write this.
Advertisements

Complete on the back of your homework.
10.1 Parabolas.
10.4 Ellipses p An ellipse is a set of points such that the distance between that point and two fixed points called Foci remains constant d1 d2.
Chapter 11 Proof by Induction. Induction and Recursion Two sides of the same coin.  Induction usually starts with small things, and then generalizes.
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 #8, Oct. 20, 2004 Todays Topics –Sets and characteristic functions –Regions –Is a point in a Region –Currying.
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.
Chapter 2 A Module of Shapes: Part I. Defining New Datatypes  The ability to define new data types in a programming language is important.  Kinds of.
Advanced Programming Handout 4. Introductions  Me: Benjamin C. Pierce (known as Benjamin, or, if you prefer, Dr. Pierce, but not Ben or Professor) 
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.
Chapter 5 Polymorphic and Higher-Order Functions.
Advanced Programming Handout 5 Recursive Data Types (SOE Chapter 7)
Chapter 10 Measurement Section 10.4 The Pythagorean Theorem.
Perimeters and Areas of Rectangles
Chapter 10 Measurement Section 10.2 Perimeter and Area.
Definitions and formulas for the shapes you love Perimeter and Area.
Perimeter How do we find the perimeter of a quadrilateral? Standards: M5P3, M5P4, and M5A1c.
Area & Perimeter.
Composite Shapes Math 10-3 Ch.3 Measurement.  Consider a rectangle with the dimensions 2 cm by 3 cm.  -What is the perimeter? = 10 cm.
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.
Warm Up Graph the line segment for each set of ordered pairs. Then find the length of the line segment. 1. (–7, 0), (0, 0) 2. (0, 3), (0, 6) 3. (–4, –2),
GEOMETRY 6.G.1 VOCABULARY Mrs. Thompson Level 1. Review Words Side: A line bounding a geometric figure: one of the faces forming the outside of an object.
9-2 Area of Regular Polygons The center of a regular polygon is equidistant from its vertices. Radius- the distance from the center to a vertex. Apothem-
11.2a Area of Regular Polygons CCSSLESSON GOALS  Understand that to find the area of a regular polygon you find the area of one triangle and multiply.
Perimeter of Rectangles
1.8: Perimeter, Circumference, and Area
USE ALL FOUR OPERATIONS TO SOLVE PROBLEMS INVOLVING PERIMETER AND UNKNOWN MEASUREMENTS MODULE 7 LESSON 17.
Hyberbola Conic Sections. Hyperbola The plane can intersect two nappes of the cone resulting in a hyperbola.
Basic Measurement.
5 Minute Check Find the area. Complete on the back of
Perimeter is the distance around an object. If you started at one point and went all the way around until you got to where you started, the perimeter.
NCSC Sample Instructional Unit - Elementary Measurement Lesson 4
Area & Perimeter. Area Area: The space that an object covers. We measure this by multiplying the length of one side by the width of one side. For example:
What questions could we ask about this tile?. What if… We put 2 together?
Hyberbola Conic Sections.
Area & Perimeter An Introduction. AREA The amount of space inside a 2-dimensional object. Measured in square units cm 2, m 2, mm 2 Example: 1 cm 2 cm.
0 PROGRAMMING IN HASKELL Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and a few other sources) Odds and Ends,
Can you figure out what shape will be drawn just by looking at the coordinates of the vertices? (-4, 8) (-9, 3) (1, 3) (-4, -2)
Copyright © Ed2Net Learning Inc.1. 2 Warm Up x y y = 3x - 11) x y y = x - 62)
Functional Programming Lecture 3 - Lists Muffy Calder.
Haskell Chapter 5, Part II. Topics  Review/More Higher Order Functions  Lambda functions  Folds.
7-1C Areas and Perimeters of Similar Polygons What is the relationship between the perimeter ratios of similar figures and their similarity ratio? What.
Perimeter and Area Formulas.  Perimeter is the distance around an object. It is easily the simplest formula. Simply add up all the sides of the shape,
Perimeter & Area. Today’s Objectives:  Learn what it means to find perimeter and area.  Practice finding or estimating the perimeter and area.
9.1 PERIMETER AND AREA OF PARALLELOGRAMS Objective: Students find the perimeter and area of parallelograms.
Q. What do we mean by finding the perimeter of a shape? Q. What strategies might you use for calculating the perimeter of a rectangle?
A polygon is a geometric figure with three or more sides.
Solving Equations Conceputally
Recursion.
Theory of Computation Lecture 4: Programs and Computable Functions II
Perimeter Formula.
May 1, 2017 Area of Regular Polygons
Perimeters and Areas of Similar Figures
Perimeter.
Perimeter and Area of Rectangles on the Coordinate Plane
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Higher Order Functions
two-dimensional shape.
Perimeter.
9.4 Graph & Write Equations of Ellipses
Perimeter.
HIGHER ORDER FUNCTIONS
Higher-Order Functions in Haskell
Theory of Computation Lecture 4: Programs and Computable Functions II
Perimeter.
Presentation transcript:

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 constructor).  The first three are easy … perimeter :: Shape -> Float perimeter (Rectangle s1 s2) = 2*(s1+s2) perimeter (RtTriangle s1 s2) = s1 + s2 + sqrt (s1^2+s2^2) perimeter (Polygon pts) = foldl (+) 0 (sides pts)  This assumes that we can compute the lengths of the sides of a polygon. This shouldn’t be too difficult since we can compute the distance between two points with distBetween. s1 s2 s1 s2

Recursive Def’n of Sides sides :: [Vertex] -> [Side] sides [] = [] sides (v:vs) = aux v vs where aux v1 (v2:vs’) = distBetween v1 v2 : aux v2 vs’ aux vn [] = distBetween vn v : [] -- aux vn [] = [distBetween vn v]  But can we do better? Can we remove the direct recursion, as a seasoned functional programmer might?

Visualize What’s Happening  The list of vertices is: vs = [A,B,C,D,E]  We need to compute the distances between the pairs of points (A,B), (B,C), (C,D), (D,E), and (E,A).  Can we compute these pairs as a list? [(A,B),(B,C),(C,D),(D,E),(E,A)]  Yes, by “zipping” the two lists: [A,B,C,D,E] and [B,C,D,E,A] as follows: zip vs (tail vs ++ [head vs]) A B C DE

New Version of sides This leads to: sides :: [Vertex] -> [Side] sides vs = zipWith distBetween vs (tail vs ++ [head vs]) Where zipWith is a predefined function that is just like zip except that it applies its first argument (a function) to each pair of values. For example: zipWith (+) [1,2,3] [4,5,6]  [5,7,9]

There is one remaining case: the ellipse. The perimeter of an ellipse is given by the summation of an infinite series. For an ellipse with radii r 1 and r 2 : p = 2  r 1 (1 -  s i ) where s 1 = 1/4 e 2 s i = s i-1 (2i-1)(2i-3) e 2 for i >= 1 4i 2 e = sqrt (r 1 2 – r 2 2 ) / r 1 Given s i, it is easy to compute s i+1. Perimeter of an Ellipse

Computing the Series nextEl:: Float -> Float -> Float -> Float nextEl e s i = s*(2*i-1)*(2*i-3)*(e^2) / (4*i^2) Now we want to compute [s 1,s 2,s 3, …]. To fix e, let’s define: aux s i = nextEl e s i So, we would like to compute: [s 1, s 2 = aux s 1 2, s 3 = aux s 2 3 = aux (aux s 1 2) 3, s 4 = aux s 3 4 = aux (aux (aux s 1 2) 3) 4,... ] s i+1 = s i (2i-1)(2i-3) e 2 4i 2 Can we capture this pattern?

Scanl (scan from the left)  Yes, using the predefined function scanl: scanl :: (a -> b -> b) -> b -> [a] -> [b] scanl f seed [] = seed : [] scanl f seed (x:xs) = seed : scanl f newseed xs where newseed = f x seed  For example: scanl (+) 0 [1,2,3]  [ 0, 1 = (+) 0 1, 3 = (+) 1 2, 6 = (+) 3 3 ]  [ 0, 1, 3, 6 ]  Using scanl, the result we want is: scanl aux s1 [2..]

r2 = 1.5 r1 = 2.1 [s1 = , s2 = , s3 = , s4 = , s5 = ,...] Note how quickly the values in the series get smaller... Sample Series Values

Putting it all Together perimeter (Ellipse r1 r2) | r1 > r2 = ellipsePerim r1 r2 | otherwise = ellipsePerim r2 r1 where ellipsePerim r1 r2 = let e = sqrt (r1^2 - r2^2) / r1 s = scanl aux (0.25*e^2) (map intToFloat [2..]) aux s i = nextEl e s i test x = x > epsilon sSum = foldl (+) 0 (takeWhile test s) in 2*r1*pi*(1 - sSum)