add and improve code to generate graphLines dynamically

This commit is contained in:
2024-08-03 02:31:35 +01:00
parent 6c444696d4
commit 57c21448e7
3 changed files with 196 additions and 712 deletions

BIN
dotscape

Binary file not shown.

View File

@@ -22,10 +22,13 @@ sig
, windowHeight: int
, xClickPoints: Real32.real vector
, yClickPoints: Real32.real vector
, graphLines: Real32.real vector
}
val getInitial: int * int -> app_type
val genGraphLines: int * int -> Real32.real vector
val genClickPoints: int * int -> Real32.real vector
val withTriangleStage: app_type * triangle_stage -> app_type
@@ -75,6 +78,7 @@ struct
, windowHeight: int
, xClickPoints: Real32.real vector
, yClickPoints: Real32.real vector
, graphLines: Real32.real vector
}
fun genClickPoints (start, finish) =
@@ -86,6 +90,184 @@ struct
Vector.tabulate (41, fn idx => (Real32.fromInt idx * increment) + start)
end
local
(* This function only produces the desired result
* when the window is a square and has the aspect ratio 1:1.
* This is because the function assumes it can use
* the same position coordinates both horizontally and vertically.
* *)
fun helpGenGraphLinesSquare (pos: Real32.real, limit, acc) =
if pos >= limit then
Vector.concat acc
else
let
val pos2 = pos + 0.05
val vec = Vector.fromList
[ (* x = _.1 *)
pos - 0.002, ~1.0
, pos + 0.002, ~1.0
, pos + 0.002, 1.0
, pos + 0.002, 1.0
, pos - 0.002, 1.0
, pos - 0.002, ~1.0
(* y = _.1 *)
, ~1.0, pos - 0.002
, ~1.0, pos + 0.002
, 1.0, pos + 0.002
, 1.0, pos + 0.002
, 1.0, pos - 0.002
, ~1.0, pos - 0.002
(* x = _.05 *)
, pos2 - 0.001, ~1.0
, pos2 + 0.001, ~1.0
, pos2 + 0.001, 1.0
, pos2 + 0.001, 1.0
, pos2 - 0.001, 1.0
, pos2 - 0.001, ~1.0
(* y = _.05 *)
, ~1.0, pos2 - 0.001
, ~1.0, pos2 + 0.001
, 1.0, pos2 + 0.001
, 1.0, pos2 + 0.001
, 1.0, pos2 - 0.001
, ~1.0, pos2 - 0.001
]
val acc = vec :: acc
val nextPos = pos + 0.1
in
helpGenGraphLinesSquare (nextPos, limit, acc)
end
fun helpGenGraphLinesVertical (pos, limit, acc) =
if pos <= limit + 0.01 then
let
val vec = Vector.fromList
[ (* y = _.1 *)
~1.0, pos - 0.002
, ~1.0, pos + 0.002
, 1.0, pos + 0.002
, 1.0, pos + 0.002
, 1.0, pos - 0.002
, ~1.0, pos - 0.002
]
val acc = vec :: acc
val pos = pos + 0.05
in
if pos <= limit + 0.01 then
let
val vec = Vector.fromList
[ (* y = _.05 *)
~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 pos = pos + 0.05
in
helpGenGraphLinesVertical (pos, limit, acc)
end
else
acc
end
else
acc
fun helpGenGraphLinesHorizontal (pos, limit, acc) =
if pos <= limit + 0.01 then
let
val pos2 = pos + 0.05
val vec = Vector.fromList
[ (* x = _.1 *)
pos - 0.002, ~1.0
, pos + 0.002, ~1.0
, pos + 0.002, 1.0
, pos + 0.002, 1.0
, pos - 0.002, 1.0
, pos - 0.002, ~1.0
]
val acc = vec :: acc
val pos = pos + 0.05
in
if pos <= limit + 0.01 then
let
val vec = Vector.fromList
[
(* x = _.05 *)
pos2 - 0.001, ~1.0
, pos2 + 0.001, ~1.0
, pos2 + 0.001, 1.0
, pos2 + 0.001, 1.0
, pos2 - 0.001, 1.0
, pos2 - 0.001, ~1.0
]
val acc = vec :: acc
val pos = pos + 0.05
in
helpGenGraphLinesHorizontal (pos, limit, acc)
end
else
acc
end
else
acc
in
fun genGraphLines (windowWidth, windowHeight) =
if windowWidth = windowHeight then
helpGenGraphLinesSquare (~1.0, 1.0, [])
else if windowWidth > windowHeight then
let
val difference = windowWidth - windowHeight
val offset = difference div 2
val halfWidth = Real32.fromInt (windowWidth div 2)
val start = offset - (windowWidth div 2)
val _ = print ("start = " ^ Int.toString start ^ "\n")
val start = Real32.fromInt start / halfWidth
val finish = (windowWidth - offset) - (windowWidth div 2)
val _ = print ("finish = " ^ Int.toString finish ^ "\n")
val finish = Real32.fromInt finish / halfWidth
val lines = helpGenGraphLinesHorizontal (start, finish, [])
val lines = helpGenGraphLinesVertical (~1.0, 1.0, lines)
in
Vector.concat lines
end
else
(* windowWidth < windowHeight *)
let
val difference = windowHeight - windowWidth
val offset = difference div 2
val offset = Real32.fromInt (difference - (windowWidth div 2))
val ndcOffset = offset / Real32.fromInt windowWidth
val start = ndcOffset
val finish =
if ndcOffset > 0.0 then
1.0 - ndcOffset
else
1.0 + ndcOffset
val lines = helpGenGraphLinesHorizontal (~1.0, 1.0, [])
val lines = helpGenGraphLinesVertical (start, finish, lines)
in
Vector.concat lines
end
end
local
fun make (windowWidth, windowHeight, wStart, wFinish, hStart, hFinish) =
{ triangles = []
@@ -94,6 +276,7 @@ struct
, windowHeight = windowHeight
, xClickPoints = genClickPoints (wStart, wFinish)
, yClickPoints = genClickPoints (hStart, hFinish)
, graphLines = genGraphLines (windowWidth, windowHeight)
}
in
fun getInitial (windowWidth, windowHeight) =
@@ -126,6 +309,7 @@ struct
, yClickPoints
, windowWidth
, windowHeight
, graphLines
} = app
in
{ triangleStage = newTriangleStage
@@ -134,6 +318,7 @@ struct
, yClickPoints = yClickPoints
, windowWidth = windowWidth
, windowHeight = windowHeight
, graphLines = graphLines
}
end
end
@@ -148,6 +333,7 @@ struct
, yClickPoints
, windowWidth
, windowHeight
, graphLines
} = app
val newTriangle = {x1 = x1, y1 = y1, x2 = x2, y2 = y2, x3 = x3, y3 = y3}
@@ -159,6 +345,7 @@ struct
, yClickPoints = yClickPoints
, windowWidth = windowWidth
, windowHeight = windowHeight
, graphLines = graphLines
}
end
@@ -176,17 +363,16 @@ struct
val
{ xClickPoints = _
, yClickPoints = _
, triangles
, triangleStage
, windowWidth = _
, windowHeight = _
, graphLines = _
, triangles
, triangleStage
} = app
val xClickPoints = genClickPoints (wStart, wFinish)
val yClickPoints = genClickPoints (hStart, hFinish)
in
{ xClickPoints = xClickPoints
, yClickPoints = yClickPoints
{ xClickPoints = genClickPoints (wStart, wFinish)
, yClickPoints = genClickPoints (hStart, hFinish)
, graphLines = genGraphLines (windowWidth, windowHeight)
, triangles = triangles
, triangleStage = triangleStage
, windowWidth = windowWidth

View File

@@ -1,6 +1,6 @@
structure Constants =
struct
val windowWidth = 900
val windowWidth = 1000
val windowHeight = 900
val graphVertexShaderString =
@@ -41,708 +41,6 @@ struct
\ FragColor = vec4(frag_col.x, frag_col.y, frag_col.z, 1.0f);\n\
\}"
val graphLines: Real32.real vector =
#[
(* x = ~0.95 *)
~0.949, ~1.0,
~0.951, ~1.0,
~0.951, 1.0,
~0.951, 1.0,
~0.949, 1.0,
~0.949, ~1.0,
(* x = ~0.9 *)
~0.898, ~1.0,
~0.902, ~1.0,
~0.902, 1.0,
~0.902, 1.0,
~0.898, 1.0,
~0.898, ~1.0,
(* x = ~0.85 *)
~0.849, ~1.0,
~0.851, ~1.0,
~0.851, 1.0,
~0.851, 1.0,
~0.849, 1.0,
~0.849, ~1.0,
(* x = ~0.8 *)
~0.798, ~1.0,
~0.802, ~1.0,
~0.802, 1.0,
~0.802, 1.0,
~0.798, 1.0,
~0.798, ~1.0,
(* x = ~0.75 *)
~0.749, ~1.0,
~0.751, ~1.0,
~0.751, 1.0,
~0.751, 1.0,
~0.749, 1.0,
~0.749, ~1.0,
(* x = ~0.7 *)
~0.698, ~1.0,
~0.702, ~1.0,
~0.702, 1.0,
~0.702, 1.0,
~0.698, 1.0,
~0.698, ~1.0,
(* x = ~0.65 *)
~0.649, ~1.0,
~0.651, ~1.0,
~0.651, 1.0,
~0.651, 1.0,
~0.649, 1.0,
~0.649, ~1.0,
(* x = ~0.6 *)
~0.598, ~1.0,
~0.602, ~1.0,
~0.602, 1.0,
~0.602, 1.0,
~0.598, 1.0,
~0.598, ~1.0,
(* x = ~0.55 *)
~0.549, ~1.0,
~0.551, ~1.0,
~0.551, 1.0,
~0.551, 1.0,
~0.549, 1.0,
~0.549, ~1.0,
(* x = ~0.5 *)
~0.498, ~1.0,
~0.502, ~1.0,
~0.502, 1.0,
~0.502, 1.0,
~0.498, 1.0,
~0.498, ~1.0,
(* x = ~0.45 *)
~0.449, ~1.0,
~0.451, ~1.0,
~0.451, 1.0,
~0.451, 1.0,
~0.449, 1.0,
~0.449, ~1.0,
(* x = ~0.4 *)
~0.398, ~1.0,
~0.402, ~1.0,
~0.402, 1.0,
~0.402, 1.0,
~0.398, 1.0,
~0.398, ~1.0,
(* x = ~0.35 *)
~0.349, ~1.0,
~0.351, ~1.0,
~0.351, 1.0,
~0.351, 1.0,
~0.349, 1.0,
~0.349, ~1.0,
(* x = ~0.3 *)
~0.298, ~1.0,
~0.302, ~1.0,
~0.302, 1.0,
~0.302, 1.0,
~0.298, 1.0,
~0.298, ~1.0,
(* x = ~0.25 *)
~0.249, ~1.0,
~0.251, ~1.0,
~0.251, 1.0,
~0.251, 1.0,
~0.249, 1.0,
~0.249, ~1.0,
(* x = ~0.2 *)
~0.198, ~1.0,
~0.202, ~1.0,
~0.202, 1.0,
~0.202, 1.0,
~0.198, 1.0,
~0.198, ~1.0,
(* x = ~0.15 *)
~0.149, ~1.0,
~0.151, ~1.0,
~0.151, 1.0,
~0.151, 1.0,
~0.149, 1.0,
~0.149, ~1.0,
(* x = ~0.1 *)
~0.098, ~1.0,
~0.102, ~1.0,
~0.102, 1.0,
~0.102, 1.0,
~0.098, 1.0,
~0.098, ~1.0,
(* x = ~0.05 *)
~0.049, ~1.0,
~0.051, ~1.0,
~0.051, 1.0,
~0.051, 1.0,
~0.049, 1.0,
~0.049, ~1.0,
(* x = 0.0 *)
~0.002, ~1.0,
0.002, ~1.0,
0.002, 1.0,
0.002, 1.0,
~0.002, 1.0,
~0.002, ~1.0,
(* x = 0.05 *)
0.049, ~1.0,
0.051, ~1.0,
0.051, 1.0,
0.051, 1.0,
0.049, 1.0,
0.049, ~1.0,
(* x = 0.0 *)
0.098, ~1.0,
0.102, ~1.0,
0.102, 1.0,
0.102, 1.0,
0.098, 1.0,
0.098, ~1.0,
(* x = 0.15 *)
0.149, ~1.0,
0.151, ~1.0,
0.151, 1.0,
0.151, 1.0,
0.149, 1.0,
0.149, ~1.0,
(* x = 0.2 *)
0.198, ~1.0,
0.202, ~1.0,
0.202, 1.0,
0.202, 1.0,
0.198, 1.0,
0.198, ~1.0,
(* x = 0.25 *)
0.249, ~1.0,
0.251, ~1.0,
0.251, 1.0,
0.251, 1.0,
0.249, 1.0,
0.249, ~1.0,
(* x = 0.3 *)
0.298, ~1.0,
0.302, ~1.0,
0.302, 1.0,
0.302, 1.0,
0.298, 1.0,
0.298, ~1.0,
(* x = 0.35 *)
0.349, ~1.0,
0.351, ~1.0,
0.351, 1.0,
0.351, 1.0,
0.349, 1.0,
0.349, ~1.0,
(* x = 0.4 *)
0.398, ~1.0,
0.402, ~1.0,
0.402, 1.0,
0.402, 1.0,
0.398, 1.0,
0.398, ~1.0,
(* x = 0.45 *)
0.449, ~1.0,
0.451, ~1.0,
0.451, 1.0,
0.451, 1.0,
0.449, 1.0,
0.449, ~1.0,
(* x = 0.5 *)
0.498, ~1.0,
0.502, ~1.0,
0.502, 1.0,
0.502, 1.0,
0.498, 1.0,
0.498, ~1.0,
(* x = 0.55 *)
0.549, ~1.0,
0.551, ~1.0,
0.551, 1.0,
0.551, 1.0,
0.549, 1.0,
0.549, ~1.0,
(* x = 0.6 *)
0.598, ~1.0,
0.602, ~1.0,
0.602, 1.0,
0.602, 1.0,
0.598, 1.0,
0.598, ~1.0,
(* x = 0.65 *)
0.649, ~1.0,
0.651, ~1.0,
0.651, 1.0,
0.651, 1.0,
0.649, 1.0,
0.649, ~1.0,
(* x = 0.7 *)
0.698, ~1.0,
0.702, ~1.0,
0.702, 1.0,
0.702, 1.0,
0.698, 1.0,
0.698, ~1.0,
(* x = 0.75 *)
0.749, ~1.0,
0.751, ~1.0,
0.751, 1.0,
0.751, 1.0,
0.749, 1.0,
0.749, ~1.0,
(* x = 0.8 *)
0.798, ~1.0,
0.802, ~1.0,
0.802, 1.0,
0.802, 1.0,
0.798, 1.0,
0.798, ~1.0,
(* x = 0.85 *)
0.849, ~1.0,
0.851, ~1.0,
0.851, 1.0,
0.851, 1.0,
0.849, 1.0,
0.849, ~1.0,
(* x = 0.9 *)
0.898, ~1.0,
0.902, ~1.0,
0.902, 1.0,
0.902, 1.0,
0.898, 1.0,
0.898, ~1.0,
(* x = 0.95 *)
0.949, ~1.0,
0.951, ~1.0,
0.951, 1.0,
0.951, 1.0,
0.949, 1.0,
0.949, ~1.0,
(* y = ~0.95 *)
~1.0, ~0.949,
~1.0, ~0.951,
1.0, ~0.951,
1.0, ~0.951,
1.0, ~0.949,
~1.0, ~0.949,
(* y = ~0.9 *)
~1.0, ~0.898,
~1.0, ~0.902,
1.0, ~0.902,
1.0, ~0.902,
1.0, ~0.898,
~1.0, ~0.898,
(* y = ~0.85 *)
~1.0, ~0.849,
~1.0, ~0.851,
1.0, ~0.851,
1.0, ~0.851,
1.0, ~0.849,
~1.0, ~0.849,
(* y = ~0.8 *)
~1.0, ~0.798,
~1.0, ~0.802,
1.0, ~0.802,
1.0, ~0.802,
1.0, ~0.798,
~1.0, ~0.798,
(* y = ~0.75 *)
~1.0, ~0.749,
~1.0, ~0.751,
1.0, ~0.751,
1.0, ~0.751,
1.0, ~0.749,
~1.0, ~0.749,
(* y = ~0.7 *)
~1.0, ~0.698,
~1.0, ~0.702,
1.0, ~0.702,
1.0, ~0.702,
1.0, ~0.698,
~1.0, ~0.698,
(* y = ~0.65 *)
~1.0, ~0.649,
~1.0, ~0.651,
1.0, ~0.651,
1.0, ~0.651,
1.0, ~0.649,
~1.0, ~0.649,
(* y = ~0.6 *)
~1.0, ~0.598,
~1.0, ~0.602,
1.0, ~0.602,
1.0, ~0.602,
1.0, ~0.598,
~1.0, ~0.598,
(* y = ~0.55 *)
~1.0, ~0.549,
~1.0, ~0.551,
1.0, ~0.551,
1.0, ~0.551,
1.0, ~0.549,
~1.0, ~0.549,
(* y = ~0.5 *)
~1.0, ~0.498,
~1.0, ~0.502,
1.0, ~0.502,
1.0, ~0.502,
1.0, ~0.498,
~1.0, ~0.498,
(* y = ~0.45 *)
~1.0, ~0.449,
~1.0, ~0.451,
1.0, ~0.451,
1.0, ~0.451,
1.0, ~0.449,
~1.0, ~0.449,
(* y = ~0.4 *)
~1.0, ~0.398,
~1.0, ~0.402,
1.0, ~0.402,
1.0, ~0.402,
1.0, ~0.398,
~1.0, ~0.398,
(* y = ~0.35 *)
~1.0, ~0.349,
~1.0, ~0.351,
1.0, ~0.351,
1.0, ~0.351,
1.0, ~0.349,
~1.0, ~0.349,
(* y = ~0.3 *)
~1.0, ~0.298,
~1.0, ~0.302,
1.0, ~0.302,
1.0, ~0.302,
1.0, ~0.298,
~1.0, ~0.298,
(* y = ~0.25 *)
~1.0, ~0.249,
~1.0, ~0.251,
1.0, ~0.251,
1.0, ~0.251,
1.0, ~0.249,
~1.0, ~0.249,
(* y = ~0.2 *)
~1.0, ~0.198,
~1.0, ~0.202,
1.0, ~0.202,
1.0, ~0.202,
1.0, ~0.198,
~1.0, ~0.198,
(* y = ~0.15 *)
~1.0, ~0.149,
~1.0, ~0.151,
1.0, ~0.151,
1.0, ~0.151,
1.0, ~0.149,
~1.0, ~0.149,
(* y = ~0.1 *)
~1.0, ~0.098,
~1.0, ~0.102,
1.0, ~0.102,
1.0, ~0.102,
1.0, ~0.098,
~1.0, ~0.098,
(* y = ~0.05 *)
~1.0, ~0.049,
~1.0, ~0.051,
1.0, ~0.051,
1.0, ~0.051,
1.0, ~0.049,
~1.0, ~0.049,
(* y = 0.0 *)
~1.0, ~0.002,
~1.0, 0.002,
1.0, 0.002,
1.0, 0.002,
1.0, ~0.002,
~1.0, ~0.002,
(* y = 0.05 *)
~1.0, 0.049,
~1.0, 0.051,
1.0, 0.051,
1.0, 0.051,
1.0, 0.049,
~1.0, 0.049,
(* y = 0.0 *)
~1.0, 0.098,
~1.0, 0.102,
1.0, 0.102,
1.0, 0.102,
1.0, 0.098,
~1.0, 0.098,
(* y = 0.15 *)
~1.0, 0.149,
~1.0, 0.151,
1.0, 0.151,
1.0, 0.151,
1.0, 0.149,
~1.0, 0.149,
(* y = 0.2 *)
~1.0, 0.198,
~1.0, 0.202,
1.0, 0.202,
1.0, 0.202,
1.0, 0.198,
~1.0, 0.198,
(* y = 0.25 *)
~1.0, 0.249,
~1.0, 0.251,
1.0, 0.251,
1.0, 0.251,
1.0, 0.249,
~1.0, 0.249,
(* y = 0.3 *)
~1.0, 0.298,
~1.0, 0.302,
1.0, 0.302,
1.0, 0.302,
1.0, 0.298,
~1.0, 0.298,
(* y = 0.35 *)
~1.0, 0.349,
~1.0, 0.351,
1.0, 0.351,
1.0, 0.351,
1.0, 0.349,
~1.0, 0.349,
(* y = 0.4 *)
~1.0, 0.398,
~1.0, 0.402,
1.0, 0.402,
1.0, 0.402,
1.0, 0.398,
~1.0, 0.398,
(* y = 0.45 *)
~1.0, 0.449,
~1.0, 0.451,
1.0, 0.451,
1.0, 0.451,
1.0, 0.449,
~1.0, 0.449,
(* y = 0.5 *)
~1.0, 0.498,
~1.0, 0.502,
1.0, 0.502,
1.0, 0.502,
1.0, 0.498,
~1.0, 0.498,
(* y = 0.55 *)
~1.0, 0.549,
~1.0, 0.551,
1.0, 0.551,
1.0, 0.551,
1.0, 0.549,
~1.0, 0.549,
(* y = 0.6 *)
~1.0, 0.598,
~1.0, 0.602,
1.0, 0.602,
1.0, 0.602,
1.0, 0.598,
~1.0, 0.598,
(* y = 0.65 *)
~1.0, 0.649,
~1.0, 0.651,
1.0, 0.651,
1.0, 0.651,
1.0, 0.649,
~1.0, 0.649,
(* y = 0.7 *)
~1.0, 0.698,
~1.0, 0.702,
1.0, 0.702,
1.0, 0.702,
1.0, 0.698,
~1.0, 0.698,
(* y = 0.75 *)
~1.0, 0.749,
~1.0, 0.751,
1.0, 0.751,
1.0, 0.751,
1.0, 0.749,
~1.0, 0.749,
(* y = 0.8 *)
~1.0, 0.798,
~1.0, 0.802,
1.0, 0.802,
1.0, 0.802,
1.0, 0.798,
~1.0, 0.798,
(* y = 0.85 *)
~1.0, 0.849,
~1.0, 0.851,
1.0, 0.851,
1.0, 0.851,
1.0, 0.849,
~1.0, 0.849,
(* y = 0.9 *)
~1.0, 0.898,
~1.0, 0.902,
1.0, 0.902,
1.0, 0.902,
1.0, 0.898,
~1.0, 0.898,
(* y = 0.95 *)
~1.0, 0.949,
~1.0, 0.951,
1.0, 0.951,
1.0, 0.951,
1.0, 0.949,
~1.0, 0.949
]
val graphLines: Real32.real vector =
AppType.genGraphLines (windowWidth, windowHeight)
end