change graph lines to draw dots at clickable points rather than lines which are similar to graph paper
This commit is contained in:
@@ -15,10 +15,12 @@ struct
|
||||
|
||||
fun getDotVecFromIndices (model, hIdx, vIdx) =
|
||||
let
|
||||
val {windowWidth, windowHeight, ...} = model
|
||||
val xpos = Vector.sub (#xClickPoints model, hIdx)
|
||||
val ypos = Vector.sub (#yClickPoints model, vIdx)
|
||||
in
|
||||
ClickPoints.getDrawDot (xpos, ypos, 1.0, 0.0, 0.0, model)
|
||||
ClickPoints.getDrawDotRgb
|
||||
(xpos, ypos, 1.0, 0.0, 0.0, windowWidth, windowHeight)
|
||||
end
|
||||
|
||||
fun mouseMoveOrRelease (model: app_type) =
|
||||
@@ -133,7 +135,8 @@ struct
|
||||
|
||||
val xpos = Vector.sub (xClickPoints, hIdx)
|
||||
val ypos = Vector.sub (yClickPoints, vIdx)
|
||||
val dotVec = ClickPoints.getDrawDot (xpos, ypos, 0.0, 0.0, 1.0, model)
|
||||
val dotVec = ClickPoints.getDrawDotRgb
|
||||
(xpos, ypos, 0.0, 0.0, 1.0, windowWidth, windowHeight)
|
||||
|
||||
val halfWidth = Real32.fromInt (windowWidth div 2)
|
||||
val halfHeight = Real32.fromInt (windowHeight div 2)
|
||||
|
||||
@@ -2,13 +2,17 @@ signature CLICK_POINTS =
|
||||
sig
|
||||
val generate: int * int * int -> Real32.real vector
|
||||
val getClickPositionFromMouse: AppType.app_type -> (int * int) option
|
||||
val getDrawDot:
|
||||
|
||||
val getDrawDot: Real32.real * Real32.real * int * int -> Real32.real vector
|
||||
|
||||
val getDrawDotRgb:
|
||||
Real32.real
|
||||
* Real32.real
|
||||
* Real32.real
|
||||
* Real32.real
|
||||
* Real32.real
|
||||
* AppType.app_type
|
||||
* int
|
||||
* int
|
||||
-> Real32.real vector
|
||||
|
||||
(* two below functions convert pixel coordinates to normalised device coordinates *)
|
||||
@@ -57,10 +61,8 @@ struct
|
||||
| NONE => NONE)
|
||||
| NONE => NONE
|
||||
|
||||
fun getDrawDot (xpos, ypos, r, g, b, app: AppType.app_type) =
|
||||
fun getDrawDot (xpos, ypos, windowWidth, windowHeight) =
|
||||
let
|
||||
val {windowWidth, windowHeight, ...} = app
|
||||
|
||||
(* calculate normalised device coordinates *)
|
||||
val halfWidth = Real32.fromInt (windowWidth div 2)
|
||||
val halfHeight = Real32.fromInt (windowHeight div 2)
|
||||
@@ -73,14 +75,29 @@ struct
|
||||
val bottom = (vpos - 5.0) / halfHeight
|
||||
val top = (vpos + 5.0) / halfHeight
|
||||
in
|
||||
Ndc.ltrbToVertex (left, top, right, bottom, r, g, b)
|
||||
Ndc.ltrbToVertex (left, top, right, bottom)
|
||||
end
|
||||
|
||||
fun getDrawDotRgb (xpos, ypos, r, g, b, windowWidth, windowHeight) =
|
||||
let
|
||||
(* calculate normalised device coordinates *)
|
||||
val halfWidth = Real32.fromInt (windowWidth div 2)
|
||||
val halfHeight = Real32.fromInt (windowHeight div 2)
|
||||
val hpos = xpos - halfWidth
|
||||
val vpos = ~(ypos - halfHeight)
|
||||
|
||||
(* coordinates to form small box around clicked area *)
|
||||
val left = (hpos - 5.0) / halfWidth
|
||||
val right = (hpos + 5.0) / halfWidth
|
||||
val bottom = (vpos - 5.0) / halfHeight
|
||||
val top = (vpos + 5.0) / halfHeight
|
||||
in
|
||||
Ndc.ltrbToVertexRgb (left, top, right, bottom, r, g, b)
|
||||
end
|
||||
|
||||
fun xposToNdc (xpos, windowWidth, windowHeight, halfWidth) =
|
||||
|
||||
let
|
||||
val xpos = xpos - halfWidth
|
||||
|
||||
in
|
||||
if windowWidth > windowHeight then
|
||||
let
|
||||
@@ -91,7 +108,6 @@ struct
|
||||
end
|
||||
else
|
||||
xpos / halfWidth
|
||||
|
||||
end
|
||||
|
||||
fun yposToNdc (ypos, windowWidth, windowHeight, halfHeight) =
|
||||
|
||||
@@ -5,185 +5,17 @@ end
|
||||
|
||||
structure GraphLines :> GRAPH_LINES =
|
||||
struct
|
||||
(*
|
||||
* 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 =
|
||||
#[ (* 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 helpGenGraphLinesHorizontal
|
||||
(pos, xClickPoints, acc, halfWidth, yMin, yMax) =
|
||||
if pos = Vector.length xClickPoints then
|
||||
acc
|
||||
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) *)
|
||||
#[
|
||||
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) *)
|
||||
#[
|
||||
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) *)
|
||||
#[
|
||||
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) *)
|
||||
#[
|
||||
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
|
||||
|
||||
fun helpGenerate (windowWidth, windowHeight, xClickPoints, yClickPoints) =
|
||||
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 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
|
||||
val {windowWidth, windowHeight, xClickPoints, yClickPoints, ...} = app
|
||||
in
|
||||
helpGenerate (windowWidth, windowHeight, xClickPoints, yClickPoints)
|
||||
Vector.concat (List.tabulate (Vector.length xClickPoints, fn xIdx =>
|
||||
let
|
||||
val xpos = Vector.sub (xClickPoints, xIdx)
|
||||
in
|
||||
Vector.concat (List.tabulate (Vector.length yClickPoints, fn yIdx =>
|
||||
ClickPoints.getDrawDot
|
||||
(xpos, Vector.sub (yClickPoints, yIdx), windowWidth, windowHeight)))
|
||||
end))
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,8 +1,17 @@
|
||||
structure Ndc =
|
||||
struct
|
||||
(* ndc = normalised device coordinates *)
|
||||
fun ltrbToVertex (left, top, right, bottom) =
|
||||
#[ left, bottom
|
||||
, right, bottom
|
||||
, left, top
|
||||
|
||||
fun ltrbToVertex (left, top, right, bottom, r, g, b) =
|
||||
, left, top
|
||||
, right, bottom
|
||||
, right, top
|
||||
]
|
||||
|
||||
fun ltrbToVertexRgb (left, top, right, bottom, r, g, b) =
|
||||
#[ left, bottom, r, g, b
|
||||
, right, bottom, r, g, b
|
||||
, left, top, r, g, b
|
||||
|
||||
@@ -18,7 +18,8 @@ struct
|
||||
val top = (y1px + 5.0) / halfHeight
|
||||
val bottom = (y1px - 5.0) / halfHeight
|
||||
|
||||
val firstVec = Ndc.ltrbToVertex (left, top, right, bottom, 0.0, 0.0, 1.0)
|
||||
val firstVec = Ndc.ltrbToVertexRgb
|
||||
(left, top, right, bottom, 0.0, 0.0, 1.0)
|
||||
in
|
||||
Vector.concat [firstVec, drawVec]
|
||||
end
|
||||
@@ -39,7 +40,8 @@ struct
|
||||
val top = (y1px + 5.0) / halfHeight
|
||||
val bottom = (y1px - 5.0) / halfHeight
|
||||
|
||||
val firstVec = Ndc.ltrbToVertex (left, top, right, bottom, 0.0, 0.0, 1.0)
|
||||
val firstVec = Ndc.ltrbToVertexRgb
|
||||
(left, top, right, bottom, 0.0, 0.0, 1.0)
|
||||
|
||||
val x2px = Ndc.centreAlignX (x2, windowWidth, windowHeight, halfWidth)
|
||||
val left = (x2px - 5.0) / halfWidth
|
||||
@@ -49,7 +51,7 @@ struct
|
||||
val top = (y2px + 5.0) / halfHeight
|
||||
val bottom = (y2px - 5.0) / halfHeight
|
||||
|
||||
val secVec = Ndc.ltrbToVertex (left, top, right, bottom, 0.0, 0.0, 1.0)
|
||||
val secVec = Ndc.ltrbToVertexRgb (left, top, right, bottom, 0.0, 0.0, 1.0)
|
||||
in
|
||||
Vector.concat [firstVec, secVec, drawVec]
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user