-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArt.hs
90 lines (74 loc) · 3.4 KB
/
Art.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
module Art where
import ShapeGraphics
import Codec.Picture
art = tree 15 (Point 400 400) (Vector 7 (-35)) (Colour 32 178 170 255)
++ tree 15 (Point 400 400) (Vector 6 (-30)) (Colour 32 178 170 255)
++ tree 15 (Point 400 400) (Vector 5 (-25)) (Colour 32 178 170 255)
++ tree 15 (Point 400 400) (Vector 4 (-20)) (Colour 32 178 170 255)
++ tree 15 (Point 400 400) (Vector 3 (-15)) (Colour 32 178 170 255)
++tree 15 (Point 400 400) (Vector 2 (-10)) (Colour 32 178 170 255)
++ tree 15 (Point 400 400) (Vector 1 (-5)) (Colour 32 178 170 255)
++ tree 15 (Point 400 400) (Vector 1 (5)) (Colour 32 178 170 255)
++ tree 15 (Point 400 400) (Vector 2 (10)) (Colour 32 178 170 255)
++ tree 15 (Point 400 400) (Vector 3 (15)) (Colour 32 178 170 255)
++ tree 15 (Point 400 400) (Vector 4 (20)) (Colour 32 178 170 255)
++ tree 15 (Point 400 400) (Vector 5 (25)) (Colour 32 178 170 255)
++ tree 15 (Point 400 400) (Vector 6 (30)) (Colour 32 178 170 255)
++ tree 15 (Point 400 400) (Vector 7 (35)) (Colour 32 178 170 255)
++ tree 7 (Point 175 550) (Vector 8 (40)) (Colour 3 252 252 255)
tree :: Int -> Point -> Vector -> Colour -> Picture
tree depth treeBase treeDirection startColour =
let
-- Scale of branches
branchScale = 1.0
-- Angle of left branch (radians)
leftAngle = -0.4
-- Angle of right branch (radians)
rightAngle = 0.4
-- middleAngle = 0.05
-- Change in color for each iteration
colourChange = Colour 15 3 15 1
recursiveFractal :: Int -> Point -> Vector -> Colour -> [PictureObject]
recursiveFractal 0 _ _ _ = []
recursiveFractal depth base direction colour =
[lineToPath (vectorLine base direction) colour Solid]
++ recursiveFractal (depth - 1) con leftDirection branchColour
-- ++ recursiveFractal (depth - 1) con rightDirection branchColour
++ recursiveFractal (depth - 1) con rightDirection branchColour
-- ++ recursiveFractal (depth - 1) bbb rightDirection branchColour
where
topOfRoot = movePoint base direction
con = movePoint topOfRoot direction
leftDirection =
scaleVector branchScale $ rotateVector leftAngle direction
rightDirection =
scaleVector branchScale $ rotateVector leftAngle direction
branchColour = addColour colour colourChange
in
recursiveFractal depth treeBase treeDirection startColour
-- Produce a line by drawing a vector from a point
vectorLine :: Point -> Vector -> Line
vectorLine base vector = Line base $ movePoint base vector
-- Produce a picture object from a line
lineToPath :: Line -> Colour -> LineStyle -> PictureObject
lineToPath (Line start end) = Path [start, end]
-- Scale a vector by a given factor
scaleVector :: Float -> Vector -> Vector
scaleVector factor (Vector x y) = Vector (factor * x) (factor * y)
-- Rotate a vector by a given angle (in radians)
rotateVector :: Float -> Vector -> Vector
rotateVector angle (Vector x y) = Vector x' y'
where
x' = x * (cos angle) - y * (sin angle)
y' = y * (cos angle) + x * (sin angle)
-- Offset a point using a vector for difference between points
movePoint :: Point -> Vector -> Point
movePoint (Point x y) (Vector dx dy)
= Point (x + dx) (y + dy)
addColour :: Colour -> Colour -> Colour
addColour (Colour lr lg lb lo) (Colour rr rg rb ro) =
Colour (mix lr rr) (mix lg rg) (mix lb rb) (mix lo ro)
where
mix a b = min 255 (a + b)
writeToFile pic
= writePng "art.png" (drawPicture 2 art)