improve generated graph lines, so it aligns perfectly with clickPoints (but there are two 'todo' notes: one when the height is greather than the width, and also the constant vector when the program starts is an empty vector)

This commit is contained in:
2024-08-03 04:40:53 +01:00
parent 5a14f5300b
commit ee2274ffbc
8 changed files with 148 additions and 107 deletions

BIN
dotscape

Binary file not shown.

View File

@@ -27,7 +27,7 @@ sig
val getInitial: int * int -> app_type
val genGraphLines: int * int -> Real32.real vector
val genGraphLines: int * int * Real32.real vector * Real32.real vector -> Real32.real vector
val genClickPoints: int * int -> Real32.real vector
@@ -144,87 +144,83 @@ struct
helpGenGraphLinesSquare (nextPos, limit, acc)
end
fun helpGenGraphLinesVertical (pos, limit, acc, xMin, xMax) =
if pos <= limit + 0.01 then
let
val vec = Vector.fromList
[ (* y = _.1 *)
xMin, pos - 0.002
, xMin, pos + 0.002
, xMax, pos + 0.002
, xMax, pos + 0.002
, xMax, pos - 0.002
, xMin, 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 *)
xMin, pos - 0.001
, xMin, pos + 0.001
, xMax, pos + 0.001
, xMax, pos + 0.001
, xMax, pos - 0.001
, xMin, pos - 0.001
]
val acc = vec :: acc
val pos = pos + 0.05
in
helpGenGraphLinesVertical (pos, limit, acc, xMin, xMax)
end
else
acc
end
else
fun helpGenGraphLinesHorizontal (pos, xClickPoints, acc, halfWidth, yMin, yMax) =
if pos = Vector.length xClickPoints then
acc
fun helpGenGraphLinesHorizontal (pos, limit, acc, yMin, yMax) =
if pos <= limit + 0.01 then
let
val pos2 = pos + 0.05
val vec = Vector.fromList
[ (* x = _.1 *)
pos - 0.002, yMin
, pos + 0.002, yMin
, pos + 0.002, yMax
, pos + 0.002, yMax
, pos - 0.002, yMax
, pos - 0.002, yMin
]
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, yMin
, pos2 + 0.001, yMin
, pos2 + 0.001, yMax
, pos2 + 0.001, yMax
, pos2 - 0.001, yMax
, pos2 - 0.001, yMin
]
val acc = vec :: acc
val pos = pos + 0.05
in
helpGenGraphLinesHorizontal (pos, limit, acc, yMin, yMax)
end
else
acc
end
else
let
val curX = Vector.sub (xClickPoints, pos)
val ndc = (curX - halfWidth) / halfWidth
val vec =
if (pos + 1) mod 2 = 0 then
(* if even (thin lines) *)
Vector.fromList
[
ndc - 0.001, yMin
, ndc + 0.001, yMin
, ndc + 0.001, yMax
, ndc + 0.001, yMax
, ndc - 0.001, yMax
, ndc - 0.001, yMin
]
else
(* if odd (thick lines) *)
Vector.fromList
[
ndc - 0.002, yMin
, ndc + 0.002, yMin
, ndc + 0.002, yMax
, ndc + 0.002, yMax
, ndc - 0.002, yMax
, ndc - 0.002, yMin
]
val acc = vec:: acc
in
helpGenGraphLinesHorizontal
(pos + 1, xClickPoints, acc, halfWidth, yMin, yMax)
end
fun helpGenGraphLinesVertical (pos, yClickPoints, acc, halfHeight, xMin, xMax) =
if pos = Vector.length yClickPoints then
acc
else
let
val curY = Vector.sub (yClickPoints, pos)
val ndc = (curY - halfHeight) / halfHeight
val vec =
if (pos + 1) mod 2 = 0 then
(* if even (thin lines) *)
Vector.fromList
[
xMin, ndc - 0.001
, xMin, ndc + 0.001
, xMax, ndc + 0.001
, xMax, ndc + 0.001
, xMax, ndc - 0.001
, xMin, ndc - 0.001
]
else
(* if odd (thick lines) *)
Vector.fromList
[
xMin, ndc - 0.002
, xMin, ndc + 0.002
, xMax, ndc + 0.002
, xMax, ndc + 0.002
, xMax, ndc - 0.002
, xMin, ndc - 0.002
]
val acc = vec:: acc
in
helpGenGraphLinesVertical
(pos + 1, yClickPoints, acc, halfHeight, xMin, xMax)
end
in
fun genGraphLines (windowWidth, windowHeight) =
fun genGraphLines (windowWidth, windowHeight, xClickPoints, yClickPoints) =
if windowWidth = windowHeight then
helpGenGraphLinesSquare (~1.0, 1.0, [])
else if windowWidth > windowHeight then
@@ -233,48 +229,45 @@ struct
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 (start, finish, [], ~1.0, 1.0)
val lines = helpGenGraphLinesVertical (~1.0, 1.0, lines, start, finish)
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 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, [], start, finish)
val lines = helpGenGraphLinesVertical (start, finish, lines, ~1.0, 1.0)
(* todo; fix based on windowWidth > windowHeight *)
in
Vector.concat lines
Vector.fromList []
end
end
local
fun make (windowWidth, windowHeight, wStart, wFinish, hStart, hFinish) =
let
val xClickPoints = genClickPoints (wStart, wFinish)
val yClickPoints = genClickPoints (hStart, hFinish)
in
{ triangles = []
, triangleStage = NO_TRIANGLE
, windowWidth = windowWidth
, windowHeight = windowHeight
, xClickPoints = genClickPoints (wStart, wFinish)
, yClickPoints = genClickPoints (hStart, hFinish)
, graphLines = genGraphLines (windowWidth, windowHeight)
, xClickPoints = xClickPoints
, yClickPoints = yClickPoints
, graphLines = genGraphLines (windowWidth, windowHeight, xClickPoints, yClickPoints)
}
end
in
fun getInitial (windowWidth, windowHeight) =
if windowWidth = windowHeight then
@@ -366,10 +359,12 @@ struct
, triangles
, triangleStage
} = app
val xClickPoints = genClickPoints (wStart, wFinish)
val yClickPoints = genClickPoints (hStart, hFinish)
in
{ xClickPoints = genClickPoints (wStart, wFinish)
, yClickPoints = genClickPoints (hStart, hFinish)
, graphLines = genGraphLines (windowWidth, windowHeight)
{ xClickPoints = xClickPoints
, yClickPoints = yClickPoints
, graphLines = genGraphLines (windowWidth, windowHeight, xClickPoints, yClickPoints)
, triangles = triangles
, triangleStage = triangleStage
, windowWidth = windowWidth

View File

@@ -385,8 +385,10 @@ struct
val _ = print msg
val model = AppType.withWindowResize (model, width, height)
val drawVec = getTrianglesVector model
val drawMsg = DRAW_TRIANGLES_AND_RESET_BUTTONS drawVec
val triangles = getTrianglesVector model
val graphLines = #graphLines model
val drawMsg = RESIZE_TRIANGLES_BUTTONS_AND_GRAPH {triangles = triangles,
graphLines = graphLines}
in
(model, drawMsg, mouseX, mouseY)
end

View File

@@ -52,15 +52,23 @@ struct
graphDrawObject
end
fun drawGraphLines (graphDrawObject: draw_object) =
fun uploadGraphLines (graphDrawObject: draw_object, vec) =
let
val {vertexBuffer, ...} = graphDrawObject
val _ = Gles3.bindBuffer vertexBuffer
val _ = Gles3.bufferData (vec, Vector.length vec, Gles3.STATIC_DRAW ())
in
()
end
fun drawGraphLines (graphDrawObject: draw_object, graphDrawLength) =
let
val {vertexBuffer, program, ...} = graphDrawObject
val _ = Gles3.bindBuffer vertexBuffer
val _ = Gles3.vertexAttribPointer (0, 2, 2, 0)
val _ = Gles3.enableVertexAttribArray 0
val _ = Gles3.useProgram program
val _ = Gles3.drawArrays
(Gles3.TRIANGLES (), 0, Vector.length Constants.graphLines div 2)
val _ = Gles3.drawArrays (Gles3.TRIANGLES (), 0, graphDrawLength)
in
()
end

View File

@@ -41,6 +41,8 @@ struct
\ FragColor = vec4(frag_col.x, frag_col.y, frag_col.z, 1.0f);\n\
\}"
(* Todo: fix. Currently an empty vector which is not desired result,
* but changing vector dynamically (through resizing) works as desired. *)
val graphLines: Real32.real vector =
AppType.genGraphLines (windowWidth, windowHeight)
Vector.fromList []
end

View File

@@ -28,6 +28,7 @@ struct
( drawMailbox
, window
, graphDrawObject
, drawGraphLength
, buttonDrawObject
, buttonDrawLength
, triangleDrawObject
@@ -40,7 +41,7 @@ struct
val _ = Gles3.clearColor (1.0, 1.0, 1.0, 1.0)
val _ = Gles3.clear ()
val _ = AppDraw.drawGraphLines graphDrawObject
val _ = AppDraw.drawGraphLines (graphDrawObject, drawGraphLength)
val _ =
AppDraw.drawTriangles (triangleDrawObject, triangleDrawLength)
val _ = AppDraw.drawButton (buttonDrawObject, buttonDrawLength)
@@ -52,6 +53,7 @@ struct
( drawMailbox
, window
, graphDrawObject
, drawGraphLength
, buttonDrawObject
, buttonDrawLength
, triangleDrawObject
@@ -69,6 +71,7 @@ struct
( drawMailbox
, window
, graphDrawObject
, drawGraphLength
, buttonDrawObject
, buttonDrawLength
, triangleDrawObject
@@ -87,6 +90,31 @@ struct
( drawMailbox
, window
, graphDrawObject
, drawGraphLength
, buttonDrawObject
, 0
, triangleDrawObject
, triangleDrawLength
)
end
| RESIZE_TRIANGLES_BUTTONS_AND_GRAPH {triangles, graphLines} =>
let
val _ = print ("resize in event loop\n")
val _ =
AppDraw.uploadTrianglesVector
(triangleDrawObject, triangles)
val triangleDrawLength = Vector.length triangles div 2
(* buttons are reset by setting buttonDrawLength to 0 *)
val _ =
AppDraw.uploadGraphLines
(graphDrawObject, graphLines)
val drawGraphLength = Vector.length graphLines div 2
in
draw
( drawMailbox
, window
, graphDrawObject
, drawGraphLength
, buttonDrawObject
, 0
, triangleDrawObject
@@ -98,6 +126,7 @@ struct
( drawMailbox
, window
, graphDrawObject
, drawGraphLength
, buttonDrawObject
, buttonDrawLength
, triangleDrawObject

View File

@@ -30,6 +30,7 @@ struct
( drawMailbox
, window
, graphDrawObject
, Vector.length Constants.graphLines div 2
, buttonDrawObject
, 0
, triangleDrawObject

View File

@@ -3,6 +3,8 @@ sig
datatype t =
DRAW_BUTTON of Real32.real vector
| DRAW_TRIANGLES_AND_RESET_BUTTONS of Real32.real vector
| RESIZE_TRIANGLES_BUTTONS_AND_GRAPH of
{triangles: Real32.real vector, graphLines: Real32.real vector}
| NO_DRAW
end
@@ -11,5 +13,7 @@ struct
datatype t =
DRAW_BUTTON of Real32.real vector
| DRAW_TRIANGLES_AND_RESET_BUTTONS of Real32.real vector
| RESIZE_TRIANGLES_BUTTONS_AND_GRAPH of
{triangles: Real32.real vector, graphLines: Real32.real vector}
| NO_DRAW
end