Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
The concepts of drawing position-independent shapes in the functional programming course cs457/557 at pennsylvania state university (psu) during the fall'05 semester. It covers the shape datatype, properties of shapes, coordinate systems, units coercion, translating points and shapes, and some test shapes. The document also includes code snippets for drawing multiple shapes using different colors.
Typology: Study notes
1 / 15
Window Coordinate System Shape Coordinate System (200,200) pixels or (1,-1) inches (0,0) (0,0) (200,0) (0,200) (-1,0) (1,0) (0,1) (0,-1)
Note: simpler alternative to book's definition.
xWin, yWin :: Int xWin = 600 yWin = 500 xWin2, yWin2 :: Int xWin2 = xWin div
2 yWin2 = yWin div
2 trans :: Vertex -> Point trans (x,y) = ( xWin2 + inchToPixel x, yWin2 - inchToPixel y )
(xWin2,yWin2) (xWin,yWin)
shapeToGraphic :: Shape -> Graphic shapeToGraphic (Rectangle s1 s2) = let s12 = s1/ s22 = s2/ 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 and centered about the origin
main = runGraphics ( do w <- openWindow "Drawing Shapes" (xWin,yWin) drawInWindow w (withColor Red (shapeToGraphic sh1)) drawInWindow w (withColor Blue (shapeToGraphic sh2)) spaceClose w )
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
main = runGraphics ( do w <- openWindow "Drawing Shapes“ (xWin,yWin) drawShapes w shs spaceClose w )