/// <summary> /// 重画所有面 /// </summary> void ReDraw() { pictureBox1.Invalidate(); pictureBox1.Update(); Graphics g = pictureBox1.CreateGraphics(); LongtitudeCircle tempCirle = new LongtitudeCircle(); for (int i = 0; i < listOfCircle.Count; i++) { if (dataGridViewX1.Rows[i].Cells[0].Value.ToString().Substring(0, 1) == "A") { DrawPlanes(out tempCirle, g, dataGridViewX1.Rows[i], new Pen(Color.MidnightBlue, 1)); } else { DrawPlanes(out tempCirle, g, dataGridViewX1.Rows[i], new Pen(Color.HotPink, 1)); } } if (listOfPoint.Count > 0) { int pointR = 3; for (int j = 0; j < listOfPoint.Count; j++) { g.FillEllipse(Brushes.Black, (float)(listOfPoint[j].X + 148 - pointR), (float)(listOfPoint[j].Y + 150 - pointR), (float)(pointR * 2), (float)(pointR * 2)); // g.DrawLine } } g.DrawArc(new Pen(Color.MidnightBlue, 3), 18, 20, 260, 260, 0, 360); }
/// <summary> /// 求交点 /// /// </summary> /// <param name="c1">第一个圆</param> /// <param name="c2">第二个圆</param> /// <returns></returns> Point FindIntersection(LongtitudeCircle c1, LongtitudeCircle c2) { Point p1, p2; double x1 = c1.X0, y1 = c1.Y0, r1 = c1.R; double x2 = c2.X0, y2 = c2.Y0, r2 = c2.R; double k1 = 2 * (y1 - y2), k2 = 2 * (x1 - x2), M = x1 * x1 - x2 * x2 - (y1 - y2) * (y1 - y2) - r1 * r1 + r2 * r2; double a = k1 * k1 + k2 * k2, b = -2 * (x1 * k1 * k1 + k2 * M), c = k1 * k1 * x1 * x1 + M * M - r1 * r1 * k1 * k1; p1 = new Point((-b + Math.Sqrt(b * b - 4 * a * c)) / (2 * a), 0); p2 = new Point((-b - Math.Sqrt(b * b - 4 * a * c)) / (2 * a), 0); // 1、 y1 不可能等于 y2, x1 也不可等于x2 // 2、 只可能有一个交点 p1.Y = -(r1 * r1 - r2 * r2 - x1 * x1 + x2 * x2 - y1 * y1 + y2 * y2 + 2 * p1.X * (x1 - x2)) / 2 / (y1 - y2); p2.Y = -(r1 * r1 - r2 * r2 - x1 * x1 + x2 * x2 - y1 * y1 + y2 * y2 + 2 * p2.X * (x1 - x2)) / 2 / (y1 - y2); p1 = new Point(p1.X, p1.Y); p2 = new Point(p2.X, p2.Y); if (IsInCircle(p1)) { return(p1); } else if (IsInCircle(p2)) { return(p2); } else { return(null); } }
/// <summary> /// 选择产状信息,用于编辑 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void dataGridViewX1_SelectionChanged(object sender, EventArgs e) { ReDraw(); if (dataGridViewX1.SelectedRows.Count > 0) { if (dataGridViewX1.SelectedRows[0].Cells[0].Value != null) { Graphics g = pictureBox1.CreateGraphics(); LongtitudeCircle tempCircle = new LongtitudeCircle(); if (dataGridViewX1.SelectedRows[0].Cells[0].Value.ToString().Substring(0, 1) == "A") { typeOfArc = buttonX2.Name; highlighter1.SetHighlightColor(buttonX2, DevComponents.DotNetBar.Validator.eHighlightColor.Orange); highlighter1.SetHighlightColor(buttonX3, DevComponents.DotNetBar.Validator.eHighlightColor.None); textBoxX5.Text = dataGridViewX1.SelectedRows[0].Cells[1].Value.ToString(); textBoxX4.Text = dataGridViewX1.SelectedRows[0].Cells[2].Value.ToString(); DrawPlanes(out tempCircle, g, dataGridViewX1.SelectedRows[0], new Pen(Color.Gold, 2)); g.DrawArc(new Pen(Color.MidnightBlue, 3), 18, 20, 260, 260, 0, 360); } else if (dataGridViewX1.SelectedRows[0].Cells[0].Value.ToString().Substring(0, 1) == "B") { typeOfArc = buttonX3.Name; highlighter1.SetHighlightColor(buttonX3, DevComponents.DotNetBar.Validator.eHighlightColor.Orange); highlighter1.SetHighlightColor(buttonX2, DevComponents.DotNetBar.Validator.eHighlightColor.None); textBoxX5.Text = dataGridViewX1.SelectedRows[0].Cells[1].Value.ToString(); textBoxX4.Text = dataGridViewX1.SelectedRows[0].Cells[2].Value.ToString(); DrawPlanes(out tempCircle, g, dataGridViewX1.SelectedRows[0], new Pen(Color.Gold, 2)); g.DrawArc(new Pen(Color.MidnightBlue, 3), 18, 20, 260, 260, 0, 360); } else if (dataGridViewX1.SelectedRows[0].Cells[0].Value.ToString().Substring(0, 1) == "L") { int pointR = 4; for (int i = 0; i < listOfPoint.Count; i++) { if (listOfPoint[i].Name == dataGridViewX1.SelectedRows[0].Cells[0].Value.ToString()) { g.FillEllipse(Brushes.OliveDrab, (float)(listOfPoint[i].X + 148 - pointR), (float)(listOfPoint[i].Y + 150 - pointR), (float)(pointR * 2), (float)(pointR * 2)); } } } tempPlane = dataGridViewX1.SelectedRows[0]; tempType = typeOfArc; } else { dataGridViewX1.ClearSelection(); } } }
/// <summary> /// 画 面 /// </summary> void DrawPlanes(out LongtitudeCircle newCircle, Graphics g, DataGridViewRow row, Pen p) { string name = row.Cells[0].Value.ToString(); double trend = double.Parse(row.Cells[1].Value.ToString()); trend = trend * Math.PI / 180; double dip = double.Parse(row.Cells[2].Value.ToString()); dip = dip * Math.PI / 180; Point newp1 = new Point(-R * Math.Cos(trend), -R * Math.Sin(trend)); Point newp2 = new Point(R * Math.Cos(trend), R * Math.Sin(trend)); double d = R * Math.Tan(Math.PI / 4 - dip / 2); // Point newp3 = new Point(R * Math.Sin(trend) / (Math.Tan(dip) + 1), -R * Math.Cos(trend) / (Math.Tan(dip) + 1)); Point newp3 = new Point(d * Math.Sin(trend), -d * Math.Cos(trend)); double x0, y0, r; // 三点定圆 CenterAndR(out x0, out y0, out r, newp1, newp2, newp3); // 存入经向大圆临时列表 newCircle = new LongtitudeCircle(name, x0, y0, r); // listOfCircle.Add(new LongtitudeCircle(name, x0, y0, r)); double theta = Math.Asin(R / r); theta = theta * 180 / Math.PI; // 大圆对应的圆心角的一半 double gama = Math.Atan(Math.Sqrt(y0 * y0 / x0 / x0)); gama = gama * 180 / Math.PI; // 画大圆弧 if (trend <= Math.PI / 2) { g.DrawArc(p, (float)(148 + x0 - r), (float)(150 + y0 - r), (float)(2 * r), (float)(2 * r), (float)(-theta - gama), (float)(2 * theta)); } else if (trend > Math.PI / 2 && trend <= Math.PI) { g.DrawArc(p, (float)(148 + x0 - r), (float)(150 + y0 - r), (float)(2 * r), (float)(2 * r), (float)(gama - theta), (float)(2 * theta)); } else if (trend > Math.PI && trend <= Math.PI * 1.5) { g.DrawArc(p, (float)(148 + x0 - r), (float)(150 + y0 - r), (float)(2 * r), (float)(2 * r), (float)((180 - gama) - theta), (float)(2 * theta)); } else { g.DrawArc(p, (float)(148 + x0 - r), (float)(150 + y0 - r), (float)(2 * r), (float)(2 * r), (float)(gama + 180 - theta), (float)(2 * theta)); } }
/// <summary> /// 判断是否有交点 /// /// </summary> /// <param name="c1"></param> /// <param name="c2"></param> /// <returns></returns> bool HaveIntersection(LongtitudeCircle c1, LongtitudeCircle c2) { double x1 = c1.X0, y1 = c1.Y0, r1 = c1.R; double x2 = c2.X0, y2 = c2.Y0, r2 = c2.R; double k1 = 2 * (y1 - y2), k2 = 2 * (x1 - x2), M = x1 * x1 - x2 * x2 - (y1 - y2) * (y1 - y2) - r1 * r1 + r2 * r2; double a = k1 * k1 + k2 * k2, b = -2 * (x1 * k1 * k1 + k2 * M), c = k1 * k1 * x1 * x1 + M * M - r1 * r1 * k1 * k1; if (b * b - 4 * a * c <= 0) { return(false); } else { return(true); } }