diff --git a/dotscape b/dotscape index 206c8ae..d9c1b70 100755 Binary files a/dotscape and b/dotscape differ diff --git a/functional-core/app/app-update.sml b/functional-core/app/app-update.sml index 1e44e1c..0531658 100644 --- a/functional-core/app/app-update.sml +++ b/functional-core/app/app-update.sml @@ -18,7 +18,7 @@ struct val drawVec = case ClickPoints.getClickPositionFromMouse model of SOME (xpos, ypos) => - ClickPoints.getDrawVec (xpos, ypos, 1.0, 0.0, 0.0, model) + ClickPoints.getDrawDot (xpos, ypos, 1.0, 0.0, 0.0, model) | NONE => Vector.fromList [] val drawMsg = DRAW_DOT drawVec in @@ -29,14 +29,16 @@ struct case ClickPoints.getClickPositionFromMouse model of SOME (xpos, ypos) => let - val dotVec = ClickPoints.getDrawVec (xpos, ypos, 0.0, 0.0, 1.0, model) + val dotVec = ClickPoints.getDrawDot (xpos, ypos, 0.0, 0.0, 1.0, model) + val {windowWidth, windowHeight, ...} = model val halfWidth = Real32.fromInt (windowWidth div 2) val halfHeight = Real32.fromInt (windowHeight div 2) val hpos = - Ndc.centreAlignX (xpos, windowWidth, windowHeight, halfWidth) + ClickPoints.xposToNdc (xpos, windowWidth, windowHeight, halfWidth) val vpos = - Ndc.centreAlignY (ypos, windowWidth, windowHeight, halfHeight) + ClickPoints.yposToNdc (ypos, windowWidth, windowHeight, halfHeight) + val newUndoTuple = (hpos, vpos) in (case #triangleStage model of @@ -70,7 +72,6 @@ struct let val model = AppWith.addTriangle (model, x1, y1, x2, y2, hpos, vpos, newUndoTuple) - val drawVec = Triangles.toVector model val drawMsg = DRAW_TRIANGLES_AND_RESET_DOTS drawVec in diff --git a/functional-core/app/click-points.sml b/functional-core/app/click-points.sml index 4eb7f86..924ae13 100644 --- a/functional-core/app/click-points.sml +++ b/functional-core/app/click-points.sml @@ -3,7 +3,7 @@ sig val generate: int * int -> Real32.real vector val getClickPositionFromMouse: AppType.app_type -> (Real32.real * Real32.real) option - val getDrawVec: + val getDrawDot: Real32.real * Real32.real * Real32.real @@ -11,6 +11,10 @@ sig * Real32.real * AppType.app_type -> Real32.real vector + + (* two below functions convert pixel coordinates to normalised device coordinates *) + val xposToNdc: Real32.real * int * int * Real32.real -> Real32.real + val yposToNdc: Real32.real * int * int * Real32.real -> Real32.real end structure ClickPoints :> CLICK_POINTS = @@ -53,7 +57,7 @@ struct | NONE => NONE) | NONE => NONE - fun getDrawVec (xpos, ypos, r, g, b, app: AppType.app_type) = + fun getDrawDot (xpos, ypos, r, g, b, app: AppType.app_type) = let val {windowWidth, windowHeight, ...} = app @@ -71,4 +75,37 @@ struct in Ndc.ltrbToVertex (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 + val difference = windowWidth - windowHeight + val offset = Real32.fromInt (difference div 2) + in + xpos / (halfWidth - offset) + end + else + xpos / halfWidth + + end + + fun yposToNdc (ypos, windowWidth, windowHeight, halfHeight) = + let + val ypos = ~(ypos - halfHeight) + in + if windowHeight > windowWidth then + let + val difference = windowHeight - windowWidth + val offset = Real32.fromInt (difference div 2) + in + ypos / (halfHeight - offset) + end + else + ypos / halfHeight + end end