Skip to content

Instantly share code, notes, and snippets.

@Lazzlo2096
Created February 26, 2019 16:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Lazzlo2096/4679f664ef3bfdd76e09c3d319f01306 to your computer and use it in GitHub Desktop.
Save Lazzlo2096/4679f664ef3bfdd76e09c3d319f01306 to your computer and use it in GitHub Desktop.
import CodeWorld
-- https://code.world/haskell
main = drawingOf rewq
rewq = polyline $ getNFractal myShape1
getNFractal ps = [ x | i <- [ (1.0, 0.0), (0.0, 1.0)], x <- getChild2 i ps]
getNFractal [] = []
--qwerty = Plain "1234"
-- polyline = map goToAndPrint [point]
--myShape1' = moveAndPlotShapeByVectors (0.0,0.0) myShape1 = [(0.0,0.0), (1.0, 0.0), (1.0, 1.0), (2.0, 1.0), (2.0, 0.0), (2.0, -1.0), (3.0, -1.0), (3.0, 0.0), (4.0, 0.0)]
vectorsOfStepsOfMyShape1 = [(0.0,0.0), (1.0, 0.0), (0.0, 1.0), (1.0, 0.0), (0.0, -1.0), (0.0, -1.0), (1.0, 0.0), (0.0, 1.0), (1.0, 0.0)]
myShape1 = scaleVectors (1.0,1.0) $ moveAndPlotShapeByVectors (0.0,0.0) vectorsOfStepsOfMyShape1
sumPoints (x:xs) = addPoints x $ sumPoints xs -- or = last moveAndPlotShapeByVectors (0.0,0.0) (x:xs)
sumPoints [] = (0,0)
--getChild p = map ( flip divPoints (sumPoints p)) p
getChild2 (1.0, 0.0) ps = scaleVectors (1/a, 1/a) ps where a = get2DVectorLength $ sumPoints ps
getChild2 (0.0, 1.0) ps = scaleVectors (1/a, 1/a) $ map flipTuple ps where a = get2DVectorLength $ sumPoints ps
getChild2 (-1.0, 0.0) ps = scaleVectors (-1/a, 1/a) ps where a = get2DVectorLength $ sumPoints ps
getChild2 (0.0, -1.0) ps = scaleVectors (1/a, -1/a) $ map flipTuple ps where a = get2DVectorLength $ sumPoints ps
getChild2 (0.0, 0.0) ps = scaleVectors (1/a, 1/a) ps where a = get2DVectorLength $ sumPoints ps
flipTuple (x,y) = (y,x)
addPoints :: Point -> Point -> Point
addPoints (x,y) (x2, y2) = (x+x2, y+y2)
mulPoints (x,y) (x2, y2) = (x*x2, y*y2)
----------
--divPoints p (0, 0) = p
--divPoints (x,y) (0, y2) = (x/1, y/y2)
--divPoints (x,y) (x2, 0) = (x/x2, y/1)
--divPoints (x,y) (x2, y2) = (x/x2, y/y2)
----------
get2DVectorLength (x, y) = sqrt (x^2 + y^2)
scaleVectors :: Point -> [Point] -> [Point]
scaleVectors p ls = map (mulPoints p) ls
moveVectors p ls = map (addPoints p) ls
moveAndPlotShapeByVectors :: Point -> [Point] -> [Point]
moveAndPlotShapeByVectors p (x:xs) = rrr : moveAndPlotShapeByVectors rrr xs
where rrr = addPoints p x
moveAndPlotShapeByVectors _ [] = []
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment