Guest User

Untitled

a guest
Dec 7th, 2022
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 0.86 KB | None | 0 0
  1. interface Point2D {
  2.     val x: Double
  3.     val y: Double
  4.  
  5.     fun distanceFromOrigin(): Double = sqrt(x * y)
  6. }
  7.  
  8. interface Point3D : Point2D {
  9.     override val x: Double
  10.     override val y: Double
  11.     val z: Double
  12.  
  13.     override fun distanceFromOrigin(): Double = sqrt(x * y * z)
  14. }
  15.  
  16. // 2D sort in most positive direction first by x, then by y
  17. fun comparePositiveward(pA: Point2D, pB: Point2D): Int {
  18.     return when {
  19.         pA.x > pB.x -> 1
  20.         pA.x < pB.x -> -1
  21.         pA.y > pB.y -> 1
  22.         pA.y < pB.y -> -1
  23.         else        -> 0
  24.     }
  25. }
  26.  
  27. // Any-D sort by distance from zero
  28. fun compareDistance(pA: Point2D, pB: Point2D): Int {
  29.     val aDist: Double = pA.distanceFromOrigin()
  30.     val bDist: Double = pB.distanceFromOrigin()
  31.     return when {
  32.         aDist > bDist -> 1
  33.         aDist < bDist -> -1
  34.         else          -> 0
  35.     }
  36. }
  37.  
Advertisement
Add Comment
Please, Sign In to add comment