-
Notifications
You must be signed in to change notification settings - Fork 0
/
Square.scala
55 lines (48 loc) · 2.02 KB
/
Square.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package scalabrot
import Direction._
import java.awt.Graphics2D
class Square(override val north: Double,
override val east: Double,
val halfWidth: Double) extends Region(north, east, halfWidth) {
def contains(reg: Region): Boolean = {
(north - halfWidth <= reg.north) &
(reg.north < halfWidth + north) &
(east - halfWidth <= reg.east) &
(reg.east < halfWidth + east)
}
def quadrantContaining(reg: Region): Direction = {
val northDir = reg.north.compare(north)
val eastDir = reg.east.compare(east)
new Direction(northDir, eastDir)
}
def quadrant(dir: Direction): Square = {
val newHalfWidth = halfWidth / 2
new Square(north + newHalfWidth * dir.north,
east + newHalfWidth * dir.east,
newHalfWidth)
}
def neighbor(dir: Direction): Square = {
new Square(north + halfWidth * dir.north * 2,
east + halfWidth * dir.east * 2,
halfWidth)
}
override def toString(): String = {
val eastPart = "(%f" format east
val northPart = ", %f): " format north
val widthPart = "%f" format halfWidth
eastPart ++ northPart ++ widthPart
}
def paint(g: Graphics2D, northCenter: Double, eastCenter: Double, resolution: Int, totalWidth: Double) {
val width = (halfWidth * 2 * resolution / totalWidth).toInt
val x = (east - eastCenter + totalWidth / 2.0 - halfWidth) * resolution / totalWidth
val y = (north - northCenter + totalWidth / 2.0 - halfWidth) * resolution / totalWidth
g.fillRect( x.toInt,
y.toInt,
width,
width)
g.drawRect( x.toInt,
y.toInt,
width,
width)
}
}