//Calculates neighbourhood number of one cell override public int neightbours_number(ModuloIndexableList <ModuloIndexableList <int> > matrix, int i, int j) { TriangleUlamNeightbourHandler thc = new TriangleUlamNeightbourHandler(); int l = thc.neightbours_number(matrix, i, j); if (i % 4 == 0) { l = l + matrix[i - 3][j - 1]; l = l + matrix[i - 3][j]; l = l + matrix[i + 1][j + 1]; l = l + matrix[i + 3][j + 1]; l = l + matrix[i + 1][j - 2]; l = l + matrix[i + 3][j - 1]; } else if (i % 4 == 1) { l = l + matrix[i - 3][j - 1]; l = l + matrix[i - 1][j - 1]; l = l + matrix[i + 3][j]; l = l + matrix[i + 3][j + 1]; l = l + matrix[i - 3][j + 1]; l = l + matrix[i - 1][j + 2]; } else if (i % 4 == 2) { l = l + matrix[i - 3][j + 1]; l = l + matrix[i - 3][j]; l = l + matrix[i + 1][j + 2]; l = l + matrix[i + 3][j + 1]; l = l + matrix[i + 1][j - 1]; l = l + matrix[i + 3][j - 1]; } else if (i % 4 == 3) { l = l + matrix[i - 3][j - 1]; l = l + matrix[i - 1][j - 2]; l = l + matrix[i + 3][j]; l = l + matrix[i + 3][j - 1]; l = l + matrix[i - 3][j + 1]; l = l + matrix[i - 1][j + 1]; } return(l); }
public void Drawing() { //Default colors color black; black.red = 0; black.green = 0; black.blue = 0; black.alpha = ColorHandler.colorMaxValue; color white; white.red = ColorHandler.colorMaxValue; white.green = ColorHandler.colorMaxValue; white.blue = ColorHandler.colorMaxValue; white.alpha = ColorHandler.colorMaxValue; //Initialization of graphics and opening painting window Sdl.SDL_Event ev = new Sdl.SDL_Event(); Sdl.SDL_Init(Sdl.SDL_INIT_VIDEO); System.IntPtr screen = Sdl.SDL_SetVideoMode(0, 0, 0, Sdl.SDL_ANYFORMAT); if (screen == IntPtr.Zero) { System.Console.WriteLine("Nem sikerult megnyitni az ablakot!\n"); return; } Sdl.SDL_WM_SetCaption("SDL peldaprogram", "SDL peldaprogram"); //Sets NeightbourHandler NeightbourHandler nh = new QuadraticConwayNeightbourHandler(); if (mesh == MeshType.SQUARE) { if (neighbourhood == NeighbourhoodType.ULAM) { nh = new QuadraticUlamNeightbourHandler(); } else if (neighbourhood == NeighbourhoodType.KNIGHT) { nh = new QuadraticKnightNeightbourHandler(); } } else if (mesh == MeshType.TRIANGLE) { if (neighbourhood == NeighbourhoodType.ULAM) { nh = new TriangleUlamNeightbourHandler(); } else if (neighbourhood == NeighbourhoodType.CONWAY) { nh = new TriangleConwayNeightbourHandler(); } else if (neighbourhood == NeighbourhoodType.KNIGHT) { nh = new TriangleKnightNeightbourHandler(); } } else if (mesh == MeshType.HEXAGON) { if (neighbourhood == NeighbourhoodType.ULAM) { nh = new HexagonUlamNeightbourHandler(); } else if (neighbourhood == NeighbourhoodType.CONWAY) { nh = new HexagonConwayNeightbourHandler(); } else if (neighbourhood == NeighbourhoodType.KNIGHT) { nh = new HexagonKnightNeightbourHandler(); } } //Sets MatrixHandler and ColorHandler MatrixHandler game = new QuadraticMatrixHandler(random, nh, ageT); ColorHandler ch = new ColorHandler(random); //Gets sizes of screen int width = Screen.PrimaryScreen.Bounds.Width; int height = Screen.PrimaryScreen.Bounds.Height; //Calculates main datas int cellSize = 10; const int half = ColorHandler.colorMaxValue / 2; const int state_number = 2; // Itt rajzolj int n = width / cellSize; int m = height / cellSize; int del = 0; int delt = max(n, m) * 2; //Generates random matrix as default double probability = 0.3; ModuloIndexableList <ModuloIndexableList <int> > cell_matrix = game.generate_random_matrix(n, m, probability); if (matrix == MatrixType.ONE_POINT) { cell_matrix = game.generate_matrix(n, m); } //Adds state rules in order of states List <List <int> > rule = new List <List <int> >(); rule.Add(borns); rule.Add(survives); //Generating and setting colors List <color> colors = ch.generate_colors_(); int index1 = random.Next(0, colors.Count); color c1 = colors[index1]; Dictionary <color, List <color> > color_matrix = ch.generate_colors(); List <color> cls = color_matrix[c1]; int index2 = random.Next(0, cls.Count); color col = cls[index2]; color col1 = white; color col2 = black; int wt = random.Next(0, 6); if (wt == 0) { col1 = c1; col2 = ch.generate_dark(col); } else if (wt == 1) { col1 = ch.generate_light(col); col2 = c1; } else if (wt == 2) { col1 = col; col2 = c1; } else if (wt == 3) { col1 = c1; col2 = col; } else if (wt == 4) { col1 = c1; col2 = ch.generate_light(col); } if (wt == 5) { col1 = ch.generate_dark(col); col2 = c1; } Sdl.SDL_WaitEvent(out ev); //Drawing loop while (ev.type != Sdl.SDL_QUIT && ev.type != Sdl.SDL_KEYDOWN) { if (ev.type == Sdl.SDL_KEYDOWN) { System.Console.WriteLine("Billentyű észlelve."); Sdl.SDL_Quit(); } else if (ev.type == Sdl.SDL_MOUSEBUTTONDOWN) { System.Console.WriteLine("Egér észlelve."); Sdl.SDL_Quit(); } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (mesh == MeshType.SQUARE) { short x1 = (short)(i * cellSize + 1); short y1 = (short)(j * cellSize + 1); short x2 = (short)((i + 1) * cellSize); short y2 = (short)((j + 1) * cellSize); // drawing quadratic mesh with age if (ageT) { List <color> cols = ch.generate_age_colors(); if (cell_matrix[i][j] == 0) { Tao.Sdl.SdlGfx.boxRGBA(screen, x1, y1, x2, y2, (byte)(black.red), (byte)(black.green), (byte)(black.blue), (byte)(black.alpha)); } else { int ix = cell_matrix[i][j] % cols.Count; if (ix < 0) { ix = ix + cols.Count; } Tao.Sdl.SdlGfx.boxRGBA(screen, x1, y1, x2, y2, (byte)((cols[ix]).red), (byte)((cols[ix]).green), (byte)((cols[ix]).blue), (byte)(cols[ix].alpha)); } } else { // drawing quadratic mesh without age if (cell_matrix[i][j] == 1) { Tao.Sdl.SdlGfx.boxRGBA(screen, x1, y1, x2, y2, (byte)(col2.red), (byte)(col2.green), (byte)(col2.blue), (byte)(col2.alpha)); } else if (cell_matrix[i][j] == 0) { Tao.Sdl.SdlGfx.boxRGBA(screen, x1, y1, x2, y2, (byte)(col1.red), (byte)(col1.green), (byte)(col1.blue), (byte)(col1.alpha)); } } } else { // drawing another mesh List <Tuple <double, double> > points = new List <Tuple <double, double> >(); if (mesh == MeshType.TRIANGLE) { points = calculate_triangle_coordinates(i, j); } else if (mesh == MeshType.HEXAGON) { points = calculate_hexagon_coordinates(i, j); } short[] vx = new short[points.Count]; short[] vy = new short[points.Count]; for (int k = 0; k < points.Count; k++) { double xx = points[k].Item1; double yy = points[k].Item2; xx = xx * cellSize; if (mesh == MeshType.TRIANGLE) { //to have better rates of sides and hights xx = xx * Math.Sqrt(3); //to set the middle into the middle xx = xx + 10 * (1 - Math.Sqrt(3)) * cellSize; yy = yy + 5 * (1 - Math.Sqrt(3)) * cellSize; } else if (mesh == MeshType.HEXAGON) { //to set the middle into the middle xx = xx + 30 * (1 - Math.Sqrt(3)) * cellSize; yy = yy + 3 * (1 - Math.Sqrt(3)) * cellSize; } yy = yy * cellSize; int xt = (int)xx; int yt = (int)yy; short xs = (short)xt; short ys = (short)yt; vx[k] = xs; vy[k] = ys; if ((mesh == MeshType.TRIANGLE) || (mesh == MeshType.HEXAGON)) { vx[k] = ys; vy[k] = xs; } } if (ageT) { // drawing mesh with age List <color> cols = ch.generate_age_colors(); if (cell_matrix[i][j] == 0) { Tao.Sdl.SdlGfx.filledPolygonRGBA(screen, vx, vy, points.Count, (byte)(black.red), (byte)(black.green), (byte)(black.blue), (byte)(black.alpha)); } else { int ix = cell_matrix[i][j] % cols.Count; if (ix < 0) { ix = ix + cols.Count; } Tao.Sdl.SdlGfx.filledPolygonRGBA(screen, vx, vy, points.Count, (byte)((cols[ix]).red), (byte)((cols[ix]).green), (byte)((cols[ix]).blue), (byte)(cols[ix].alpha)); } } else { // drawing mesh without age if (cell_matrix[i][j] == 1) { Tao.Sdl.SdlGfx.filledPolygonRGBA(screen, vx, vy, points.Count, (byte)(col2.red), (byte)(col2.green), (byte)(col2.blue), (byte)(col2.alpha)); } else if (cell_matrix[i][j] == 0) { Tao.Sdl.SdlGfx.filledPolygonRGBA(screen, vx, vy, points.Count, (byte)(col1.red), (byte)(col1.green), (byte)(col1.blue), (byte)(col1.alpha)); } } } } } if (del == delt) { if (matrix == MatrixType.RANDOM) { cell_matrix = game.generate_random_matrix(n, m, probability); } else if (matrix == MatrixType.ONE_POINT) { cell_matrix = game.generate_matrix(n, m); del = 0; } wt = random.Next(0, 6); if (wt == 0) { col1 = c1; col2 = ch.generate_dark(col); } else if (wt == 1) { col1 = ch.generate_light(col); col2 = c1; } else if (wt == 2) { col1 = col; col2 = c1; } else if (wt == 3) { col1 = c1; col2 = col; } else if (wt == 4) { col1 = c1; col2 = ch.generate_light(col); } if (wt == 5) { col1 = ch.generate_dark(col); col2 = c1; } } else { cell_matrix = game.new_matrix(cell_matrix, rule); } /* Flips drawings */ Sdl.SDL_Flip(screen); Sdl.SDL_WaitEvent(out ev); System.Threading.Thread.Sleep(1000); del = del + 1; } /* closing window */ Sdl.SDL_Quit(); return; }