Skip to content

Instantly share code, notes, and snippets.

@davethomas11
Created September 1, 2016 14:31
Show Gist options
  • Save davethomas11/5ebaa8d3f3c27cba98b8963edaf0e8b3 to your computer and use it in GitHub Desktop.
Save davethomas11/5ebaa8d3f3c27cba98b8963edaf0e8b3 to your computer and use it in GitHub Desktop.
import System.Environment
import Text.Printf
bz = 0.552284749831
zero = read "0" :: Float
main = do
args <- getArgs
let c = parseArg 0 args
let d = c / 2
let o = parseArg 1 args
let ps = points d o
let cs = controls zero (ip d) (op d) c o
putStrLn $ (showMove $ offset o (zero, d)) ++ (showAllCurves ps cs)
where
parseArg i args = (if length args >= i + 1 then read (args !! i) :: Float else zero)
ip d = d - (d * bz)
op d = d + (d * bz)
controls a b c d o = map (\(a, b, c, d) -> (z a,z b,z c,z d)) [c1, c2, c3, c4]
where z = (+o)
c1 = (a, b, b, a)
c2 = (c, a, d, b)
c3 = (d, c, c, d)
c4 = (b, d, a, c)
points d o = map (offset o) [p2, p3, p4, p1]
where p1 = (zero, d)
p2 = rotate90 p1
p3 = offset d p2
p4 = offset d p1
offset o (x, y) = (x + o, y + o)
rotate90 (x, y) = (y, x * (-1))
showMove (x, y) = printf "M %f %f \n" x y
showCurve (x, y) (cx1, cy1, cx2, cy2) = do
printf "C %f %f %f %f %f %f \n" cx1 cy1 cx2 cy2 x y
showAllCurves as bs = concat $ zipWith (showCurve) as bs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment