BLOG.CSHARPHELPER.COM: Make a rotating radial brush in C#
Make a rotating radial brush in C#
When the program starts, it creates points that define a regular polygon.
// The polygon's points. private PointF[] PolygonPoints;
// The path. private GraphicsPath Path;
// Offset when assigning colors. private int ColorOffset = 0;
// Make points that define a polygon. private void Form1_Load(object sender, EventArgs e) { // Make the polygon's points. PolygonPoints = MakePolygon(22, this.ClientRectangle);
// Make the brush's path. Path = new GraphicsPath(); Path.AddPolygon(DoublePoints(PolygonPoints)); }
The form's Load event handler calls MakePolygon to make an array of points defining the polygon. It saves those points and then uses them to make the GraphicsPath that it will later use to make the brush.
The MakePolygon method makes the polygon.
// Return PointFs to define a polygon. private PointF[] MakePolygon(int num_points, Rectangle bounds) { // Make room for the points. PointF[] pts = new PointF[num_points];
// Start at the top. double theta = -Math.PI / 2; double dtheta = 2 * Math.PI / num_points; for (int i = 0; i < num_points; i++) { pts[i] = new PointF( (float)(cx + rx1 * Math.Cos(theta)), (float)(cy + ry1 * Math.Sin(theta))); theta += dtheta; }
return pts; }
The MakePolygon calculates the center point for the polygon. It then uses an angle theta to generate the polygon's points.
The DoublePoints method takes an array of PointF and returns a new array that has extra points added between each of the existing points.
// Insert a point between each of the polygon's points. private PointF[] DoublePoints(PointF[] points) { List new_points = new List(); for (int i = 0; i < points.Length - 1; i++) { new_points.Add(points[i]); new_points.Add(PointBetween(points[i], points[i + 1])); } new_points.Add(points[points.Length - 1]); new_points.Add(PointBetween(points[0], points[points.Length - 1]));
// Return the new points. return new_points.ToArray(); }
// Return a point between two points. private PointF PointBetween(PointF point1, PointF point2) { return new PointF( (point1.X + point2.X) / 2, (point1.Y + point2.Y) / 2); }
The DoublePoints method loops through the existing points adding them to a list. It also uses the PointBetween method to add new points between the existing ones to the list. When it finishes, it converts the list into an array.
The form's Paint event handler draws with the rotating brush.
// Draw the polygon. private void Form1_Paint(object sender, PaintEventArgs e) { // Make a path gradient brush. using (PathGradientBrush br = new PathGradientBrush(Path)) { // Define edge colors. Color[] edge_colors = new Color[PolygonPoints.Length * 2]; Color[] color_series = new Color[] { Color.Green, Color.LightGreen, Color.White, Color.LightGreen, }; for (int i = 0; i < edge_colors.Length; i++) { edge_colors[i] = color_series[(i + ColorOffset) % color_series.Length]; } br.SurroundColors = edge_colors; br.CenterColor = Color.White; ColorOffset++;
The Paint event handler defines an array to hold colors for the polygon's edges. It then loops through that array assigning colors in turn from the color_series array. This gives the edge colors the pattern green, light green, white, light green, and then repeating. The program adds the value ColorOffset to each color's index so in subsequent calls the colors indexes are shifted. That makes the brush seem to rotate.
After defining the colors, the program uses them to define the brush and it fills the rectangle on the screen.
Comments