c# - Brushes.White slows graphics demo down -
below (very naive) implementation of conway's game of life in wpf. it's demo...
xaml:
<window x:class="wpf_conway_life_2013_05_19.mainwindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" title="mainwindow" height="500" width="500"> <grid> <canvas name="canvas" width="auto" height="auto" horizontalalignment="stretch" verticalalignment="stretch"> </canvas> </grid> </window>
code behind:
using system; using system.windows; using system.windows.controls; using system.windows.media; using system.windows.shapes; using system.windows.threading; namespace wpf_conway_life_2013_05_19 { public partial class mainwindow : window { public mainwindow() { initializecomponent(); var random = new random(); var data = new int[100, 100]; var datab = new int[100, 100]; func<int, int, int> @ = (x, y) => { if (x < 0) x = 100 + x; if (x >= 100) x = x % 100; if (y < 0) y = 100 + y; if (y >= 100) y = y % 100; return data[x, y]; }; (var x = 0; x < 100; x++) (var y = 0; y < 100; y++) data[x, y] = random.next(2); var rectangles = new rectangle[100, 100]; (var x = 0; x < 100; x++) (var y = 0; y < 100; y++) { rectangles[x, y] = new rectangle(); canvas.children.add(rectangles[x, y]); } canvas.sizechanged += (s, e) => { (var x = 0; x < 100; x++) { (var y = 0; y < 100; y++) { rectangles[x, y].width = canvas.actualwidth / 100; rectangles[x, y].height = canvas.actualheight / 100; canvas.setleft(rectangles[x, y], (canvas.actualwidth / 100) * x); canvas.settop(rectangles[x, y], (canvas.actualheight / 100) * y); } } }; action macrostep = () => { datab = new int[100, 100]; (var x = 0; x < 100; x++) { (var y = 0; y < 100; y++) { var neighbors = 0; (var = -1; <= 1; i++) (var j = -1; j <= 1; j++) if (i == 0 && j == 0) continue; else neighbors += at(x + i, y + j); datab[x, y] = data[x, y]; if (neighbors < 2) datab[x, y] = 0; if (neighbors == 3) datab[x, y] = 1; if (neighbors > 3) datab[x, y] = 0; rectangles[x, y].fill = datab[x, y] == 0 ? new solidcolorbrush(new color()) : brushes.black; } } data = datab; }; var timer = new dispatchertimer(); timer.tick += (s, e) => macrostep(); timer.start(); } } }
here's looks like:
if replace new solidcolorbrush(new color())
brushes.white
program runs more slowly. why?
i'm testing on windows 7 64-bit using 2010 express.
because new color()
has alpha value of zero, means wpf doesn't have render because it's transparent - on other hand white color's alpha 255, means solid white color have rendered.
Comments
Post a Comment