-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.nim
45 lines (41 loc) · 1.17 KB
/
utils.nim
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
type
PointConcept = concept u
u.x is float64
u.y is float64
Point* = tuple
x: float64
y: float64
Area* = tuple
left: float64
right: float64
top: float64
bottom: float64
proc areaFromUnits*(units: seq[PointConcept]): Area
proc inArea*(unit: PointConcept, a: Area): bool
proc getSqDistance*(u1, u2: PointConcept): float
from strutils import split
template debug*(v: string) =
when defined(stdebug):
const stdebug {.strdefine.}: string = "all"
const files = stdebug.split({','})
const ii = instantiationInfo()
when stdebug == "all" or ii.filename in files:
stderr.writeLine(ii.filename & ": " & v)
proc getSqDistance(u1, u2: PointConcept): float =
let dx = (u1.x - u2.x)
let dy = (u1.y - u2.y)
dx*dx + dy*dy
proc inArea(unit: PointConcept, a: Area): bool =
unit.x in a.left..a.right and unit.y in a.top..a.bottom
proc areaFromUnits(units: seq[PointConcept]): Area =
result.left = 1024
result.top = 1024
for u in units:
if u.x < result.left:
result.left = u.x
if u.x > result.right:
result.right = u.x
if u.y < result.top:
result.top = u.y
if u.y > result.bottom:
result.bottom = u.y