using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApp13 { public partial class Form1 : Form { public List centers; public List ages; public static int R = 40; public static int GREEN_THRESHOLD = 750; public static int YELLOW_THRESHOLD = 900; public static int RED_THRESHOLD = 1200; System.Windows.Forms.Timer myTimer; System.Windows.Forms.Timer colorTimer; Random rng; public Form1() { InitializeComponent(); myTimer = new System.Windows.Forms.Timer(); // Interval set to 10 seconds myTimer.Interval = 1000; // measured in milliseconds myTimer.Tick += new EventHandler(AddAnotherCircle); myTimer.Enabled = true; colorTimer = new System.Windows.Forms.Timer(); // Interval set to 10 seconds colorTimer.Interval = 200; // measured in milliseconds colorTimer.Tick += new EventHandler(RefreshColors); colorTimer.Enabled = true; rng = new Random(); centers = new List(); ages = new List(); } public void RefreshColors(object sender, EventArgs args) { Canvas.Refresh(); } public void AddAnotherCircle(object sender, EventArgs args) { if (centers.Count >= 20) return; Boolean candidateViable = true; Point slacker; do { slacker = new Point(rng.Next(Canvas.Width - (R * 2)) + R, rng.Next(Canvas.Height - (R * 2)) + R); candidateViable = true; foreach (Point i in centers.ToList()) { if (Math.Sqrt(((slacker.X - i.X) * (slacker.X - i.X)) + ((slacker.Y - i.Y) * (slacker.Y - i.Y))) < (R * 2)) // Bingo! { candidateViable = false; break; } } } while (!candidateViable); centers.Add(slacker); ages.Add(new System.Diagnostics.Stopwatch()); ages[ages.Count - 1].Start(); myTimer.Interval = rng.Next(3500) + 1000; Canvas.Refresh(); // This directs us to the Paint event } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { myTimer.Dispose(); } private void Canvas_Paint(object sender, PaintEventArgs e) { Graphics g = e.Graphics; Brush myBrush; int index = 0; foreach (Point i in centers.ToList()) { if (ages[index].ElapsedMilliseconds < GREEN_THRESHOLD) myBrush = new SolidBrush(Color.Green); else if (ages[index].ElapsedMilliseconds > GREEN_THRESHOLD && ages[index].ElapsedMilliseconds < YELLOW_THRESHOLD) myBrush = new SolidBrush(Color.Yellow); else // It's red myBrush = new SolidBrush(Color.Red); g.FillCircle(myBrush, i.X, i.Y, R); index++; myBrush.Dispose(); } } private void Canvas_MouseDown(object sender, MouseEventArgs e) { int index = 0; foreach (Point i in centers.ToList()) { // (x - h)^2 + (y - k)^2 < R^2 // Where (x, y) == where I clicked // and (h, k) == the center of the circle if ( ( ((e.X - i.X) * (e.X - i.X)) + ((e.Y - i.Y) * (e.Y - i.Y)) ) < (R * R) ) // Bingo! { centers.Remove(i); ages.RemoveAt(index); break; } index++; } Canvas.Refresh(); } } public static class GraphicsExtensions { public static void DrawCircle(this Graphics g, Pen pen, float centerX, float centerY, float radius) { g.DrawEllipse(pen, centerX - radius, centerY - radius, radius + radius, radius + radius); } public static void FillCircle(this Graphics g, Brush brush, float centerX, float centerY, float radius) { g.FillEllipse(brush, centerX - radius, centerY - radius, radius + radius, radius + radius); } } }