Advertisement
Guest User

Untitled

a guest
Jan 21st, 2020
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 2.49 KB | None | 0 0
  1.   private def calculateConvexHull(bee: Bee): Double = {
  2.     var results: Seq[(Int, Int)] = Seq.empty
  3.  
  4.     def findSide(point: (Int, Int), first: (Int, Int), second: (Int, Int)) =
  5.       math.signum((point._2 - first._2) * (second._1 - first._1) -
  6.         (second._2 - first._2) * (point._1 - first._1))
  7.  
  8.     def lineDist(point: (Int, Int), first: (Int, Int), second: (Int, Int)) =
  9.       math.abs((point._2 - first._2) * (second._1 - first._1) -
  10.         (second._1 - first._1) * (point._1 - first._1))
  11.  
  12.     def calculate(positions: Seq[(Int, Int)], first: (Int, Int), second: (Int, Int), side: Int, level: Int): Unit = {
  13.       println(level)
  14.       println("_____FIRST_____")
  15.       println(first)
  16.       println("____SECOND____")
  17.       println(second)
  18.       val (newPos, maxDist) = positions.zipWithIndex.foldLeft(((Int.MinValue, Int.MinValue), Int.MinValue)) {
  19.         case ((positionAccumulated, maxDist), (pos, i)) =>
  20.           val dist = lineDist(first, second, pos)
  21.           if (findSide(first, second, pos) == side && dist > maxDist) {
  22.             (pos, dist)
  23.           } else (positionAccumulated, maxDist)
  24.       }
  25.       // finding the point with maximum distance
  26.       // from L and also on the specified side of L.
  27.  
  28.       // If no point is found, add the end points
  29.       // of L to the convex hull.
  30.       if (maxDist == Int.MinValue) {
  31.         results +:= first
  32.         results +:= second
  33.         println(results)
  34.         ()
  35.       } else {
  36.         calculate(positions, first, newPos, -findSide(newPos, first, second), level + 1)
  37.         calculate(positions, newPos, second, -findSide(newPos, second, first), level + 1)
  38.       }
  39.  
  40.       // Recur for the two parts divided by a[ind]
  41.     }
  42.  
  43.     val positions = beesPositions(bee.id)
  44.     println(positions.length)
  45.     val (min, max) = positions.foldLeft((Int.MaxValue, Int.MaxValue), (Int.MinValue, Int.MinValue)) {
  46.       case ((minXPos, maxXPos), (posX, posY)) =>
  47.         val newMin = if (posX < minXPos._1) (posX, posY) else minXPos
  48.         val newMax = if (posX > maxXPos._1) (posX, posY) else maxXPos
  49.         (newMin, newMax)
  50.     }
  51.  
  52.     // Recursively find convex hull points on
  53.     // one side of line joining a[min_x] and
  54.     // a[max_x]
  55.     calculate(positions, min, max, 1, 0)
  56.     // other side of line joining a[min_x] and
  57.     calculate(positions, min, max, -1, 0)
  58.  
  59.     val points = (positions.zip(positions.tail)) :+ (positions.last, positions.head)
  60.  
  61.     points.map { case (a, b) => a._1 * b._2 - a._2 * b._1 }.sum / 2.0
  62.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement