Advertisement
Guest User

Untitled

a guest
Oct 28th, 2017
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Nim 3.67 KB | None | 0 0
  1. import math, sequtils, pipe
  2.  
  3. {.experimental.}
  4.  
  5. using
  6.     n: SomeNumber
  7.     i: SomeInteger
  8.     r: SomeReal
  9.  
  10. type
  11.     Vector2D*[T: SomeNumber] = object
  12.         x*, y*: T
  13.  
  14. using
  15.     v, v1, v2: Vector2D
  16.     vv: var Vector2D
  17.  
  18. proc vec2*(x, y: SomeNumber): auto = Vector2D[SomeNumber](x: x, y: y)
  19.  
  20. proc `+`*(v1, v2): auto =   vec2(v1.x + v2.x, v1.y + v2.y)
  21. proc `-`*(v1, v2): auto =   vec2(v1.x - v2.x, v1.y - v2.y)
  22. proc `*`*(v, n): auto =     vec2(v.x * n, v.y * n)
  23. proc `*`*(n, v): auto =     vec2(v.x * n, v.y * n)
  24. proc `/`*(v, r): auto =     vec2(v.x / r, v.y / r)
  25. proc `/`*(v, i): auto =     vec2(v.x div i, v.y div i)
  26.  
  27. proc `+=`*(vv, v) = vv.x += v.x; vv.y += v.y
  28. proc `-=`*(vv, v) = vv.x -= v.x; vv.y -= v.y
  29. proc `*=`*(vv, n) = vv.x *= n; vv.y *= n
  30. proc `/=`*(vv, n) = vv.x /= n; vv.y /= n
  31.  
  32. type
  33.     Point2D*[T: SomeNumber] = object
  34.         x*, y*: T
  35.  
  36. using
  37.     p, p1, p2: Point2D
  38.     vp: var Point2D
  39.  
  40. proc point2*(x, y: SomeNumber): auto = Point2D[SomeNumber](x: x, y: y)
  41. proc asVec2*[T](p: Point2D[T]): auto = Vector2D[T](x: p.x, y: p.y)
  42.  
  43. proc `+`*(p, v): auto = point2(p.x + v.x, p.y + v.y)
  44. proc `-`*(p, v): auto = point2(p.x - v.x, p.y - v.y)
  45. proc `+=`*(vp, v) =     vp.x += v.x; vp.y += v.y
  46. proc `-=`*(vp, v) =     vp.x -= v.x; vp.y -= v.y
  47.  
  48. proc dist*(p1, p2): auto = (p2.x - p1.x)^2 + (p2.y - p1.y)^2 |> float |> sqrt
  49.  
  50. type
  51.     Rect*[T: SomeNumber] = object
  52.         x*, y*, w*, h*: T
  53.  
  54. using
  55.     r: Rect
  56.     ri: Rect[SomeInteger]
  57.  
  58. proc rect*(x, y, w, h: SomeNumber): auto = Rect[SomeNumber](x: x, y: y, w: w, h: h)
  59.  
  60. proc x1*(r): auto = r.x
  61. proc x2*(r): auto = r.x + r.w - 1
  62. proc y1*(r): auto = r.y
  63. proc y2*(r): auto = r.y + r.h - 1
  64.  
  65. iterator items*(ri): auto =
  66.     for x in ri.x .. ri.x + ri.w:
  67.         for y in ri.y .. ri.y + ri.h:
  68.             yield point2(x, y)
  69.  
  70. iterator outline*(ri): auto =
  71.     for x in [ri.x, <ri.w]:
  72.         for y in ri.y .. <ri.h:
  73.             yield point2(x, y)
  74.     for x in ri.x + 1 .. <ri.w - 1:
  75.         for y in [ri.y, <ri.h]:
  76.             yield point2(x, y)
  77.  
  78. proc contains*(r, p): auto = p in toSeq(r.items)
  79.  
  80. type
  81.     Grid2D*[w, h: static[int], T: SomeNumber] = object
  82.         grid: array[w, array[h, T]]
  83.  
  84. using
  85.     g: Grid2D
  86.     vg: var Grid2D
  87.     x, y: int
  88.     positions: openArray[Point2D]
  89.  
  90. proc `[]`*(g, x, y): auto =         g.grid[x][y]
  91. proc `[]=`*(vg, x, y, n): auto =    vg.grid[x][y] = n
  92. proc `[]`*(g, p): auto =            g.grid[p.x][p.y]
  93. proc `[]=`*[T](vg, p, n): auto =    vg.grid[p.x][p.y] = n
  94.  
  95. proc inBounds*(g, x, y): auto = x >= 0 and y >= 0 and x < g.w and y < g.h
  96. proc inBounds*(g, p): auto =    p.x >= 0 and p.y >= 0 and p.x < g.w and p.y < g.h
  97.  
  98. iterator items*(g): auto =
  99.     for x in 0 .. <g.w:
  100.         for y in 0 .. <g.h:
  101.             yield g[x, y]
  102.  
  103. iterator xyitems*(g): auto =
  104.     for x in 0 .. <g.w:
  105.         for y in 0 .. <g.h:
  106.             yield(x, y, g[x, y])
  107.  
  108. iterator positions*(g): auto =
  109.     for x in 0 .. <g.w:
  110.         for y in 0 .. <g.h:
  111.             yield(x, y)
  112.  
  113. iterator select*(g, positions): auto =
  114.     for pos in positions:
  115.         yield g[pos.x, pos.y]
  116.  
  117. iterator outline*(g): auto =
  118.     for x in [g.x, <g.w]:
  119.         for y in g.y .. <g.h:
  120.             yield g[x, y]
  121.     for x in g.x + 1 .. <g.w - 1:
  122.         for y in [g.y, <g.h]:
  123.             yield g[x, y]
  124.  
  125. import hashes
  126.  
  127. proc hash*(p): Hash =
  128.     var h: Hash = 0
  129.     h = h !& hash(p.x)
  130.     h = h !& hash(p.y)
  131.     result = !$h
  132.  
  133. proc hash*(v): Hash =
  134.     var h: Hash = 0
  135.     h = h !& hash(v.x)
  136.     h = h !& hash(v.y)
  137.     result = !$h
  138.  
  139. proc hash*(r): Hash =
  140.     var h: Hash = 0
  141.     h = h !& hash(r.x)
  142.     h = h !& hash(r.y)
  143.     h = h !& hash(r.w)
  144.     h = h !& hash(r.h)
  145.     result = !$h
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement