Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- interface Point2D {
- val x: Double
- val y: Double
- fun distanceFromOrigin(): Double = sqrt(x * y)
- }
- interface Point3D : Point2D {
- override val x: Double
- override val y: Double
- val z: Double
- override fun distanceFromOrigin(): Double = sqrt(x * y * z)
- }
- // 2D sort in most positive direction first by x, then by y
- fun comparePositiveward(pA: Point2D, pB: Point2D): Int {
- return when {
- pA.x > pB.x -> 1
- pA.x < pB.x -> -1
- pA.y > pB.y -> 1
- pA.y < pB.y -> -1
- else -> 0
- }
- }
- // Any-D sort by distance from zero
- fun compareDistance(pA: Point2D, pB: Point2D): Int {
- val aDist: Double = pA.distanceFromOrigin()
- val bDist: Double = pB.distanceFromOrigin()
- return when {
- aDist > bDist -> 1
- aDist < bDist -> -1
- else -> 0
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment