Add math operators

I didn't realize before that you had to import them
This commit is contained in:
Nathan McRae
2025-07-14 18:52:54 -07:00
parent 60a0cc74a3
commit 9f64037bec

View File

@@ -1,6 +1,6 @@
module Main where module Main where
import Prelude (discard, class Monoid, class Semigroup, Unit, ($), (<>), (>>>)) import Prelude (discard, class Monoid, class Semigroup, Unit, ($), (<>), (>>>), (*), (+), (-), (/))
import Data.Array (range, cons) import Data.Array (range, cons)
import Data.Field (div) import Data.Field (div)
@@ -28,14 +28,14 @@ type Line =
-- TODO: How to do more normal math expressions? -- TODO: How to do more normal math expressions?
rotate :: Number -> Point -> Point rotate :: Number -> Point -> Point
rotate angle { x: x, y: y} = { rotate angle { x: x, y: y} = {
x: sub (mul (cos angle) x) (mul (sin angle) y), x: x * (cos angle) - y * (sin angle),
y: add (mul (sin angle) x) (mul (cos angle) y) y: x * (sin angle) + y * (cos angle)
} }
transform :: Number -> Number -> Number -> Point -> Point transform :: Number -> Number -> Number -> Point -> Point
transform scale xOffset yOffset { x: x, y: y} = { transform scale xOffset yOffset { x: x, y: y} = {
x: (add (mul scale x) xOffset), x: scale * x + xOffset,
y: (add (mul scale y) yOffset) y: scale * y + yOffset
} }
rotateLine :: Number -> Line -> Line rotateLine :: Number -> Line -> Line
@@ -72,12 +72,12 @@ svgLine { start: {x: x1, y: y1}, end: {x: x2, y: y2} } =
-- TODO: Make axis tick size a parameter -- TODO: Make axis tick size a parameter
getTick :: Number -> Int -> Int -> Line getTick :: Number -> Int -> Int -> Line
getTick scale numTicks tickI = getTick scale numTicks tickI =
{start: {x: x, y: negate (add 0.5 (div 5.0 scale))}, end: {x: x, y: y}} {start: {x: x, y: -(0.5 + scale / 5.0)}, end: {x: x, y: y}}
where where
x = (sub (div (mul 2.0 (mul (sin (div pi 3.0)) (toNumber tickI))) (toNumber numTicks)) (sin (div pi 3.0))) x = 2.0 * (sin (pi / 3.0)) * (toNumber tickI) / (toNumber numTicks) - (sin (pi / 3.0))
y = if tickI < (div numTicks 2) y = if tickI < numTicks / 2
then (add 1.0 (mul x (div 1.5 (sin (div pi 3.0))))) then 1.0 + x * 1.5 / (sin (pi / 3.0))
else (sub 1.0 (mul x (div 1.5 (sin (div pi 3.0))))) else 1.0 + x * 1.5 / (sin (pi / 3.0))
getTicks :: Number -> Number -> Int -> Tuple3 (Array Line) (Array Line) (Array Line) getTicks :: Number -> Number -> Int -> Tuple3 (Array Line) (Array Line) (Array Line)
getTicks scale angle numTicks = --tuple3 [] [] [] getTicks scale angle numTicks = --tuple3 [] [] []
@@ -85,8 +85,8 @@ getTicks scale angle numTicks = --tuple3 [] [] []
where where
foo = map (getTick scale numTicks) (range 0 numTicks) foo = map (getTick scale numTicks) (range 0 numTicks)
axis1Lines = map (rotateLine angle) foo axis1Lines = map (rotateLine angle) foo
axis2Lines = map (rotateLine (mul 2.0 (div pi 3.0))) axis1Lines axis2Lines = map (rotateLine (2.0 * pi / 3.0)) axis1Lines
axis3Lines = map (rotateLine (mul 2.0 (div pi 3.0))) axis2Lines axis3Lines = map (rotateLine (2.0 * pi / 3.0)) axis2Lines
-- There's probably a better way to do this -- There's probably a better way to do this
unfragment :: XMLFragment -> String unfragment :: XMLFragment -> String
@@ -107,25 +107,25 @@ ternaryGraphSvg fragments = """<?xml version="1.0" encoding="UTF-8"?>
axesPoints :: Number -> Array Point axesPoints :: Number -> Array Point
axesPoints angle = [ axesPoints angle = [
rotate angle { x: 0, y: 1 }, rotate angle { x: 0.0, y: 1.0 },
rotate angle { x: (negate (sin (mul 2.0 (div pi 3.0)))), y : (negate 0.5)}, rotate angle { x: -(sin (2.0 * pi / 3.0)), y : -0.5},
rotate angle { x: (sin (mul (div pi 3.0))), y: (negate 0.5) } rotate angle { x: (sin (2.8 * pi / 3.0)), y: -0.5 }
] ]
ternaryGraph :: Number -> Number -> Number -> Int -> String --ternaryGraph :: Number -> Number -> Number -> Int -> String
ternaryGraph scale xOffset yOffset numTicks = ternaryGraphSvg [tickLinesSvg, axesPathSvg, axisTitleSvg, tickLabelsSvg] --ternaryGraph scale xOffset yOffset numTicks = ternaryGraphSvg [tickLinesSvg, axesPathSvg, axisTitleSvg, tickLabelsSvg]
where -- where
axisTickLines = getTicks scale pi numTicks -- axisTickLines = getTicks scale pi numTicks
axis1TickLines = get1 axisTickLines -- axis1TickLines = get1 axisTickLines
axis2TickLines = get2 axisTickLines -- axis2TickLines = get2 axisTickLines
axis3TickLines = get3 axisTickLines -- axis3TickLines = get3 axisTickLines
tickLines = (cons axis1TickLines (cons axis2TickLines axis3TickLines)) -- tickLines = (cons axis1TickLines (cons axis2TickLines axis3TickLines))
transformMyLine = transformLine scale xOffset yOffset -- transformMyLine = transformLine scale xOffset yOffset
tickLinesSvg = fold $ map (svgLine >>> transformMyLine) tickLines -- tickLinesSvg = fold $ map (svgLine >>> transformMyLine) tickLines
--
axesPathSvg = -- axesPathSvg =
main :: Effect Unit main :: Effect Unit
main = do main = do
log $ ternaryGraphSvg [XMLFragment "<asdf/>", XMLFragment "<foobar/>"] log $ ternaryGraphSvg [XMLFragment "<asdf/>", XMLFragment "<foobar/>"]
log $ toString $ toNumber (div 5 3) log $ toString $ toNumber (5 / 3)