From 5e795ac9f3ca9ee08a134f44852a79863873e6f7 Mon Sep 17 00:00:00 2001 From: Nathan McRae Date: Sun, 24 Aug 2025 19:03:26 -0700 Subject: [PATCH] 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. --- src/TernaryGraph.purs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/TernaryGraph.purs b/src/TernaryGraph.purs index 393ef55..69bc2a6 100644 --- a/src/TernaryGraph.purs +++ b/src/TernaryGraph.purs @@ -1,6 +1,6 @@ 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 as Array @@ -113,8 +113,11 @@ getTick :: Number -> Int -> Pixels -> Int -> Line getTick scale numTicks tickSize tickI = {start: {x: x, y: -(0.5 + (unpixel tickSize) / scale)}, end: {x: x, y: y}} where - x = 2.0 * (sin (pi / 3.0)) * (Int.toNumber tickI) / (Int.toNumber numTicks) - (sin (pi / 3.0)) - y = if tickI <= numTicks / 2 + -- For even number of ticks, the ticks don't intersect in the required pattern, so offset them + 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)) 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 = tuple3 axis1Lines axis2Lines axis3Lines 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 axis2Lines = map (rotateLine (2.0 * pi / 3.0)) axis1Lines axis3Lines = map (rotateLine (2.0 * pi / 3.0)) axis2Lines