Advertisement
Guest User

Untitled

a guest
Mar 24th, 2014
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 3.51 KB | None | 0 0
  1. package controllers
  2.  
  3. import scala.collection.mutable.ArrayBuffer
  4. import scala.collection.mutable.HashMap
  5.  
  6. object Geometry extends App {
  7.  
  8.   case class Vector(x: Double, y: Double) {
  9.  
  10.     def +(other: Vector) = Vector(this.x + other.x, this.y + other.y)
  11.     def -(other: Vector) = Vector(this.x - other.x, this.y - other.y)
  12.     def *(factor: Double) = Vector(this.y * factor, this.y * factor)
  13.  
  14.     def normalize = {
  15.       if (this.length == 0) this
  16.       else Vector(this.x / length, y / length)
  17.     }
  18.  
  19.     def o(other: Vector) = (x * other.x) + (y * other.y)
  20.  
  21.     val length = Math.sqrt(Math.pow(this.x, 2) + Math.pow(this.y, 2))
  22.     def negate = Vector(this.x, this.y) * -1
  23.  
  24.   }
  25.  
  26.   case class AABB(halfSize: Vector, center: Vector) {
  27.  
  28.     val min = center - halfSize
  29.     val max = center + halfSize
  30.  
  31.     def contains(vector: Vector): Boolean = {
  32.       (min.x <= vector.x && vector.x <= max.x) &&
  33.         (min.y <= vector.y && vector.y <= max.y)
  34.     }
  35.  
  36.     def intersects(other: AABB): Boolean = {
  37.       (min.x < other.max.x) && (max.x > other.min.x) &&
  38.         (min.y < other.max.y) && (max.y > other.min.y)
  39.     }
  40.   }
  41.  
  42.   case class Entity(id: String, boundingBox: AABB, position: Vector)
  43.  
  44.   type Coords = (Int, Int)
  45.  
  46.   case class Grid(val boundingBox: AABB, val cellSize: Double) {
  47.  
  48.     var cells = HashMap.empty[Coords, ArrayBuffer[Entity]]
  49.     val entities = HashMap.empty[String, ArrayBuffer[Coords]]
  50.  
  51.     val width = boundingBox.halfSize.x * 2
  52.     val height = boundingBox.halfSize.y * 2
  53.  
  54.     val rowCount = (width / cellSize).toInt
  55.     val colCount = (height / cellSize).toInt
  56.     val cellCount = rowCount * colCount
  57.     val center = boundingBox.center
  58.  
  59.     def put(entity: Entity): Option[Coords] = {
  60.       val position = entity.position
  61.  
  62.       if (!contains(entity)) None
  63.       else {
  64.         val coords = getCoordinates(position)
  65.  
  66.         cells.get(coords).map {
  67.           case entities => entities += entity
  68.         }.getOrElse {
  69.           cells += (coords -> ArrayBuffer(entity))
  70.         }
  71.  
  72.         Some(coords)
  73.       }
  74.     }
  75.  
  76.     def contains(entity: Entity): Boolean = boundingBox.contains(entity.position)
  77.  
  78.     def getEntites(coords: Coords): ArrayBuffer[Entity] = {
  79.       cells.get(coords).map {
  80.         case entities => entities
  81.       }.getOrElse { ArrayBuffer.empty[Entity] }
  82.     }
  83.  
  84.     def getCoordinates(vec: Vector): (Int, Int) = {
  85.       ((vec.x / cellSize).toInt, (vec.y / cellSize).toInt)
  86.     }
  87.  
  88.     def printGrid = {
  89.       for (y <- -colCount / 2 to colCount / 2) {
  90.         for (x <- -rowCount / 2 to rowCount / 2) {
  91.           print((x, y) + ": " )
  92.           print(cells.get((x, y)).map(_.size).getOrElse(0))
  93.           print("\t")
  94.         }
  95.         println("\t")
  96.       }
  97.     }
  98.  
  99.     override def toString = {
  100.       s"Grid{width: $width, height: $height, cellSize: $cellSize, cellCount: $cellCount, center: $center}"
  101.     }
  102.   }
  103.  
  104.   val boundingBox = AABB(halfSize = Vector(500, 500), center = Vector(0, 0))
  105.  
  106.   val g = Grid(boundingBox, cellSize = 500)
  107.   g.put(Entity("ADOLF", boundingBox, Vector(-500, 500)))
  108.     g.put(Entity("ADOLF", boundingBox, Vector(-250, 250)))
  109.   g.put(Entity("ADOLF", boundingBox, Vector(500, 500)))
  110.     g.put(Entity("ADOLF", boundingBox, Vector(250, 250)))
  111.   g.put(Entity("ADOLF", boundingBox, Vector(500, -500)))
  112.   g.put(Entity("ADOLF", boundingBox, Vector(-500, -500)))
  113.      g.put(Entity("ADOLF", boundingBox, Vector(-250, -250)))
  114.   g.put(Entity("ADOLF", boundingBox, Vector(0, 0)))
  115.  
  116.   println(g)
  117.  
  118.   g.cells.foreach(println)
  119.  
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement