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 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 val genClickPoints: int * int -> Real32.real vector
@@ -144,87 +144,83 @@ struct
helpGenGraphLinesSquare (nextPos, limit, acc) helpGenGraphLinesSquare (nextPos, limit, acc)
end end
fun helpGenGraphLinesVertical (pos, limit, acc, xMin, xMax) = fun helpGenGraphLinesHorizontal (pos, xClickPoints, acc, halfWidth, yMin, yMax) =
if pos <= limit + 0.01 then if pos = Vector.length xClickPoints 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
acc 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 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 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 in
fun genGraphLines (windowWidth, windowHeight) = fun genGraphLines (windowWidth, windowHeight, xClickPoints, yClickPoints) =
if windowWidth = windowHeight then if windowWidth = windowHeight then
helpGenGraphLinesSquare (~1.0, 1.0, []) helpGenGraphLinesSquare (~1.0, 1.0, [])
else if windowWidth > windowHeight then else if windowWidth > windowHeight then
@@ -233,48 +229,45 @@ struct
val offset = difference div 2 val offset = difference div 2
val halfWidth = Real32.fromInt (windowWidth div 2) val halfWidth = Real32.fromInt (windowWidth div 2)
val halfHeight = Real32.fromInt (windowHeight div 2)
val start = offset - (windowWidth div 2) val start = offset - (windowWidth div 2)
val start = Real32.fromInt start / halfWidth val start = Real32.fromInt start / halfWidth
val finish = (windowWidth - offset) - (windowWidth div 2) val finish = (windowWidth - offset) - (windowWidth div 2)
val finish = Real32.fromInt finish / halfWidth val finish = Real32.fromInt finish / halfWidth
val lines = helpGenGraphLinesHorizontal (start, finish, [], ~1.0, 1.0) val lines = helpGenGraphLinesHorizontal
val lines = helpGenGraphLinesVertical (~1.0, 1.0, lines, start, finish) (0, xClickPoints, [], halfWidth, ~1.0, 1.0)
val lines = helpGenGraphLinesVertical
(0, yClickPoints, lines, halfHeight, start, finish)
in in
Vector.concat lines Vector.concat lines
end end
else else
(* windowWidth < windowHeight *) (* windowWidth < windowHeight *)
let let
val difference = windowHeight - windowWidth (* todo; fix based on windowWidth > windowHeight *)
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)
in in
Vector.concat lines Vector.fromList []
end end
end end
local local
fun make (windowWidth, windowHeight, wStart, wFinish, hStart, hFinish) = fun make (windowWidth, windowHeight, wStart, wFinish, hStart, hFinish) =
let
val xClickPoints = genClickPoints (wStart, wFinish)
val yClickPoints = genClickPoints (hStart, hFinish)
in
{ triangles = [] { triangles = []
, triangleStage = NO_TRIANGLE , triangleStage = NO_TRIANGLE
, windowWidth = windowWidth , windowWidth = windowWidth
, windowHeight = windowHeight , windowHeight = windowHeight
, xClickPoints = genClickPoints (wStart, wFinish) , xClickPoints = xClickPoints
, yClickPoints = genClickPoints (hStart, hFinish) , yClickPoints = yClickPoints
, graphLines = genGraphLines (windowWidth, windowHeight) , graphLines = genGraphLines (windowWidth, windowHeight, xClickPoints, yClickPoints)
} }
end
in in
fun getInitial (windowWidth, windowHeight) = fun getInitial (windowWidth, windowHeight) =
if windowWidth = windowHeight then if windowWidth = windowHeight then
@@ -366,10 +359,12 @@ struct
, triangles , triangles
, triangleStage , triangleStage
} = app } = app
val xClickPoints = genClickPoints (wStart, wFinish)
val yClickPoints = genClickPoints (hStart, hFinish)
in in
{ xClickPoints = genClickPoints (wStart, wFinish) { xClickPoints = xClickPoints
, yClickPoints = genClickPoints (hStart, hFinish) , yClickPoints = yClickPoints
, graphLines = genGraphLines (windowWidth, windowHeight) , graphLines = genGraphLines (windowWidth, windowHeight, xClickPoints, yClickPoints)
, triangles = triangles , triangles = triangles
, triangleStage = triangleStage , triangleStage = triangleStage
, windowWidth = windowWidth , windowWidth = windowWidth

View File

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

View File

@@ -52,15 +52,23 @@ struct
graphDrawObject graphDrawObject
end 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 let
val {vertexBuffer, program, ...} = graphDrawObject val {vertexBuffer, program, ...} = graphDrawObject
val _ = Gles3.bindBuffer vertexBuffer val _ = Gles3.bindBuffer vertexBuffer
val _ = Gles3.vertexAttribPointer (0, 2, 2, 0) val _ = Gles3.vertexAttribPointer (0, 2, 2, 0)
val _ = Gles3.enableVertexAttribArray 0 val _ = Gles3.enableVertexAttribArray 0
val _ = Gles3.useProgram program val _ = Gles3.useProgram program
val _ = Gles3.drawArrays val _ = Gles3.drawArrays (Gles3.TRIANGLES (), 0, graphDrawLength)
(Gles3.TRIANGLES (), 0, Vector.length Constants.graphLines div 2)
in in
() ()
end end

View File

@@ -41,6 +41,8 @@ struct
\ FragColor = vec4(frag_col.x, frag_col.y, frag_col.z, 1.0f);\n\ \ 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 = val graphLines: Real32.real vector =
AppType.genGraphLines (windowWidth, windowHeight) Vector.fromList []
end end

View File

@@ -28,6 +28,7 @@ struct
( drawMailbox ( drawMailbox
, window , window
, graphDrawObject , graphDrawObject
, drawGraphLength
, buttonDrawObject , buttonDrawObject
, buttonDrawLength , buttonDrawLength
, triangleDrawObject , triangleDrawObject
@@ -40,7 +41,7 @@ struct
val _ = Gles3.clearColor (1.0, 1.0, 1.0, 1.0) val _ = Gles3.clearColor (1.0, 1.0, 1.0, 1.0)
val _ = Gles3.clear () val _ = Gles3.clear ()
val _ = AppDraw.drawGraphLines graphDrawObject val _ = AppDraw.drawGraphLines (graphDrawObject, drawGraphLength)
val _ = val _ =
AppDraw.drawTriangles (triangleDrawObject, triangleDrawLength) AppDraw.drawTriangles (triangleDrawObject, triangleDrawLength)
val _ = AppDraw.drawButton (buttonDrawObject, buttonDrawLength) val _ = AppDraw.drawButton (buttonDrawObject, buttonDrawLength)
@@ -52,6 +53,7 @@ struct
( drawMailbox ( drawMailbox
, window , window
, graphDrawObject , graphDrawObject
, drawGraphLength
, buttonDrawObject , buttonDrawObject
, buttonDrawLength , buttonDrawLength
, triangleDrawObject , triangleDrawObject
@@ -69,6 +71,7 @@ struct
( drawMailbox ( drawMailbox
, window , window
, graphDrawObject , graphDrawObject
, drawGraphLength
, buttonDrawObject , buttonDrawObject
, buttonDrawLength , buttonDrawLength
, triangleDrawObject , triangleDrawObject
@@ -87,6 +90,31 @@ struct
( drawMailbox ( drawMailbox
, window , window
, graphDrawObject , 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 , buttonDrawObject
, 0 , 0
, triangleDrawObject , triangleDrawObject
@@ -98,6 +126,7 @@ struct
( drawMailbox ( drawMailbox
, window , window
, graphDrawObject , graphDrawObject
, drawGraphLength
, buttonDrawObject , buttonDrawObject
, buttonDrawLength , buttonDrawLength
, triangleDrawObject , triangleDrawObject

View File

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

View File

@@ -3,6 +3,8 @@ sig
datatype t = datatype t =
DRAW_BUTTON of Real32.real vector DRAW_BUTTON of Real32.real vector
| DRAW_TRIANGLES_AND_RESET_BUTTONS 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 | NO_DRAW
end end
@@ -11,5 +13,7 @@ struct
datatype t = datatype t =
DRAW_BUTTON of Real32.real vector DRAW_BUTTON of Real32.real vector
| DRAW_TRIANGLES_AND_RESET_BUTTONS 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 | NO_DRAW
end end