1 /*
2 * Copyright 2023 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 @file:Suppress("NOTHING_TO_INLINE")
18
19 package androidx.graphics.shapes
20
21 import androidx.collection.FloatFloatPair
22 import kotlin.math.sqrt
23
24 internal typealias Point = FloatFloatPair
25
26 internal val Point.x
27 get() = first
28
29 internal val Point.y
30 get() = second
31
copynull32 internal fun Point.copy(x: Float = first, y: Float = second) = Point(x, y)
33
34 /**
35 * The magnitude of the Point, which is the distance of this point from (0, 0).
36 *
37 * If you need this value to compare it to another [Point]'s distance, consider using
38 * [getDistanceSquared] instead, since it is cheaper to compute.
39 */
40 internal fun Point.getDistance() = sqrt(x * x + y * y)
41
42 /**
43 * The square of the magnitude (which is the distance of this point from (0, 0)) of the Point.
44 *
45 * This is cheaper than computing the [getDistance] itself.
46 */
47 internal fun Point.getDistanceSquared() = x * x + y * y
48
49 internal fun Point.dotProduct(other: Point) = x * other.x + y * other.y
50
51 internal fun Point.dotProduct(otherX: Float, otherY: Float) = x * otherX + y * otherY
52
53 /**
54 * Compute the Z coordinate of the cross product of two vectors, to check if the second vector is
55 * going clockwise ( > 0 ) or counterclockwise (< 0) compared with the first one. It could also be
56 * 0, if the vectors are co-linear.
57 */
58 internal fun Point.clockwise(other: Point) = x * other.y - y * other.x > 0
59
60 /** Returns unit vector representing the direction to this point from (0, 0) */
61 internal fun Point.getDirection() = run {
62 val d = this.getDistance()
63 require(d > 0f) { "Can't get the direction of a 0-length vector" }
64 this / d
65 }
66
67 /**
68 * Unary negation operator.
69 *
70 * Returns a Point with the coordinates negated.
71 *
72 * If the [Point] represents an arrow on a plane, this operator returns the same arrow but pointing
73 * in the reverse direction.
74 */
unaryMinusnull75 internal operator fun Point.unaryMinus(): Point = Point(-x, -y)
76
77 /**
78 * Binary subtraction operator.
79 *
80 * Returns a Point whose [x] value is the left-hand-side operand's [x] minus the right-hand-side
81 * operand's [x] and whose [y] value is the left-hand-side operand's [y] minus the right-hand-side
82 * operand's [y].
83 */
84 internal operator fun Point.minus(other: Point): Point = Point(x - other.x, y - other.y)
85
86 /**
87 * Binary addition operator.
88 *
89 * Returns a Point whose [x] value is the sum of the [x] values of the two operands, and whose [y]
90 * value is the sum of the [y] values of the two operands.
91 */
92 internal operator fun Point.plus(other: Point): Point = Point(x + other.x, y + other.y)
93
94 /**
95 * Multiplication operator.
96 *
97 * Returns a Point whose coordinates are the coordinates of the left-hand-side operand (a Point)
98 * multiplied by the scalar right-hand-side operand (a Float).
99 */
100 internal operator fun Point.times(operand: Float): Point = Point(x * operand, y * operand)
101
102 /**
103 * Division operator.
104 *
105 * Returns a Point whose coordinates are the coordinates of the left-hand-side operand (a Point)
106 * divided by the scalar right-hand-side operand (a Float).
107 */
108 internal operator fun Point.div(operand: Float): Point = Point(x / operand, y / operand)
109
110 /**
111 * Modulo (remainder) operator.
112 *
113 * Returns a Point whose coordinates are the remainder of dividing the coordinates of the
114 * left-hand-side operand (a Point) by the scalar right-hand-side operand (a Float).
115 */
116 internal operator fun Point.rem(operand: Float) = Point(x % operand, y % operand)
117
118 /**
119 * Linearly interpolate between two Points.
120 *
121 * The [fraction] argument represents position on the timeline, with 0.0 meaning that the
122 * interpolation has not started, returning [start] (or something equivalent to [start]), 1.0
123 * meaning that the interpolation has finished, returning [stop] (or something equivalent to
124 * [stop]), and values in between meaning that the interpolation is at the relevant point on the
125 * timeline between [start] and [stop]. The interpolation can be extrapolated beyond 0.0 and 1.0, so
126 * negative values and values greater than 1.0 are valid (and can easily be generated by curves).
127 *
128 * Values for [fraction] are usually obtained from an [Animation<Float>], such as an
129 * `AnimationController`.
130 */
131 internal fun interpolate(start: Point, stop: Point, fraction: Float): Point {
132 return Point(interpolate(start.x, stop.x, fraction), interpolate(start.y, stop.y, fraction))
133 }
134
transformednull135 internal fun Point.transformed(f: PointTransformer): Point {
136 val result = f.transform(x, y)
137 return Point(result.first, result.second)
138 }
139