16
16
package curves
17
17
18
18
import java.util.*
19
+ import kotlin.math.cos
20
+ import kotlin.math.hypot
21
+ import kotlin.math.sin
19
22
20
23
/* *
21
24
* This provides a curve fit system that stitches the x,y path together with
@@ -29,7 +32,9 @@ class ArcSpline(arcModes: IntArray, val timePoints: FloatArray, y: List<FloatArr
29
32
mArcs = arrayOfNulls(timePoints.size - 1 )
30
33
var mode = START_VERTICAL
31
34
var last = START_VERTICAL
32
- for (i in mArcs.indices) {
35
+
36
+ mArcs = Array (timePoints.size - 1 ) {it->
37
+ val i = it
33
38
when (arcModes[i]) {
34
39
ARC_START_VERTICAL -> {
35
40
mode = START_VERTICAL
@@ -50,7 +55,7 @@ class ArcSpline(arcModes: IntArray, val timePoints: FloatArray, y: List<FloatArr
50
55
ARC_ABOVE -> mode = UP_ARC
51
56
ARC_BELOW -> mode = DOWN_ARC
52
57
}
53
- mArcs[i] = Arc (mode, timePoints[i], timePoints[i + 1 ], y[i][0 ], y[i][1 ], y[i + 1 ][0 ], y[i + 1 ][1 ])
58
+ Arc (mode, timePoints[i], timePoints[i + 1 ], y[i][0 ], y[i][1 ], y[i + 1 ][0 ], y[i + 1 ][1 ])
54
59
}
55
60
}
56
61
@@ -258,7 +263,7 @@ class ArcSpline(arcModes: IntArray, val timePoints: FloatArray, y: List<FloatArr
258
263
mX2 = x2
259
264
mY1 = y1
260
265
mY2 = y2
261
- mArcDistance = Math . hypot(dy.toDouble() , dx.toDouble() ).toFloat()
266
+ mArcDistance = hypot(dy, dx).toFloat()
262
267
mArcVelocity = mArcDistance * mOneOverDeltaTime
263
268
mEllipseCenterX = dx / (mTime2 - mTime1) // cache the slope in the unused center
264
269
mEllipseCenterY = dy / (mTime2 - mTime1) // cache the slope in the unused center
@@ -279,8 +284,8 @@ class ArcSpline(arcModes: IntArray, val timePoints: FloatArray, y: List<FloatArr
279
284
fun setPoint (time : Float ) {
280
285
val percent = (if (mVertical) mTime2 - time else time - mTime1) * mOneOverDeltaTime
281
286
val angle = Math .PI .toFloat() * 0.5f * lookup(percent)
282
- mTmpSinAngle = Math . sin(angle.toDouble()).toFloat( )
283
- mTmpCosAngle = Math . cos(angle.toDouble()).toFloat( )
287
+ mTmpSinAngle = sin(angle)
288
+ mTmpCosAngle = cos(angle)
284
289
}
285
290
286
291
fun calcX (): Float {
@@ -294,14 +299,14 @@ class ArcSpline(arcModes: IntArray, val timePoints: FloatArray, y: List<FloatArr
294
299
fun calcDX (): Float {
295
300
val vx = mEllipseA * mTmpCosAngle
296
301
val vy = - mEllipseB * mTmpSinAngle
297
- val norm = mArcVelocity / Math . hypot(vx.toDouble() , vy.toDouble()).toFloat( )
302
+ val norm = mArcVelocity / hypot(vx, vy)
298
303
return if (mVertical) - vx * norm else vx * norm
299
304
}
300
305
301
306
fun calcDY (): Float {
302
307
val vx = mEllipseA * mTmpCosAngle
303
308
val vy = - mEllipseB * mTmpSinAngle
304
- val norm = mArcVelocity / Math . hypot(vx.toDouble() , vy.toDouble()).toFloat( )
309
+ val norm = mArcVelocity / hypot(vx, vy)
305
310
return if (mVertical) - vy * norm else vy * norm
306
311
}
307
312
@@ -346,12 +351,12 @@ class ArcSpline(arcModes: IntArray, val timePoints: FloatArray, y: List<FloatArr
346
351
var dist = 0f
347
352
for (i in sOurPercent.indices) {
348
353
val angle = Math .toRadians(90.0 * i / (sOurPercent.size - 1 )).toFloat()
349
- val s = Math . sin(angle.toDouble()).toFloat( )
350
- val c = Math . cos(angle.toDouble()).toFloat( )
354
+ val s = sin(angle)
355
+ val c = cos(angle)
351
356
val px = a * s
352
357
val py = b * c
353
358
if (i > 0 ) {
354
- dist + = Math . hypot((px - lx).toDouble() , (py - ly).toDouble( )).toFloat()
359
+ dist + = hypot((px - lx), (py - ly)).toFloat()
355
360
sOurPercent[i] = dist
356
361
}
357
362
lx = px
0 commit comments