Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@gzoritchak
Last active June 7, 2016 22:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gzoritchak/d76abc07b0eeae66b831495bdf8bf539 to your computer and use it in GitHub Desktop.
Save gzoritchak/d76abc07b0eeae66b831495bdf8bf539 to your computer and use it in GitHub Desktop.
package org.gzk.marslander
data class Time(val sec:Double)
val Double.sec: Time
get() = Time(this)
/**
* Speed is internally using a vector, allowing to compose it. speed12 = speed1 + speed2
*/
data class Speed(val direction: Vector) {
infix operator fun plus(speed: Speed) = Speed(direction + speed.direction)
val xSpeed:Double
get() = direction.dx
val ySpeed:Double
get() = direction.dy
override fun toString() = "(${xSpeed.format(2)}, ${ySpeed.format(2)})"
}
fun speed(xSpeed: Double, ySpeed: Double) = Speed(Vector(xSpeed, ySpeed))
/**
* Acceleration is internally using a vector, allowing to compose it. acc12 = acc1 + acc2
* Acceleration * Time -> Speed
*/
data class Acceleration(val vector: Vector) {
infix operator fun times( time: Time) = Speed(vector * time.sec)
infix operator fun plus(acceleration: Acceleration) = Acceleration(vector + acceleration.vector)
}
fun acceleration(xAcc:Double, yAcc:Double) = Acceleration(Vector(xAcc, yAcc))
/**
* Represent something with an initial point and speed and on which an acceleration can be applied.
*/
data class Particule(val position: Point, val speed: Speed){
fun accelerate(acceleration: Acceleration, time: Time): Particule {
val newSpeed = speed + acceleration * time
val newPosition = position +
speed.direction * time.sec +
acceleration.vector * time.sec * time.sec * 0.5
return Particule(newPosition, newSpeed)
}
override fun toString() = " x=${position.x.format(2)} y=${position.y.format(2)} speed= $speed"
}
fun particule(x:Double, y:Double, s: Speed) = Particule(Point(x, y), s)
fun Double.format(digits: Int) = java.lang.String.format("%.${digits}f", this)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment