From 9f64037bec267008e2faa209abec380f9dff21fb Mon Sep 17 00:00:00 2001 From: Nathan McRae Date: Mon, 14 Jul 2025 18:52:54 -0700 Subject: [PATCH] Add math operators I didn't realize before that you had to import them --- src/Main.purs | 56 +++++++++++++++++++++++++-------------------------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/src/Main.purs b/src/Main.purs index 11fc281..c16457f 100644 --- a/src/Main.purs +++ b/src/Main.purs @@ -1,6 +1,6 @@ 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.Field (div) @@ -28,14 +28,14 @@ type Line = -- TODO: How to do more normal math expressions? rotate :: Number -> Point -> Point rotate angle { x: x, y: y} = { - x: sub (mul (cos angle) x) (mul (sin angle) y), - y: add (mul (sin angle) x) (mul (cos angle) y) + x: x * (cos angle) - y * (sin angle), + y: x * (sin angle) + y * (cos angle) } transform :: Number -> Number -> Number -> Point -> Point transform scale xOffset yOffset { x: x, y: y} = { - x: (add (mul scale x) xOffset), - y: (add (mul scale y) yOffset) + x: scale * x + xOffset, + y: scale * y + yOffset } 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 getTick :: Number -> Int -> Int -> Line 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 - x = (sub (div (mul 2.0 (mul (sin (div pi 3.0)) (toNumber tickI))) (toNumber numTicks)) (sin (div pi 3.0))) - y = if tickI < (div numTicks 2) - then (add 1.0 (mul x (div 1.5 (sin (div pi 3.0))))) - else (sub 1.0 (mul x (div 1.5 (sin (div pi 3.0))))) + x = 2.0 * (sin (pi / 3.0)) * (toNumber tickI) / (toNumber numTicks) - (sin (pi / 3.0)) + y = if tickI < numTicks / 2 + then 1.0 + x * 1.5 / (sin (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 scale angle numTicks = --tuple3 [] [] [] @@ -85,8 +85,8 @@ getTicks scale angle numTicks = --tuple3 [] [] [] where foo = map (getTick scale numTicks) (range 0 numTicks) axis1Lines = map (rotateLine angle) foo - axis2Lines = map (rotateLine (mul 2.0 (div pi 3.0))) axis1Lines - axis3Lines = map (rotateLine (mul 2.0 (div pi 3.0))) axis2Lines + axis2Lines = map (rotateLine (2.0 * pi / 3.0)) axis1Lines + axis3Lines = map (rotateLine (2.0 * pi / 3.0)) axis2Lines -- There's probably a better way to do this unfragment :: XMLFragment -> String @@ -107,25 +107,25 @@ ternaryGraphSvg fragments = """ axesPoints :: Number -> Array Point axesPoints angle = [ - rotate angle { x: 0, y: 1 }, - rotate angle { x: (negate (sin (mul 2.0 (div pi 3.0)))), y : (negate 0.5)}, - rotate angle { x: (sin (mul (div pi 3.0))), y: (negate 0.5) } + rotate angle { x: 0.0, y: 1.0 }, + rotate angle { x: -(sin (2.0 * pi / 3.0)), y : -0.5}, + rotate angle { x: (sin (2.8 * pi / 3.0)), y: -0.5 } ] -ternaryGraph :: Number -> Number -> Number -> Int -> String -ternaryGraph scale xOffset yOffset numTicks = ternaryGraphSvg [tickLinesSvg, axesPathSvg, axisTitleSvg, tickLabelsSvg] - where - axisTickLines = getTicks scale pi numTicks - axis1TickLines = get1 axisTickLines - axis2TickLines = get2 axisTickLines - axis3TickLines = get3 axisTickLines - tickLines = (cons axis1TickLines (cons axis2TickLines axis3TickLines)) - transformMyLine = transformLine scale xOffset yOffset - tickLinesSvg = fold $ map (svgLine >>> transformMyLine) tickLines - - axesPathSvg = +--ternaryGraph :: Number -> Number -> Number -> Int -> String +--ternaryGraph scale xOffset yOffset numTicks = ternaryGraphSvg [tickLinesSvg, axesPathSvg, axisTitleSvg, tickLabelsSvg] +-- where +-- axisTickLines = getTicks scale pi numTicks +-- axis1TickLines = get1 axisTickLines +-- axis2TickLines = get2 axisTickLines +-- axis3TickLines = get3 axisTickLines +-- tickLines = (cons axis1TickLines (cons axis2TickLines axis3TickLines)) +-- transformMyLine = transformLine scale xOffset yOffset +-- tickLinesSvg = fold $ map (svgLine >>> transformMyLine) tickLines +-- +-- axesPathSvg = main :: Effect Unit main = do log $ ternaryGraphSvg [XMLFragment "", XMLFragment ""] - log $ toString $ toNumber (div 5 3) + log $ toString $ toNumber (5 / 3)