Fill a shape drawn by the user with random lines in C#

// The points selected by the user.The MouseDown event handler starts the process. It saves the first point in the ShapePoints list and sets IsDrawing to true. The MouseMove event handler adds a new point to ShapePoints if IsDrawing is true. The MouseUp event handler calls GenerateLines if IsDrawing is true. The following code shows how GenerateLines works.
private ListShapePoints = new List ();
private GraphicsPath ShapePath = null;
private GraphicsPath LinesPath = null;
private bool IsDrawing = false;
// Start drawing.
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
ShapePoints = new List();
ShapePoints.Add(e.Location);
IsDrawing = true;
Refresh();
}
// Continue drawing.
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (!IsDrawing) return;
ShapePoints.Add(e.Location);
Refresh();
}
// Finish drawing.
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
if (!IsDrawing) return;
IsDrawing = false;
// Generate the random lines to fill the shape.
GenerateLines();
Refresh();
}
// Generate the random lines to fill the shape.This code creates a new GraphicsPath object named ShapePath to represent the shape drawn by the user. It gets the bounds of the path. It then generates random line segments connecting the bounding rectangle's left and right edges, and connecting the bounding rectangle's top and bottom edges. The following code shows how the program draws the filled shape.
private void GenerateLines()
{
if (ShapePoints.Count < 3)
{
ShapePath = null;
LinesPath = null;
return;
}
// Make the shape's path.
ShapePath = new GraphicsPath();
ShapePath.AddPolygon(ShapePoints.ToArray());
// Get the shape's bounds.
RectangleF bounds = ShapePath.GetBounds();
int xmin = (int)(bounds.Left);
int xmax = (int)(bounds.Right) + 1;
int ymin = (int)(bounds.Top);
int ymax = (int)(bounds.Bottom) + 1;
// Generate random lines.
LinesPath = new GraphicsPath();
int num_lines = (int)((bounds.Width + bounds.Height) / 16);
Random rand = new Random();
int x1, y1, x2, y2;
for (int i = 1; i <= num_lines; i++)
{
x1 = rand.Next(xmin, xmax);
y1 = ymin;
x2 = rand.Next(xmin, xmax);
y2 = ymax;
LinesPath.AddLine(x1, y1, x2, y2);
x1 = xmin;
y1 = rand.Next(ymin, ymax);
x2 = xmax;
y2 = rand.Next(ymin, ymax);
LinesPath.AddLine(x1, y1, x2, y2);
}
}
// Draw the shape.If the user is currently drawing, the program draws the shape so far as a series of lines. If the user is not currently drawing, the program fills the shape's path and outlines it. Next it sets the Graphics object's clipping region to the path the user drew. It then draws the lines, which are clipped by the clipping region.
private void Form1_Paint(object sender, PaintEventArgs e)
{
// Draw the shape.
if (IsDrawing)
{
// Draw the lines so far.
if (ShapePoints.Count > 1)
{
e.Graphics.DrawLines(Pens.Green, ShapePoints.ToArray());
}
}
else
{
// Fill and outline the finished shape.
if (ShapePath != null)
{
e.Graphics.FillPath(Brushes.LightGreen, ShapePath);
e.Graphics.DrawPath(Pens.Green, ShapePath);
// Fill with the lines.
e.Graphics.Clip = new Region(ShapePath);
e.Graphics.DrawPath(Pens.Green, LinesPath);
}
}
}


-->
Comments