Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import math, sequtils, pipe
- {.experimental.}
- using
- n: SomeNumber
- i: SomeInteger
- r: SomeReal
- type
- Vector2D*[T: SomeNumber] = object
- x*, y*: T
- using
- v, v1, v2: Vector2D
- vv: var Vector2D
- proc vec2*(x, y: SomeNumber): auto = Vector2D[SomeNumber](x: x, y: y)
- proc `+`*(v1, v2): auto = vec2(v1.x + v2.x, v1.y + v2.y)
- proc `-`*(v1, v2): auto = vec2(v1.x - v2.x, v1.y - v2.y)
- proc `*`*(v, n): auto = vec2(v.x * n, v.y * n)
- proc `*`*(n, v): auto = vec2(v.x * n, v.y * n)
- proc `/`*(v, r): auto = vec2(v.x / r, v.y / r)
- proc `/`*(v, i): auto = vec2(v.x div i, v.y div i)
- proc `+=`*(vv, v) = vv.x += v.x; vv.y += v.y
- proc `-=`*(vv, v) = vv.x -= v.x; vv.y -= v.y
- proc `*=`*(vv, n) = vv.x *= n; vv.y *= n
- proc `/=`*(vv, n) = vv.x /= n; vv.y /= n
- type
- Point2D*[T: SomeNumber] = object
- x*, y*: T
- using
- p, p1, p2: Point2D
- vp: var Point2D
- proc point2*(x, y: SomeNumber): auto = Point2D[SomeNumber](x: x, y: y)
- proc asVec2*[T](p: Point2D[T]): auto = Vector2D[T](x: p.x, y: p.y)
- proc `+`*(p, v): auto = point2(p.x + v.x, p.y + v.y)
- proc `-`*(p, v): auto = point2(p.x - v.x, p.y - v.y)
- proc `+=`*(vp, v) = vp.x += v.x; vp.y += v.y
- proc `-=`*(vp, v) = vp.x -= v.x; vp.y -= v.y
- proc dist*(p1, p2): auto = (p2.x - p1.x)^2 + (p2.y - p1.y)^2 |> float |> sqrt
- type
- Rect*[T: SomeNumber] = object
- x*, y*, w*, h*: T
- using
- r: Rect
- ri: Rect[SomeInteger]
- proc rect*(x, y, w, h: SomeNumber): auto = Rect[SomeNumber](x: x, y: y, w: w, h: h)
- proc x1*(r): auto = r.x
- proc x2*(r): auto = r.x + r.w - 1
- proc y1*(r): auto = r.y
- proc y2*(r): auto = r.y + r.h - 1
- iterator items*(ri): auto =
- for x in ri.x .. ri.x + ri.w:
- for y in ri.y .. ri.y + ri.h:
- yield point2(x, y)
- iterator outline*(ri): auto =
- for x in [ri.x, <ri.w]:
- for y in ri.y .. <ri.h:
- yield point2(x, y)
- for x in ri.x + 1 .. <ri.w - 1:
- for y in [ri.y, <ri.h]:
- yield point2(x, y)
- proc contains*(r, p): auto = p in toSeq(r.items)
- type
- Grid2D*[w, h: static[int], T: SomeNumber] = object
- grid: array[w, array[h, T]]
- using
- g: Grid2D
- vg: var Grid2D
- x, y: int
- positions: openArray[Point2D]
- proc `[]`*(g, x, y): auto = g.grid[x][y]
- proc `[]=`*(vg, x, y, n): auto = vg.grid[x][y] = n
- proc `[]`*(g, p): auto = g.grid[p.x][p.y]
- proc `[]=`*[T](vg, p, n): auto = vg.grid[p.x][p.y] = n
- proc inBounds*(g, x, y): auto = x >= 0 and y >= 0 and x < g.w and y < g.h
- proc inBounds*(g, p): auto = p.x >= 0 and p.y >= 0 and p.x < g.w and p.y < g.h
- iterator items*(g): auto =
- for x in 0 .. <g.w:
- for y in 0 .. <g.h:
- yield g[x, y]
- iterator xyitems*(g): auto =
- for x in 0 .. <g.w:
- for y in 0 .. <g.h:
- yield(x, y, g[x, y])
- iterator positions*(g): auto =
- for x in 0 .. <g.w:
- for y in 0 .. <g.h:
- yield(x, y)
- iterator select*(g, positions): auto =
- for pos in positions:
- yield g[pos.x, pos.y]
- iterator outline*(g): auto =
- for x in [g.x, <g.w]:
- for y in g.y .. <g.h:
- yield g[x, y]
- for x in g.x + 1 .. <g.w - 1:
- for y in [g.y, <g.h]:
- yield g[x, y]
- import hashes
- proc hash*(p): Hash =
- var h: Hash = 0
- h = h !& hash(p.x)
- h = h !& hash(p.y)
- result = !$h
- proc hash*(v): Hash =
- var h: Hash = 0
- h = h !& hash(v.x)
- h = h !& hash(v.y)
- result = !$h
- proc hash*(r): Hash =
- var h: Hash = 0
- h = h !& hash(r.x)
- h = h !& hash(r.y)
- h = h !& hash(r.w)
- h = h !& hash(r.h)
- result = !$h
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement