commit d853f9f14c179c5dbf3485ae56397ff249a3a699 Author: Walter Oggioni Date: Thu Jan 3 17:39:40 2019 +0100 initial commit diff --git a/geo2d.nim b/geo2d.nim new file mode 100644 index 0000000..43c8a46 --- /dev/null +++ b/geo2d.nim @@ -0,0 +1,50 @@ +import mmath/smatrix +import mmath/svector +from nwo/utils import `...` +from math import sin, cos + +type + X2d* = SquareSMatrix[3, float32] + P2d* = SVector[3, float32] + Rect2d* = object + tl*, br* : P2d + +proc newP2d*(x, y : float32) : P2d = + P2d(buildSVector[3, float32](x,y,1f32)) + +proc x*(p : P2d) : float32 = p[0] +proc y*(p : P2d) : float32 = p[1] + +proc newRect2d*(x, y, width, height : float32) : Rect2d = Rect2d(tl: newP2d(x,y), br: newP2d(x + width, y + height)) +proc top*(rect : Rect2d) : float32 = rect.tl.y +proc bottom*(rect : Rect2d) : float32 = rect.br.y +proc left*(rect : Rect2d) : float32 = rect.tl.x +proc right*(rect : Rect2d) : float32 = rect.br.x +proc width*(rect : Rect2d) : float32 = rect.br.x - rect.tl.x +proc height*(rect : Rect2d) : float32 = rect.br.y - rect.tl.y +proc `*`*(rect : Rect2d, xform : X2d) : Rect2d = Rect2d(tl: rect.tl * xform, br: rect.br * xform) + +proc rot*(alpha: float32) : X2d = + result = identity[3,float32]() + let ca = cos(alpha) + let sa = sin(alpha) + result[0,0] = ca + result[1,1] = ca + result[0,1] = sa + result[1,0] = -sa + +proc scale*(x, y: float32) : X2d = + result = identity[3,float32]() + result[0,0] = x + result[1,1] = y + +proc xlate*(x, y: float32) : X2d = + result = identity[3,float32]() + result[2,0] = x + result[2,1] = y + +proc scale*(center : P2d, x, y : float32) : X2d = xlate(-center.x, -center.y) * scale(x,y) * xlate(center.x, center.y) +proc rot*(center : P2d, angle : float32) : X2d = xlate(-center.x, -center.y) * rot(angle) * xlate(center.x, center.y) + + + diff --git a/grid.nim b/grid.nim new file mode 100644 index 0000000..038beb7 --- /dev/null +++ b/grid.nim @@ -0,0 +1,73 @@ +import random +from times import epochTime +type Grid* = object + rows*, columns* : uint32 + data : seq[uint32] + +proc newGrid*(rows, columns : uint32) : Grid = + Grid( + rows: rows, + columns: columns, + data: newSeq[uint32]((rows * columns + 8 - 1) div 8) + ) + +proc get(grid : Grid, x, y : uint32) : bool = + assert x < grid.columns + assert y < grid.rows + let index = y * grid.columns + x + (grid.data[index div 8] and (1u32 shl (index mod 8))) != 0 + +proc set(grid : var Grid, x, y : uint32, value : bool) : void = + assert x < grid.columns + assert y < grid.rows + let index = y * grid.columns + x + grid.data[(index div 8)] = + if value: + grid.data[(index div 8)] or (1u32 shl (index mod 8)) + else: + grid.data[(index div 8)] and (not (1u32 shl (index mod 8))) + +template `[]`*(grid : Grid, x,y : uint32) : bool = + get(grid, x, y) + +template `[]=`*(grid : Grid, x,y : uint32, value : bool) = + set(grid, x, y, value) + +proc next_step*(old_grid, new_grid : var Grid) = + for x in 0.. 3: + new_grid[x, y] = false + else: + new_grid[x, y] = true + elif alive_neighbours == 3: + new_grid[x, y] = true + else: + new_grid[x, y] = false + +proc rand_init*(grid : var Grid) = + var rand = initRand(epochTime().int64) + for x in 0.. 0: + uxform = uxform * scale(newP2d(windowSize.width.float32 / 2f32, windowSize.height.float32 / 2f32), 1.1f32, 1.1f32) + else: + uxform = uxform * scale(newP2d(windowSize.width.float32 / 2f32, windowSize.height.float32 / 2f32), 0.9f32, 0.9f32) + of MouseMotion: + let mouseMotionEvent = cast[MouseMotionEventPtr](addr(evt)) + let position = newP2d(mouseMotionEvent.x.float32, mouseMotionEvent.y.float32) + + else: + discard + + let dt = fpsman.getFramerate() / 1000 + + render.setDrawColor 0,0,0,255 + render.clear + var + x : float32 = bb.left + y : float32 = bb.top + + render.setDrawColor 127,127,127,255 + let f = min(windowSize.width / bb.width, windowSize.height / bb.height) + let xform = xlate(-bb.width / 2.0f32, -bb.height / 2.0f32) * scale(f, f) * + xlate(windowSize.width / 2.0f32, windowSize.height / 2.0f32) * uxform + + while x <= bb.width: + render.drawLine(newP2d(x, bb.top) * xform, newP2d(x, bb.height) * xform) + x += side.float32 + + while y <= bb.height: + render.drawLine(newP2d(bb.left, y) * xform, newP2d(bb.width, y) * xform) + y += side.float32 + + render.setDrawColor 255,255,255,255 + for i in 0.. elapsed_time_sec: + elapsed_time_sec = current_time_sec + next_step(current_grid[], next_grid[]) + let tmp = current_grid + current_grid = next_grid + next_grid = tmp + +destroy render +destroy window \ No newline at end of file