Scoop API

scoop:scoop.ui:0.1.0

src/interaction/hover_geometry.scoop

Selected production source · src/interaction/hover_geometry.scoop

1package scoop.ui
3import scoop.graphics.*
5/** A bounded triangle from the departure point to the destination's facing edge. */
6internal fun hoverTransit(
7 start: Point,
8 previous: Point,
9 point: Point,
10 bounds: Rect,
11 padding: Double
12): Boolean {
13 val target = Point(bounds.x + bounds.width / 2.0, bounds.y + bounds.height / 2.0)
14 val dx = target.x - start.x
15 val dy = target.y - start.y
16 val px = point.x - previous.x
17 val py = point.y - previous.y
18 if (px * dx + py * dy < 0.0)
19 return false
20 val horizontal = absHover(dx) >= absHover(dy)
21 val a =
22 if (horizontal)
23 Point(
24 if (dx >= 0.0)
25 bounds.x - padding
26 else
27 bounds.x + bounds.width + padding,
28 bounds.y - padding
29 )
30 else
31 Point(
32 bounds.x - padding,
33 if (dy >= 0.0)
34 bounds.y - padding
35 else
36 bounds.y + bounds.height + padding
37 )
38 val b =
39 if (horizontal)
40 Point(a.x, bounds.y + bounds.height + padding)
41 else
42 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 true
48 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 false
53 return (one >= 0.0 && two >= 0.0 && three >= 0.0) || (one <= 0.0 && two <= 0.0 && three <= 0.0)
56private 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)
59private fun absHover(value: Double): Double =
60 if (value < 0.0)
61 -value
62 else
63 value