Forwarded from Deleted Account
需要依据某一点 P 和一个 rotation、distance 求出 P' 的 (x, y) 轴距离,忘记三角函数怎么用了来着(绝望)
手头也么得纸
手头也么得纸
Forwarded from Deleted Account
KochHill.kt
2.1 KB
import kotlin.math.sin
import kotlin.math.cos
import kotlin.math.PI
protected fun forward(distance: Double) {
val pA = position
val distanceAB = distance.distanceDegree(rotation)
val pB = pA + distanceAB
canvas.drawLine(pA.x, pA.y, pB.x, pB.y)
position.assign(pB)
}
private fun Double.distanceDegree(rotation: Deg): Point2 {
val d = this; val r = rotation.coerceRad()
return Point( (d*cos(r)).toInt(), (d*sin(r)).toInt() )
} protected var rotation: Deg get() = rawRotation*FULL_DEG
set(d) { rawRotation = d/FULL_DEG }
protected fun rotate(deg: Double) { rotation += deg }
protected fun forward(distance: Double) {
val pA = position
val distanceAB = distance.lineDegreed(rotation)
val pB = pA + distanceAB
canvas.drawLine(pA.x, pA.y, pB.x, pB.y)
position.assign(pB)
}
private fun Double.lineDegreed(rotation: Deg): Point2 {
val d = this; val r = rotation.coerceRad()
return Point( (d*cos(r)).toInt(), (d*sin(r)).toInt() )
}
/** Coerce to radix from degree */
fun Deg.coerceRad() = this / 180.0 * PI