Files
sml-projects/fcore/graph-lines.sml

90 lines
2.7 KiB
Standard ML
Raw Normal View History

2025-07-06 02:15:16 +01:00
signature GRAPH_LINES =
sig
val generate: AppType.app_type -> Real32.real vector
end
structure GraphLines :> GRAPH_LINES =
struct
2025-07-06 17:50:46 +01:00
fun toNdc (cur, half) = (cur - half) / half
fun helpGenGraphLinesX (pos, xClickPoints, yClickPoints, acc,
windowWidth, windowHeight) =
if pos = Vector.length xClickPoints then
2025-07-06 02:15:16 +01:00
Vector.concat acc
else
let
2025-07-06 17:50:46 +01:00
val halfWidth = windowHeight div 2
val halfWidth = Real32.fromInt halfWidth
val halfHeight = windowHeight div 2
val halfHeight = Real32.fromInt halfHeight
2025-07-06 02:15:16 +01:00
2025-07-06 17:50:46 +01:00
val curX = Vector.sub (xClickPoints, pos)
val curYNdc = toNdc (curX, halfWidth)
2025-07-06 02:15:16 +01:00
2025-07-06 17:50:46 +01:00
val minY = Vector.sub (xClickPoints, 0)
val minY = toNdc (minY, halfWidth)
val maxY = Vector.sub (xClickPoints, Vector.length xClickPoints - 1)
val maxY = toNdc (maxY, halfWidth)
2025-07-06 02:15:16 +01:00
val acc =
2025-07-06 17:50:46 +01:00
#[ curYNdc - 0.001, minY
, curYNdc + 0.001, minY
, curYNdc + 0.001, maxY
2025-07-06 02:15:16 +01:00
2025-07-06 17:50:46 +01:00
, curYNdc + 0.001, maxY
, curYNdc - 0.001, maxY
, curYNdc - 0.001, minY
] :: acc
2025-07-06 02:15:16 +01:00
in
2025-07-06 17:50:46 +01:00
helpGenGraphLinesX
(pos + 1, xClickPoints, yClickPoints, acc, windowWidth, windowHeight)
2025-07-06 02:15:16 +01:00
end
2025-07-06 17:50:46 +01:00
fun helpGenGraphLinesY (pos, yClickPoints, xClickPoints, acc,
windowWidth, windowHeight) =
2025-07-06 02:15:16 +01:00
if pos = Vector.length yClickPoints then
acc
else
let
2025-07-06 17:50:46 +01:00
val halfWidth = windowHeight div 2
val halfWidth = Real32.fromInt halfWidth
val halfHeight = windowHeight div 2
val halfHeight = Real32.fromInt halfHeight
2025-07-06 02:15:16 +01:00
val curY = Vector.sub (yClickPoints, pos)
2025-07-06 17:50:46 +01:00
val curYNdc = toNdc (curY, halfHeight)
val minX = Vector.sub (xClickPoints, 0)
val minX = toNdc (minX, halfWidth)
val maxX = Vector.sub (xClickPoints, Vector.length xClickPoints - 1)
val maxX = toNdc (maxX, halfWidth)
2025-07-06 02:15:16 +01:00
val acc =
2025-07-06 17:50:46 +01:00
#[ minX, curYNdc - 0.001
, minX, curYNdc + 0.001
, maxX, curYNdc + 0.001
2025-07-06 02:15:16 +01:00
2025-07-06 17:50:46 +01:00
, maxX, curYNdc + 0.001
, maxX, curYNdc - 0.001
, minX, curYNdc - 0.001
2025-07-06 02:15:16 +01:00
] :: acc
in
2025-07-06 17:50:46 +01:00
helpGenGraphLinesY
(pos + 1, yClickPoints, xClickPoints, acc, windowWidth, windowHeight)
2025-07-06 02:15:16 +01:00
end
fun helpGenerate (windowWidth, windowHeight, xClickPoints, yClickPoints) =
2025-07-06 17:50:46 +01:00
let
val acc = helpGenGraphLinesY (0, yClickPoints, xClickPoints, [], windowWidth, windowHeight)
in
helpGenGraphLinesX (0, xClickPoints, yClickPoints, acc, windowWidth, windowHeight)
end
2025-07-06 02:15:16 +01:00
fun generate (app: AppType.app_type) =
let
val {windowWidth, windowHeight, xClickPoints, yClickPoints, ...} = app
in
helpGenerate (windowWidth, windowHeight, xClickPoints, yClickPoints)
end
end