scoop:scoop.ui:0.1.0
src/interaction/hover_geometry.scoop
Selected production source · src/interaction/hover_geometry.scoop
1
package scoop.ui3
import scoop.graphics.*5
/** A bounded triangle from the departure point to the destination's facing edge. */6
internal fun hoverTransit(7
start: Point,8
previous: Point,9
point: Point,10
bounds: Rect,11
padding: Double12
): Boolean {13
val target = Point(bounds.x + bounds.width / 2.0, bounds.y + bounds.height / 2.0)14
val dx = target.x - start.x15
val dy = target.y - start.y16
val px = point.x - previous.x17
val py = point.y - previous.y18
if (px * dx + py * dy < 0.0)19
return false20
val horizontal = absHover(dx) >= absHover(dy)21
val a =22
if (horizontal)23
Point(24
if (dx >= 0.0)25
bounds.x - padding26
else27
bounds.x + bounds.width + padding,28
bounds.y - padding29
)30
else31
Point(32
bounds.x - padding,33
if (dy >= 0.0)34
bounds.y - padding35
else36
bounds.y + bounds.height + padding37
)38
val b =39
if (horizontal)40
Point(a.x, bounds.y + bounds.height + padding)41
else42
Point(bounds.x + bounds.width + padding, a.y)43
if (44
(point.x >= bounds.x - padding && point.x <= bounds.x + bounds.width + padding) &&45
(point.y >= bounds.y - padding && point.y <= bounds.y + bounds.height + padding)46
)47
return true48
val one = hoverCross(start, a, point)49
val two = hoverCross(a, b, point)50
val three = hoverCross(b, start, point)51
if (!one.isFinite() || !two.isFinite() || !three.isFinite())52
return false53
return (one >= 0.0 && two >= 0.0 && three >= 0.0) || (one <= 0.0 && two <= 0.0 && three <= 0.0)54
}56
private fun hoverCross(a: Point, b: Point, point: Point): Double =57
(b.x - a.x) * (point.y - a.y) - (b.y - a.y) * (point.x - a.x)59
private fun absHover(value: Double): Double =60
if (value < 0.0)61
-value62
else63
value