/// <summary> /// Draws circles that decrease in size to build a flower that is animated /// </summary> private void DrawEllipsesFlower(WriteableBitmap writeableBmp) { if (writeableBmp == null) { return; } // Init some size vars int w = writeableBmp.PixelWidth - 2; int h = writeableBmp.PixelHeight - 2; // Wrap updates in a GetContext call, to prevent invalidation and nested locking/unlocking during this block using (writeableBmp.GetBitmapContext()) { // Increment frame counter if (++frameCounter >= int.MaxValue || frameCounter < 1) { frameCounter = 1; } double s = Math.Sin(frameCounter * 0.01); if (s < 0) { s *= -1; } // Clear writeableBmp.Clear(); // Draw center circle int xc = w >> 1; int yc = h >> 1; // Animate base size with sine int r0 = (int)((w + h) * 0.07 * s) + 10; System.Windows.Media.Imaging.Color color_brown = System.Windows.Media.Imaging.Color.FromArgb( 255, System.Drawing.Color.Brown.R, System.Drawing.Color.Brown.G, System.Drawing.Color.Brown.B); writeableBmp.DrawEllipseCentered(xc, yc, r0, r0, color_brown); // Draw outer circles int dec = (int)((w + h) * 0.0045f); int r = (int)((w + h) * 0.025f); int offset = r0 + r; for (int i = 1; i < 6 && r > 1; i++) { for (double f = 1; f < 7; f += 0.7) { // Calc postion based on unit circle int xc2 = (int)(Math.Sin(frameCounter * 0.002 * i + f) * offset + xc); int yc2 = (int)(Math.Cos(frameCounter * 0.002 * i + f) * offset + yc); int col = (int)(0xFFFF0000 | (uint)(0x1A * i) << 8 | (uint)(0x20 * f)); writeableBmp.DrawEllipseCentered(xc2, yc2, r, r, col); } // Next ring offset += r; r -= dec; offset += r; } // Invalidates on exit of using block } }
/// <summary> /// Draws the different types of shapes. /// </summary> private void DrawStaticShapes(WriteableBitmap writeableBmp) { // HideShapeCountText(); if (writeableBmp != null) { // Wrap updates in a GetContext call, to prevent invalidation and nested locking/unlocking during this block using (writeableBmp.GetBitmapContext()) { // Init some size vars int w = writeableBmp.PixelWidth; int h = writeableBmp.PixelHeight; int w3 = w / 3; int h3 = h / 3; int w6 = w3 >> 1; int h6 = h3 >> 1; int w12 = w6 >> 1; int h12 = h6 >> 1; // Clear writeableBmp.Clear(); // Fill closed concave polygon var p = new int[] { w12 >> 1, h12, w6, h3 - (h12 >> 1), w3 - (w12 >> 1), h12, w6 + w12, h12, w6, h6 + h12, w12, h12, w12 >> 1, h12, }; writeableBmp.FillPolygonsEvenOdd(new[] { p }, GetRandomColor()); // Fill closed convex polygon p = new int[] { w3 + w6, h12 >> 1, w3 + w6 + w12, h12, w3 + w6 + w12, h6 + h12, w3 + w6, h6 + h12 + (h12 >> 1), w3 + w12, h6 + h12, w3 + w12, h12, w3 + w6, h12 >> 1, }; writeableBmp.FillPolygon(p, GetRandomColor()); // Fill Triangle + Quad writeableBmp.FillTriangle(2 * w3 + w6, h12 >> 1, 2 * w3 + w6 + w12, h6 + h12, 2 * w3 + w12, h6 + h12, GetRandomColor()); writeableBmp.FillQuad(w6, h3 + (h12 >> 1), w6 + w12, h3 + h6, w6, h3 + h6 + h12 + (h12 >> 1), w12, h3 + h6, GetRandomColor()); // Fill Ellipses writeableBmp.FillEllipse(rand.Next(w3, w3 + w6), rand.Next(h3, h3 + h6), rand.Next(w3 + w6, 2 * w3), rand.Next(h3 + h6, 2 * h3), GetRandomColor()); writeableBmp.FillEllipseCentered(2 * w3 + w6, h3 + h6, w12, h12, GetRandomColor()); // Fill closed Cardinal Spline curve p = new int[] { w12 >> 1, 2 * h3 + h12, w6, h - (h12 >> 1), w3 - (w12 >> 1), 2 * h3 + h12, w6 + w12, 2 * h3 + h12, w6, 2 * h3 + (h12 >> 1), w12, 2 * h3 + h12, }; writeableBmp.FillCurveClosed(p, 0.5f, GetRandomColor()); // Fill closed Beziér curve p = new int[] { w3 + w12, 2 * h3 + h6 + h12, w3 + w6 + (w12 >> 1), 2 * h3, w3 + w6 + w12 + (w12 >> 1), 2 * h3, w3 + w6 + w12, 2 * h3 + h6 + h12, }; writeableBmp.FillBeziers(p, GetRandomColor()); // Fill Rectangle writeableBmp.FillRectangle(rand.Next(2 * w3, 2 * w3 + w6), rand.Next(2 * h3, 2 * h3 + h6), rand.Next(2 * w3 + w6, w), rand.Next(2 * h3 + h6, h), GetRandomColor()); // Fill another rectangle with alpha blending writeableBmp.FillRectangle(rand.Next(2 * w3, 2 * w3 + w6), rand.Next(2 * h3, 2 * h3 + h6), rand.Next(2 * w3 + w6, w), rand.Next(2 * h3 + h6, h), GetRandomColor(), true); System.Windows.Media.Imaging.Color black = System.Windows.Media.Imaging.Color.FromArgb(255, 0, 0, 0); // Draw Grid writeableBmp.DrawLine(0, h3, w, h3, Colors.Black); writeableBmp.DrawLine(0, 2 * h3, w, 2 * h3, Colors.Black); writeableBmp.DrawLine(w3, 0, w3, h, Colors.Black); writeableBmp.DrawLine(2 * w3, 0, 2 * w3, h, Colors.Black); // Invalidates on exit of Using block } } }