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 proj { public partial class Form1 : Form { class Figure { public PointF p1; public float R; public Figure(PointF p1) { this.p1 = p1; } } private int mousestillclick = 0; private bool initialized = false; private List
figures = new List
(); private int figurescount = 0; private PointF mousea; private PointF start; private PointF[] MaxChord = new PointF[2]; private double MaxChordLength = 0; Pen pen = new Pen(Color.Red); public Form1() { InitializeComponent(); } private void Form1_MouseDown(object sender, MouseEventArgs e) { initialized = true; start = new PointF(e.X, e.Y); mousestillclick = 1; } private void Form1_MouseMove(object sender, MouseEventArgs e) { mousea = new PointF(e.X, e.Y); if (mousestillclick == 1) Refresh(); } private void Form1_MouseUp(object sender, MouseEventArgs e) { mousestillclick = 0; Refresh(); } private void Form1_Paint_1(object sender, PaintEventArgs e) { if (!initialized) return; Font font = new Font("Times New Roman", 14); Brush brush = new SolidBrush(Color.Black); if (mousestillclick == 1) { float sq1 = (mousea.X - start.X) * (mousea.X - start.X) + (mousea.Y - start.Y) * (mousea.Y - start.Y); e.Graphics.DrawEllipse(pen, start.X - (float)Math.Sqrt(sq1), start.Y - (float)Math.Sqrt(sq1), 2 * (float)Math.Sqrt(sq1), 2 * (float)Math.Sqrt(sq1)); if (figurescount != 0) for (int i = 0; i < figurescount; i++) e.Graphics.DrawEllipse(pen, figures[i].p1.X - figures[i].R, figures[i].p1.Y - figures[i].R, 2 * figures[i].R, 2 * figures[i].R); } else { float sq1 = (mousea.X - start.X) * (mousea.X - start.X) + (mousea.Y - start.Y) * (mousea.Y - start.Y); figures.Add(new Figure(new PointF(start.X, start.Y))); figurescount++; figures[figurescount - 1].R = (float)Math.Sqrt(sq1); if (figurescount != 0) for (int i = 0; i < figurescount; i++) { e.Graphics.DrawEllipse(pen, figures[i].p1.X - figures[i].R, figures[i].p1.Y - figures[i].R, 2 * figures[i].R, 2 * figures[i].R); for (int j = 0; j < figurescount; j++) { if (j == i) continue; float x1 = figures[i].p1.X; float x2 = figures[j].p1.X; float y1 = figures[i].p1.Y; float y2 = figures[j].p1.Y; float dis = (float)Math.Sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)); float R0 = figures[i].R; float R1 = figures[j].R; if ((dis > R0 + R1) || (dis < Math.Abs(R0 - R1))) continue; float a = (R0 * R0 - R1 * R1 + dis * dis) / (2 * dis); float h = (float)Math.Sqrt(R0 * R0 - a * a); float P2x = x1 + (a * (x2 - x1) / dis); float P2y = y1 + (a * (y2 - y1) / dis); float IP1x = P2x + (h * (y2 - y1) / dis); float IP2x = P2x - (h * (y2 - y1) / dis); float IP1y = P2y - (h * (x2 - x1) / dis); float IP2y = P2y + (h * (x2 - x1) / dis); float chordlength = (float)Math.Sqrt((IP2x - IP1x) * (IP2x - IP1x) + (IP2y - IP1y) * (IP2y - IP1y)); if (chordlength > MaxChordLength) { MaxChordLength = chordlength; MaxChord[0].X = IP1x; MaxChord[1].X = IP2x; MaxChord[0].Y = IP1y; MaxChord[1].Y = IP2y; } } } } string maxrest = "Max = " + MaxChordLength; e.Graphics.DrawString(maxrest, font, brush, 600, 70); e.Graphics.DrawLine(new Pen(Color.Blue), MaxChord[0],MaxChord[1]); } } }