Chapter 4 Shapes II Drawing Shapes Recall the
- Slides: 14
Chapter 4 Shapes II: Drawing Shapes
Recall the Shape Datatype data Shape = Rectangle Side | Ellipse Radius | Rt. Triangle Side | Polygon [ Vertex ] deriving Show type Vertex = (Float, Float) type Side = Float type Radius = Float
Properties of Shapes w Note that some shapes are position independent: n Rectangle Side n Rt. Triangle Side n Ellipse Radius w On the other hand, a Polygon [Vertex] is defined in terms of where it appears in the plane. w A shape’s Size and Radius are measured in inches. w On the other hand, the Windows-based drawing mechanism of the last chapter was based on pixels.
Considerations w Where do we draw position-independent shapes? n Randomly? n In the upper left corner (the window origin)? n In the middle of the window? w We will choose the last option above, by defining the middle of the window as the origin of a standard Cartesian coordinate system. w So our new coordinate system has both a different notion of “origin” (middle vs. top-left) and of “units” (inches vs. pixels). w We will need to define coercions between these two coordinate systems.
Coordinate Systems (0, 1) Window Coordinate System (0, 0) (200, 0) Shape Coordinate System (-1, 0) (0, 200) (0, 0) (1, 0) (0, -1) (200, 200) pixels or (1, -1) inches
Units Coercion inch. To. Pixel : : Float -> Int inch. To. Pixel x = round (100*x) pixel. To. Inch : : Int -> Float pixel. To. Inch n = int. To. Float n / 100 int. To. Float : : Int -> Float n = from. Integer (to. Integer n)
Translation Coercion x. Win, y. Win : : Int x. Win = 600 y. Win = 500 (x. Win 2, y. Win 2) x. Win 2, y. Win 2 : : Int x. Win 2 = x. Win `div` 2 y. Win 2 = y. Win `div` 2 trans : : Vertex -> Point trans (x, y) = ( x. Win 2 + inch. To. Pixel x, y. Win 2 - inch. To. Pixel y ) (x. Win, y. Win)
Translating Points trans : : Vertex -> Point trans (x, y) = ( x. Win 2 + inch. To. Pixel x, y. Win 2 - inch. To. Pixel y ) trans. List : : [Vertex] -> [Point] trans. List [] = [] trans. List (p: ps) = trans p : trans. List ps
Translating Shapes Note: first two are shape. To. Graphic : : Shape -> Graphic position independent shape. To. Graphic (Rectangle s 1 s 2) and centered about = let s 12 = s 1/2 the origin s 22 = s 2/2 in polygon (trans. List [(-s 12, -s 22), (-s 12, s 22), (s 12, -s 22)]) shape. To. Graphic (Ellipse r 1 r 2) = ellipse (trans (-r 1, -r 2)) (trans (r 1, r 2)) shape. To. Graphic (Rt. Triangle s 1 s 2) = polygon (trans. List [(0, 0), (s 1, 0), (0, s 2)]) shape. To. Graphic (Polygon pts) = polygon (trans. List pts)
Some Test Shapes sh 1, sh 2, sh 3, sh 4 : : Shape sh 1 sh 2 sh 3 sh 4 = = Rectangle 3 2 Ellipse 1 1. 5 Rt. Triangle 3 2 Polygon [(-2. 5, 2. 5), (-1. 5, 2. 0), (-1. 1, 0. 2), (-1. 7, -1. 0), (-3. 0, 0)]
Drawing Shapes main 10 = run. Graphics $ do w <- open. Window "Drawing Shapes" (x. Win, y. Win) draw. In. Window w (with. Color Red (shape. To. Graphic sh 1)) draw. In. Window w (with. Color Blue (shape. To. Graphic sh 2)) space. Close w
The Result
Drawing Multiple Shapes type Colored. Shapes = [(Color, Shape)] shs : : Colored. Shapes shs = [(Red, sh 1), (Blue, sh 2), (Yellow, sh 3), (Magenta, sh 4)] draw. Shapes : : Window -> Colored. Shapes -> IO () draw. Shapes w [] = return () draw. Shapes w ((c, s): cs) = do draw. In. Window w (with. Color c (shape. To. Graphic s)) draw. Shapes w cs
Multiple Shapes, cont’d main 11 = run. Graphics $ do w <- open. Window "Drawing Shapes“ (x. Win, y. Win) draw. Shapes w shs space. Close w )
- Shapes that seem to follow no rules
- Chapter 57 pediatric dentistry recall
- Section engineering drawing
- Lines and shapes drawing
- What are line conventions
- Hình ảnh bộ gõ cơ thể búng tay
- Bổ thể
- Tỉ lệ cơ thể trẻ em
- Voi kéo gỗ như thế nào
- Tư thế worm breton
- Chúa yêu trần thế
- Các môn thể thao bắt đầu bằng từ đua
- Thế nào là hệ số cao nhất
- Các châu lục và đại dương trên thế giới