Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package controllers
- import scala.collection.mutable.ArrayBuffer
- import scala.collection.mutable.HashMap
- object Geometry extends App {
- case class Vector(x: Double, y: Double) {
- def +(other: Vector) = Vector(this.x + other.x, this.y + other.y)
- def -(other: Vector) = Vector(this.x - other.x, this.y - other.y)
- def *(factor: Double) = Vector(this.y * factor, this.y * factor)
- def normalize = {
- if (this.length == 0) this
- else Vector(this.x / length, y / length)
- }
- def o(other: Vector) = (x * other.x) + (y * other.y)
- val length = Math.sqrt(Math.pow(this.x, 2) + Math.pow(this.y, 2))
- def negate = Vector(this.x, this.y) * -1
- }
- case class AABB(halfSize: Vector, center: Vector) {
- val min = center - halfSize
- val max = center + halfSize
- def contains(vector: Vector): Boolean = {
- (min.x <= vector.x && vector.x <= max.x) &&
- (min.y <= vector.y && vector.y <= max.y)
- }
- def intersects(other: AABB): Boolean = {
- (min.x < other.max.x) && (max.x > other.min.x) &&
- (min.y < other.max.y) && (max.y > other.min.y)
- }
- }
- case class Entity(id: String, boundingBox: AABB, position: Vector)
- type Coords = (Int, Int)
- case class Grid(val boundingBox: AABB, val cellSize: Double) {
- var cells = HashMap.empty[Coords, ArrayBuffer[Entity]]
- val entities = HashMap.empty[String, ArrayBuffer[Coords]]
- val width = boundingBox.halfSize.x * 2
- val height = boundingBox.halfSize.y * 2
- val rowCount = (width / cellSize).toInt
- val colCount = (height / cellSize).toInt
- val cellCount = rowCount * colCount
- val center = boundingBox.center
- def put(entity: Entity): Option[Coords] = {
- val position = entity.position
- if (!contains(entity)) None
- else {
- val coords = getCoordinates(position)
- cells.get(coords).map {
- case entities => entities += entity
- }.getOrElse {
- cells += (coords -> ArrayBuffer(entity))
- }
- Some(coords)
- }
- }
- def contains(entity: Entity): Boolean = boundingBox.contains(entity.position)
- def getEntites(coords: Coords): ArrayBuffer[Entity] = {
- cells.get(coords).map {
- case entities => entities
- }.getOrElse { ArrayBuffer.empty[Entity] }
- }
- def getCoordinates(vec: Vector): (Int, Int) = {
- ((vec.x / cellSize).toInt, (vec.y / cellSize).toInt)
- }
- def printGrid = {
- for (y <- -colCount / 2 to colCount / 2) {
- for (x <- -rowCount / 2 to rowCount / 2) {
- print((x, y) + ": " )
- print(cells.get((x, y)).map(_.size).getOrElse(0))
- print("\t")
- }
- println("\t")
- }
- }
- override def toString = {
- s"Grid{width: $width, height: $height, cellSize: $cellSize, cellCount: $cellCount, center: $center}"
- }
- }
- val boundingBox = AABB(halfSize = Vector(500, 500), center = Vector(0, 0))
- val g = Grid(boundingBox, cellSize = 500)
- g.put(Entity("ADOLF", boundingBox, Vector(-500, 500)))
- g.put(Entity("ADOLF", boundingBox, Vector(-250, 250)))
- g.put(Entity("ADOLF", boundingBox, Vector(500, 500)))
- g.put(Entity("ADOLF", boundingBox, Vector(250, 250)))
- g.put(Entity("ADOLF", boundingBox, Vector(500, -500)))
- g.put(Entity("ADOLF", boundingBox, Vector(-500, -500)))
- g.put(Entity("ADOLF", boundingBox, Vector(-250, -250)))
- g.put(Entity("ADOLF", boundingBox, Vector(0, 0)))
- println(g)
- g.cells.foreach(println)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement