code function to turn work-in-progress triangle (of triangle_stage of) into a square, so user can see where they have already clicked

This commit is contained in:
2024-07-31 20:58:33 +01:00
parent 087998ac6b
commit bd41e7d24d
3 changed files with 61 additions and 11 deletions

View File

@@ -1,8 +1,8 @@
signature APP_TYPE =
sig
datatype triangle_stage =
FIRST of {x1: Real32.real, y1: Real32.real}
| NO_TRIANGLE
NO_TRIANGLE
| FIRST of {x1: Real32.real, y1: Real32.real}
| SECOND of
{x1: Real32.real, x2: Real32.real, y1: Real32.real, y2: Real32.real}

View File

@@ -17,6 +17,16 @@ struct
end
end
fun ltrbToVertex (left, top, right, bottom, r, g, b) =
#[ left, bottom, r, g, b
, right, bottom, r, g, b
, left, top, r, g, b
, left, top, r, g, b
, right, bottom, r, g, b
, right, top, r, g, b
]
local
fun genClickPoints (windowWidth, windowHeight) =
let
@@ -50,15 +60,7 @@ struct
val bottom = (vpos - 5.0) / halfHeight
val top = (vpos + 5.0) / halfHeight
val drawVec =
#[ left, bottom, r, g, b
, right, bottom, r, g, b
, left, top, r, g, b
, left, top, r, g, b
, right, bottom, r, g, b
, right, top, r, g, b
]
val drawVec = ltrbToVertex (left, top, right, bottom, r, g, b)
val hpos = hpos / halfWidth
val vpos = vpos / halfHeight
@@ -91,6 +93,54 @@ struct
(0, Real32.fromInt mouseX, Real32.fromInt mouseY, r, g, b)
end
fun getTriangleStageVector (model: app_type, drawVec) =
case #triangleStage model of
NO_TRIANGLE => drawVec
| FIRST {x1, y1} =>
let
val halfWidth = Real32.fromInt (Constants.windowWidth div 2)
val halfHeight = Real32.fromInt (Constants.windowHeight div 2)
val x1px = x1 * halfWidth
val left = (x1px - 5.0) / halfWidth
val right = (x1px + 5.0) / halfWidth
val y1px = y1 * halfHeight
val top = (y1px + 5.0) / halfHeight
val bottom = (y1px - 5.0) / halfHeight
val firstVec = ltrbToVertex (left, top, right, bottom, 0.0, 0.0, 1.0)
in
Vector.concat [firstVec, drawVec]
end
| SECOND {x1, y1, x2, y2} =>
let
val halfWidth = Real32.fromInt (Constants.windowWidth div 2)
val halfHeight = Real32.fromInt (Constants.windowHeight div 2)
val x1px = x1 * halfWidth
val left = (x1px - 5.0) / halfWidth
val right = (x1px + 5.0) / halfWidth
val y1px = y1 * halfHeight
val top = (y1px + 5.0) / halfHeight
val bottom = (y1px - 5.0) / halfHeight
val firstVec = ltrbToVertex (left, top, right, bottom, 0.0, 0.0, 1.0)
val x2px = x2 * halfWidth
val left = (x2px - 5.0) / halfWidth
val right = (x2px + 5.0) / halfWidth
val y2px = y2 * halfHeight
val top = (y2px + 5.0) / halfHeight
val bottom = (y2px - 5.0) / halfHeight
val secVec = ltrbToVertex (left, top, right, bottom, 0.0, 0.0, 1.0)
in
Vector.concat [firstVec, secVec, drawVec]
end
fun update (model, mouseX, mouseY, inputMsg) =
let
open DrawMessage