C#-Codeusing System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace count_area_train_your_programmer
{
public partial class Form1 : Form
{
int counter = 0;
static Random rnd = new Random((int)DateTime.Now.Ticks);
int lines = 1;
int area_count = 0;
int width = 0;
int height = 0;
Color pencolor = Color.Red;
//-------------------------------------
public Form1()
{
InitializeComponent();
pictureBox_canvas.Image = new Bitmap(pictureBox_canvas.Width, pictureBox_canvas.Height);
width = pictureBox_canvas.Width;
height = pictureBox_canvas.Height;
}
//-------------------------------------
private void button_draw_Click(object sender, EventArgs e)
{
area_count = 0;
Color color = new Color();
Point pos1 = new Point(0, 0);
Point pos2 = new Point(0, 0);
label_areacount.Text = "";
pictureBox_canvas.Image.Dispose();
pictureBox_canvas.Image = new Bitmap(pictureBox_canvas.Width, pictureBox_canvas.Height);
pictureBox_canvas.Refresh();
Pen pen = new Pen(pencolor, 2);
Image image = pictureBox_canvas.Image;
for (int i = 0; i < lines; i++)
{
Get_random_Points(ref pos1);
Get_random_Points(ref pos2);
while (pos1.X == pos2.X || pos1.Y == pos2.Y)
{
Get_random_Points(ref pos2);
}
using (Graphics grahpics = Graphics.FromImage(image))
{
grahpics.DrawLine(pen, pos1, pos2);
}
pictureBox_canvas.Image = image;
}
for (int i = 0; i < width-1; i++)
{
for (int f = 0; f < height-1; f++)
{
color = GetColorAt(i, f);
if (color.A == 0)
{
Check_pixels(i, f);
}
}
}
label_areacount.Text = area_count.ToString();
}
//-------------------------------------
public void Get_random_Points(ref Point pos)
{
counter = rnd.Next(0, 4);
switch (counter)
{
case 0:
pos.X = rnd.Next(0, width+1);
pos.Y = -1;
break;
case 1:
pos.X = rnd.Next(0, width+1);
pos.Y = height+1;
break;
case 2:
pos.Y = rnd.Next(0, height+1);
pos.X = -1;
break;
case 3:
pos.Y = rnd.Next(0, height+1);
pos.X = width+1;
break;
}
}
//-------------------------------------
public Color GetColorAt(int x, int y)
{
return ((Bitmap)pictureBox_canvas.Image).GetPixel(x, y);
}
//-------------------------------------
public void Check_pixels(int x, int y)
{
Color color = new Color();
Image image = pictureBox_canvas.Image;
List<Point> points = new List<Point>();
points.Add(new Point(x, y));
area_count++;
while (points.Count != 0)
{
foreach(Point pnt in points)
{
color = GetColorAt(pnt.X, pnt.Y);
if (color.A == 0)
{
using (Graphics graph = Graphics.FromImage(image))
{
Brush aBrush = (Brush)Brushes.Black;
graph.FillRectangle(aBrush, pnt.X, pnt.Y, 1, 1);
}
}
}
Direction_Check(points, color, -1, 0);
Direction_Check(points, color, 1, 0);
Direction_Check(points, color, 0, 1);
Direction_Check(points, color, 0, -1);
points.RemoveAt(0);
}
pictureBox_canvas.Image = image;
}
//-------------------------------------
public void Direction_Check(List<Point>points, Color color, int to_x, int to_y)
{
if (points[0].X + to_x >= 0 && points[0].Y + to_y >= 0 &&
points[0].X + to_x <= width-1 && points[0].Y + to_y <= height-1)
{
color = GetColorAt(points[0].X + to_x, points[0].Y + to_y);
if (color.A == 0 && color.R == 0 && color.G == 0 && color.B == 0)
{
points.Add(new Point(points[0].X + to_x, points[0].Y + to_y));
}
}
}
//-------------------------------------
private void textBox_linecount_TextChanged(object sender, EventArgs e)
{
int.TryParse(textBox_linecount.Text, out lines);
label_linecount.Text = lines.ToString();
}
}
}
Admin: 0