I'm writing dijkstra algorithm ,i created a class called circle that it has property
public List<Circle> Circles = new List<Circle>();
but it values are duplicate it values initialize with each MouseDoubleClick . for example : properties values Name,Location and selected are duplicate.
in event pictureBox1_MouseDoubleClick
i save all of properties related to circle but As I said values are duplicate.i mean is this line circle.Circles.Add(circle);
public class Circle
{
public List<Circle> Circles = new List<Circle>();
public List<Rectangle> CircleShape = new List<Rectangle>();
public string Name { get; set; }
public Size size = new Size(25, 25);
public Color normalFillColor = Color.White;
public Color selectedFillColor = Color.Red;
public Color borderColor = Color.Gray;
public int borderWith = 2;
// public int Diameter { get; set; }
public Point Location { get; set; }
public bool Selected { get; set; }
public Rectangle Bounds
{
get
{
return new Rectangle(Location, size);
}
}
public void HitTest(Point p)
{
//var result = false;
for (int i = 0; i < CircleShape.Count; i++)
{
using (var path = new GraphicsPath())
{
path.AddEllipse(CircleShape[i]);
if (path.IsVisible(p))
{
Circles[i].Selected = true;
}
}
}
}
private Font font = new Font("Tahoma", 8, FontStyle.Bold);
public void Draw(Graphics g)
{
for (int i = 0; i < CircleShape.Count; i++)
{
using (var brush = new SolidBrush(Circles[i].Selected ? selectedFillColor : normalFillColor))
g.FillEllipse(brush, CircleShape[i]);
using (var pen = new Pen(borderColor, 2))
g.DrawEllipse(pen, CircleShape[i]);
TextRenderer.DrawText(g, i.ToString(), font,
CircleShape[i], Color.Black,
TextFormatFlags.VerticalCenter | TextFormatFlags.HorizontalCenter);
}
}
}
namespace Dijkstra_s_algorithm
{
public partial class Form1 : Form
{
private List<Rectangle> Shapes = new List<Rectangle>();
private Circle circle = new Circle();
int number = 0;
public int Count { get { return number; } set { number = value; } }
public Form1()
{
InitializeComponent();
pictureBox1.Paint += new PaintEventHandler(pic_Paint);
}
private void pic_Paint(object sender, PaintEventArgs e)
{
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
Graphics g = e.Graphics;
circle.Draw(g);
}
private void pictureBox1_MouseDoubleClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
circle.Name = Count.ToString();
Location.Offset(-circle.size.Width / 2, -circle.size.Height / 2);
circle.Location = e.Location;
circle.CircleShape.Add(new Rectangle(circle.Location, circle.size));
pictureBox1.Invalidate();
}
circle.Circles.Add(circle);
Count++;
}
private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
if (ModifierKeys != Keys.Control && e.Button != MouseButtons.Right)
{
return;
}
else
{
circle.HitTest(e.Location);
}
pictureBox1.Invalidate();
}
}
}