Koch curve: Difference between revisions

m
(Added XPL0 example.)
Line 653:
import Text.Printf (printf)
 
------------------------ KOCH CURVE ----------------------
kochSnowflake :: Int -> (Float, Float) -> (Float, Float) -> [(Float, Float)]
kochSnowflake ::
kochSnowflake n a b = concat $ zipWith (kochCurve n) points (xs ++ [x])
Int ->
(Float, Float) ->
(Float, Float) ->
[(Float, Float)]
kochSnowflake n a b =
concat $
kochSnowflake n a b = concat $ zipWith (kochCurve n) points (xs ++<> [x])
where
points@(x : xs) = [a, equilateralApex a b, b]
 
kochCurve ::
kochCurve :: Int -> (Float, Float) -> (Float, Float) -> [(Float, Float)]
Int ->
(Float, Float) ->
(Float, Float) ->
[(Float, Float)]
kochCurve n ab xy = ab : go n (ab, xy)
where
Line 664 ⟶ 675:
go n (ab, xy) =
let (mp, mq) = midThirdOfLine ab xy
points@(_ : xs) = [ab, mp, equilateralApex mp mq, mq, xy]
in go (pred n) =<< zip points[ xsab,
mp,
equilateralApex mp mq,
mq,
xy
]
in go (pred n) =<< zip points xs
 
equilateralApex :: (Float, Float) -> (Float, Float) -> (Float, Float)
(Float, Float) ->
(Float, Float) ->
(Float, Float)
equilateralApex = rotatedPoint (pi / 3)
 
rotatedPoint ::
rotatedPoint :: Float -> (Float, Float) -> (Float, Float) -> (Float, Float)
Float ->
(Float, Float) ->
(Float, Float) ->
(Float, Float)
rotatedPoint theta (ox, oy) (a, b) = (ox + dx, oy - dy)
where
Line 677 ⟶ 701:
rotatedVector :: Float -> (Float, Float) -> (Float, Float)
rotatedVector angle (x, y) =
( x * cos angle - y * sin angle, x * sin angle + y * cos angle)
x * sin angle + y * cos angle
)
 
midThirdOfLine :: (Float, Float)
-> (Float, Float) ->
-> ((Float, Float), (Float, Float))->
kochSnowflake :: Int -> ((Float, Float) ->, (Float, Float) -> [(Float, Float)]
midThirdOfLine (a, b) (x, y) = (p, f p)
where
Line 690 ⟶ 717:
-------------------------- TEST ---------------------------
main :: IO ()
main =
main = putStrLn $ svgFromPoints 1024 $ kochSnowflake 4 (200, 600) (800, 600)
putStrLn $
svgFromPoints 1024 $
main = putStrLn $ svgFromPoints 1024 $ kochSnowflake 4 (200, 600) (800, 600)
 
-------------------------- SVG ----------------------------
Line 696 ⟶ 726:
svgFromPoints w xys =
unlines
[ "<svg xmlns=\"http://www.w3.org/2000/svg\"",
unwords
, unwords ["width=\"512\" height=\"512\" viewBox=\"5 5", sw, sw, "\"> "]
, "<path d [ "width=\"M512\" ++ points ++ height=\"512\" viewBox=\"5 5",
sw,
, "stroke-width=\"2\" stroke=\"red\" fill=\"transparent\"/>"
, "</svg>" sw,
"\"> "
],
"<path d=\"M" <> points <> "\" ",
unwords [
"stroke-width=\"2\"",
"stroke=\"red\"",
, "stroke-width=\"2\" stroke=\ "red\" fill=\"transparent\"/>"
],
"</svg>"
]
where
Line 706 ⟶ 745:
showN = printf "%.2g"
points =
( unwords
(unwords . fmap (((++) . showN . fst) <*> ((' ' :) . showN . snd))) xys</lang>
. fmap
( ((<>) . showN . fst)
(unwords . fmap (((++) . showN . fst) <*> ((' ' :) . showN . snd))) xys</lang>
)
)
xys</lang>
 
=={{header|IS-BASIC}}==
9,659

edits