improve graph lines

This commit is contained in:
2025-07-06 17:50:46 +01:00
parent 6a6a36a277
commit dc2a52bcc8
5 changed files with 109 additions and 121 deletions

BIN
dotscape

Binary file not shown.

View File

@@ -1,5 +1,36 @@
structure ClickPoints = structure ClickPoints =
struct struct
fun helpMakeOne (start, finish, numPoints, point) =
let
val difference = finish - start
val increment = Real32.fromInt difference / Real32.fromInt numPoints
val start = Real32.fromInt start
in
(Real32.fromInt point * increment) + start
end
fun makeOne (windowWidth, windowHeight, numPoints, point) =
if windowWidth > windowHeight then
let
val difference = windowWidth - windowHeight
val half = difference div 2
val widthStart = half
val widthFinish = windowWidth - half
in
helpMakeOne (widthStart, widthFinish, numPoints, point)
end
else if windowHeight > windowWidth then
let
val difference = windowHeight - windowWidth
val half = difference div 2
val heightStart = half
val heightFinish = windowHeight - half
in
helpMakeOne (heightStart, heightFinish, numPoints, point)
end
else
helpMakeOne (0, windowWidth, numPoints, point)
fun generate (start, finish, numPoints) = fun generate (start, finish, numPoints) =
let let
val difference = finish - start val difference = finish - start

View File

@@ -5,133 +5,80 @@ end
structure GraphLines :> GRAPH_LINES = structure GraphLines :> GRAPH_LINES =
struct struct
(* fun toNdc (cur, half) = (cur - half) / half
* This function only produces the desired result
* when the window is a square and has the aspect ratio 1:1. fun helpGenGraphLinesX (pos, xClickPoints, yClickPoints, acc,
* This is because the function assumes it can use windowWidth, windowHeight) =
* the same position coordinates both horizontally and vertically. if pos = Vector.length xClickPoints then
*)
fun helpGenGraphLinesSquare (pos: Real32.real, limit, acc) =
if pos >= limit then
Vector.concat acc Vector.concat acc
else else
let let
val vec = val halfWidth = windowHeight div 2
#[ (* x = _.1 *) val halfWidth = Real32.fromInt halfWidth
pos - 0.001, ~1.0 val halfHeight = windowHeight div 2
, pos + 0.001, ~1.0 val halfHeight = Real32.fromInt halfHeight
, pos + 0.001, 1.0
, pos + 0.001, 1.0
, pos - 0.001, 1.0
, pos - 0.001, ~1.0
(* y = _.1 *)
, ~1.0, pos - 0.001
, ~1.0, pos + 0.001
, 1.0, pos + 0.001
, 1.0, pos + 0.001
, 1.0, pos - 0.001
, ~1.0, pos - 0.001
]
val acc = vec :: acc
val nextPos = pos + 0.1
in
helpGenGraphLinesSquare (nextPos, limit, acc)
end
fun helpGenGraphLinesHorizontal
(pos, xClickPoints, acc, halfWidth, yMin, yMax) =
if pos = Vector.length xClickPoints then
acc
else
let
val curX = Vector.sub (xClickPoints, pos) val curX = Vector.sub (xClickPoints, pos)
val ndc = (curX - halfWidth) / halfWidth val curYNdc = toNdc (curX, halfWidth)
val acc =
#[
ndc - 0.001, yMin
, ndc + 0.001, yMin
, ndc + 0.001, yMax
, ndc + 0.001, yMax val minY = Vector.sub (xClickPoints, 0)
, ndc - 0.001, yMax val minY = toNdc (minY, halfWidth)
, ndc - 0.001, yMin val maxY = Vector.sub (xClickPoints, Vector.length xClickPoints - 1)
] :: acc val maxY = toNdc (maxY, halfWidth)
val acc =
#[ curYNdc - 0.001, minY
, curYNdc + 0.001, minY
, curYNdc + 0.001, maxY
, curYNdc + 0.001, maxY
, curYNdc - 0.001, maxY
, curYNdc - 0.001, minY
] :: acc
in in
helpGenGraphLinesHorizontal helpGenGraphLinesX
(pos + 1, xClickPoints, acc, halfWidth, yMin, yMax) (pos + 1, xClickPoints, yClickPoints, acc, windowWidth, windowHeight)
end end
fun helpGenGraphLinesVertical (pos, yClickPoints, acc, halfHeight, xMin, xMax) = fun helpGenGraphLinesY (pos, yClickPoints, xClickPoints, acc,
windowWidth, windowHeight) =
if pos = Vector.length yClickPoints then if pos = Vector.length yClickPoints then
acc acc
else else
let let
val curY = Vector.sub (yClickPoints, pos) val halfWidth = windowHeight div 2
val ndc = (curY - halfHeight) / halfHeight val halfWidth = Real32.fromInt halfWidth
val acc = val halfHeight = windowHeight div 2
#[ val halfHeight = Real32.fromInt halfHeight
xMin, ndc - 0.001
, xMin, ndc + 0.001
, xMax, ndc + 0.001
, xMax, ndc + 0.001 val curY = Vector.sub (yClickPoints, pos)
, xMax, ndc - 0.001 val curYNdc = toNdc (curY, halfHeight)
, xMin, ndc - 0.001
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)
val acc =
#[ minX, curYNdc - 0.001
, minX, curYNdc + 0.001
, maxX, curYNdc + 0.001
, maxX, curYNdc + 0.001
, maxX, curYNdc - 0.001
, minX, curYNdc - 0.001
] :: acc ] :: acc
in in
helpGenGraphLinesVertical helpGenGraphLinesY
(pos + 1, yClickPoints, acc, halfHeight, xMin, xMax) (pos + 1, yClickPoints, xClickPoints, acc, windowWidth, windowHeight)
end end
fun helpGenerate (windowWidth, windowHeight, xClickPoints, yClickPoints) = fun helpGenerate (windowWidth, windowHeight, xClickPoints, yClickPoints) =
if windowWidth = windowHeight then let
helpGenGraphLinesSquare (~1.0, 1.0, []) val acc = helpGenGraphLinesY (0, yClickPoints, xClickPoints, [], windowWidth, windowHeight)
else if windowWidth > windowHeight then in
let helpGenGraphLinesX (0, xClickPoints, yClickPoints, acc, windowWidth, windowHeight)
val difference = windowWidth - windowHeight end
val offset = difference div 2
val halfWidth = Real32.fromInt (windowWidth div 2)
val halfHeight = Real32.fromInt (windowHeight div 2)
val start = offset - (windowWidth div 2)
val start = Real32.fromInt start / halfWidth
val finish = (windowWidth - offset) - (windowWidth div 2)
val finish = Real32.fromInt finish / halfWidth
val lines = helpGenGraphLinesHorizontal
(0, xClickPoints, [], halfWidth, ~1.0, 1.0)
val lines = helpGenGraphLinesVertical
(0, yClickPoints, lines, halfHeight, start, finish)
in
Vector.concat lines
end
else
(* windowWidth < windowHeight *)
let
val difference = windowHeight - windowWidth
val offset = difference div 2
val halfWidth = Real32.fromInt (windowWidth div 2)
val halfHeight = Real32.fromInt (windowHeight div 2)
val start = offset - (windowHeight div 2)
val start = Real32.fromInt start / halfHeight
val finish = (windowHeight - offset) - (windowHeight div 2)
val finish = Real32.fromInt finish / halfHeight
val lines = helpGenGraphLinesHorizontal
(0, xClickPoints, [], halfWidth, start, finish)
val lines = helpGenGraphLinesVertical
(0, yClickPoints, lines, halfHeight, ~1.0, 1.0)
in
Vector.concat lines
end
fun generate (app: AppType.app_type) = fun generate (app: AppType.app_type) =
let let

View File

@@ -160,7 +160,8 @@ struct
val maxSide = Int.max (canvasWidth, canvasHeight) val maxSide = Int.max (canvasWidth, canvasHeight)
val squares = val squares =
CollisionTree.toTriangles (windowWidth, windowHeight, squares, maxSide) CollisionTree.toTriangles (windowWidth, windowHeight, squares, maxSide,
canvasWidth, canvasHeight, xClickPoints, yClickPoints)
val drawMsg = DRAW_SQUARES_AND_DOTS {squares = squares, dots = dotVec} val drawMsg = DRAW_SQUARES_AND_DOTS {squares = squares, dots = dotVec}
in in
(model, [DRAW drawMsg]) (model, [DRAW drawMsg])
@@ -180,11 +181,13 @@ struct
let let
val model = AppWith.windowResize (model, width, height) val model = AppWith.windowResize (model, width, height)
val {squares, canvasWidth, canvasHeight, showGraph, arrowX, arrowY, ...} = val {squares, canvasWidth, canvasHeight, showGraph, arrowX, arrowY,
xClickPoints, yClickPoints, ...} =
model model
val maxSide = Int.max (canvasWidth, canvasHeight) val maxSide = Int.max (canvasWidth, canvasHeight)
val squares = CollisionTree.toTriangles (width, height, squares, maxSide) val squares = CollisionTree.toTriangles (width, height, squares, maxSide,
canvasWidth, canvasHeight, xClickPoints, yClickPoints)
val graphLines = val graphLines =
if showGraph then GraphLines.generate model else Vector.fromList [] if showGraph then GraphLines.generate model else Vector.fromList []

View File

@@ -236,36 +236,43 @@ struct
end end
local local
fun loop (windowWidth, windowHeight, squares, acc) = fun loop (windowWidth, windowHeight, squares, acc, canvasWidth,
canvasHeight, xClickPoints, yClickPoints) =
case squares of case squares of
{x, y, ex, ey, data = _} :: tl => {x, y, ex, ey, data = _} :: tl =>
let let
val x = Real32.fromInt x val ex = if ex = x then x + 1 else ex
val y = Real32.fromInt y val ey = if ey = y then y + 1 else ey
val ex = Real32.fromInt ex
val ey = Real32.fromInt ey val x = Vector.sub (xClickPoints, x)
val ex = Vector.sub (xClickPoints, ex)
val y = Vector.sub (yClickPoints, y)
val ey = Vector.sub (yClickPoints, ey)
val startX = Ndc.fromPixelX (x, windowWidth, windowHeight) val startX = Ndc.fromPixelX (x, windowWidth, windowHeight)
val endX = Ndc.fromPixelX (ex, windowWidth, windowHeight) val endX = Ndc.fromPixelX (ex , windowWidth, windowHeight)
val startY = Ndc.fromPixelY (y, windowWidth, windowHeight) val startY = Ndc.fromPixelY (y, windowWidth, windowHeight)
val endY = Ndc.fromPixelY (ey, windowWidth, windowHeight) val endY = Ndc.fromPixelY (ey , windowWidth, windowHeight)
val vec = val vec =
Ndc.ltrbToVertexRgb (startX, startY, endX, endY, 0.0, 0.0, 0.0) Ndc.ltrbToVertexRgb (startX, startY, endX, endY, 0.0, 0.0, 0.0)
val acc = vec :: acc val acc = vec :: acc
in in
loop (windowWidth, windowHeight, tl, acc) loop (windowWidth, windowHeight, tl, acc, canvasWidth, canvasHeight,
xClickPoints, yClickPoints)
end end
| [] => Vector.concat acc | [] => Vector.concat acc
in in
fun toTriangles (windowWidth, windowHeight, squares, size) = fun toTriangles (windowWidth, windowHeight, squares, size, canvasWidth,
canvasHeight, xClickPoints, yClickPoints) =
let let
val qtree = build (0, 0, size, squares) val qtree = build (0, 0, size, squares)
val squares = toList qtree val squares = toList qtree
val msg = List.length squares val msg = List.length squares
val () = print (Int.toString msg ^ "\n") val () = print (Int.toString msg ^ "\n")
in in
loop (windowWidth, windowHeight, squares, []) loop (windowWidth, windowHeight, squares, [], canvasWidth, canvasHeight,
xClickPoints, yClickPoints)
end end
end end
end end