Fix alignment of even number of ticks

Without this, it created a hexagram type shape in the graph and the tick
marks didn't all intersect together at one point.
This commit is contained in:
Nathan McRae
2025-08-24 19:03:26 -07:00
parent da1e6f32a5
commit 5e795ac9f3

View File

@@ -1,6 +1,6 @@
module TernaryGraph where module TernaryGraph where
import Prelude (discard, class Monoid, class Semigroup, Unit, ($), (<=), (<<<), (<>), (*), (+), (-), (/), (&&)) import Prelude (discard, class Monoid, class Semigroup, Unit, (==), ($), (<), (<=), (<<<), (<>), (*), (+), (-), (/), (&&))
--import Data.Array ((!!), concat, cons, mapWithIndex, range) --import Data.Array ((!!), concat, cons, mapWithIndex, range)
import Data.Array as Array import Data.Array as Array
@@ -113,8 +113,11 @@ getTick :: Number -> Int -> Pixels -> Int -> Line
getTick scale numTicks tickSize tickI = getTick scale numTicks tickSize tickI =
{start: {x: x, y: -(0.5 + (unpixel tickSize) / scale)}, end: {x: x, y: y}} {start: {x: x, y: -(0.5 + (unpixel tickSize) / scale)}, end: {x: x, y: y}}
where where
x = 2.0 * (sin (pi / 3.0)) * (Int.toNumber tickI) / (Int.toNumber numTicks) - (sin (pi / 3.0)) -- For even number of ticks, the ticks don't intersect in the required pattern, so offset them
y = if tickI <= numTicks / 2 x = if (Int.rem numTicks 2) == 0
then 2.0 * (sin (pi / 3.0)) * (Int.toNumber tickI) / (Int.toNumber numTicks) - (sin (pi / 3.0))
else 2.0 * (sin (pi / 3.0)) * ((Int.toNumber tickI) + 1.0) / ((Int.toNumber numTicks) + 0.5) - (sin (pi / 3.0)) - ((9.5 / 6.0) / ((Int.toNumber numTicks) + 0.5))
y = if x < 0.0
then 1.0 + x * 1.5 / (sin (pi / 3.0)) then 1.0 + x * 1.5 / (sin (pi / 3.0))
else 1.0 - x * 1.5 / (sin (pi / 3.0)) else 1.0 - x * 1.5 / (sin (pi / 3.0))
@@ -122,7 +125,7 @@ getTicks :: Number -> Number -> Pixels -> Int -> Tuple3 (Array Line) (Array Line
getTicks scale angle tickSize numTicks = getTicks scale angle tickSize numTicks =
tuple3 axis1Lines axis2Lines axis3Lines tuple3 axis1Lines axis2Lines axis3Lines
where where
foo = map (getTick scale numTicks tickSize) (Array.range 0 numTicks) foo = map (getTick scale (numTicks - 1) tickSize) (Array.range 0 (numTicks - 1))
axis1Lines = map (rotateLine angle) foo axis1Lines = map (rotateLine angle) foo
axis2Lines = map (rotateLine (2.0 * pi / 3.0)) axis1Lines axis2Lines = map (rotateLine (2.0 * pi / 3.0)) axis1Lines
axis3Lines = map (rotateLine (2.0 * pi / 3.0)) axis2Lines axis3Lines = map (rotateLine (2.0 * pi / 3.0)) axis2Lines