private void DoReset() { var rnd = random.Value; HSBPalette palette = new HSBPalette(); // Create a new array of points for (int i = 0; i < _pointCount; i++) { var point = new Point(rnd.NextDouble(), rnd.NextDouble()); _points[i] = point; var vertices = SharpDXHelper.MakeRectangle((float)point.X, (float)point.Y, 0.05f, 0.05f); for (var j = 0; j < vertices.Count; j++) { _currentArray[i * 6 + j] = vertices[j]; } } // Replace the old array of points with the new array Points = _points; var temp = (Positions as DxVertex[]) ?? new DxVertex[_pointCount * 6]; Positions = _currentArray; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Positions))); _currentArray = temp; }
public DxInstancedRectangleTexturedSampleViewModel() { Geometry = SharpDXHelper.MakeRectangle(0f, 0f, 0.05f, 0.05f); StartArrayUpdate(); }
private void StartArrayUpdate() { Task.Factory.StartNew(() => { // Create the random directions var rnd = random.Value; Vector[] directions = Enumerable.Range(0, _pointCount).Select(i => { var angle = rnd.NextDouble() * Math.PI * 2; Vector dxdy = new Vector(Math.Sin(angle), Math.Cos(angle)); return(dxdy); }).ToArray(); while (!_hasBeenDisposed) { if (_doReset) { DoReset(); _doReset = false; } if (_animationRunning) { // Update the position of every point according to its direction for (int i = 0; i < Points.Count(); i++) { var dxdy = new Vector(directions[i].X * Width1Px, directions[i].Y * Height1Px); var newX = dxdy.X + _points[i].X; var newY = dxdy.Y + _points[i].Y; // Point has hit an east or west wall. Reverse direction vector X component if (newX < 0 || newX > 1) { newX = Math.Min(1, Math.Max(0, newX)); directions[i] = new Vector(-directions[i].X, directions[i].Y); } // Point has hit a north or south wall. Reverse direction vector Y component if (newY < 0 || newY > 1) { newY = Math.Min(1, Math.Max(0, newY)); directions[i] = new Vector(directions[i].X, -directions[i].Y); } _points[i] = new Point(newX, newY); var rectangle = SharpDXHelper.MakeRectangle((float)newX, (float)newY, 0.05f, 0.05f); for (var j = 0; j < 6; j++) { _currentArray[i * 6 + j] = rectangle[j]; } } Points = _points; var temp = (Positions as DxVertex[]) ?? new DxVertex[_pointCount * 6]; Positions = _currentArray; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Positions))); _currentArray = temp; } Thread.Sleep(10); } }); }