SolidBrush brush = new SolidBrush(Color.LightGreen); //цвет кисти // Добавляем/удаляем точки private void pictureBox_MouseClick(object sender, MouseEventArgs e) { my_point p = null; foreach (my_point pp in points) { if (Math.Abs(pp.X - e.X) <= 3 && Math.Abs(pp.Y - e.Y) <= 3) { p = pp; } } // если нажата левая кнопка мыши, то добавляем if (e.Button == MouseButtons.Left) { my_point new_p = new my_point(e.X, e.Y); points.Add(new_p); g.FillEllipse(brush, new_p.X - 4, new_p.Y - 4, 8, 8); pictureBox.Image = bmp; return; } // иначе - удаляем else { points.Remove(p); redrawImage(); } }
private my_point find_second_point(List <my_point> list, my_point fst_point) { my_point res = fst_point; double min_deg = 180; foreach (my_point p in list) { double deg = degree_between_edges(new edge(fst_point, new my_point(fst_point.X, fst_point.Y - 1)), new edge(fst_point, p)); if (deg < min_deg) { min_deg = deg; res = p; } } return(res); }
// Определяет c какой стороны относительно направленного ребра лежит точка // ed - ребро // pt - точка // 1 - слева, -1 - справа, 0 - на ребре private int pos_rel_edge(edge ed, my_point pt) { double z = (ed.P2.Y - ed.P1.Y) * pt.X + (ed.P1.X - ed.P2.X) * pt.Y + (ed.P1.X * (ed.P1.Y - ed.P2.Y) + ed.P1.Y * (ed.P2.X - ed.P1.X)); if (z < 0) { return(-1); } else if (z > 0) { return(1); } else { return(0); } }
private my_point find_first_point(List <my_point> list) { my_point res = list[0]; int x = pictureBox.Width, y = pictureBox.Height; foreach (my_point p in list) { if (p.X < x) { x = p.X; res = p; } else if (p.X == x) { if (p.Y > y) { y = p.Y; res = p; } } } return(res); }
public Vertex(my_point l) { x = l.X; y = l.Y; }
public triad(my_point aa, my_point bb, my_point cc) { a = aa; b = bb; c = cc; find_coord_and_radius(); find_dist_to_center(); }
public bool contains(my_point p) { return(p == P1 || p == P2); }
public edge(my_point p1, my_point p2) { P1 = p1; P2 = p2; }
private void draw_point(my_point p) { g.FillEllipse(new SolidBrush(Color.Black), p.X - 4, p.Y - 4, 8, 8); }
private void butt_triangulator_Click(object sender, EventArgs e) { redrawImage(); living_edges = new List <edge>(); my_point fst_point = find_first_point(points); draw_point(fst_point); my_point sec_point = find_second_point(points, fst_point); draw_point(sec_point); pictureBox.Image = bmp; living_edges.Add(new edge(fst_point, sec_point)); while (living_edges.Count() > 0) { edge living = living_edges[living_edges.Count() - 1]; living_edges.RemoveAt(living_edges.Count() - 1); double radius = double.MaxValue; my_point third_point = null; foreach (my_point p in points) { draw_point(p); if (pos_rel_edge(living, p) != -1) // Определяет c какой стороны относительно направленного ребра лежит точка { continue; //1 - слева, -1 - справа, 0 - на ребре } triad tr = new triad(living.start(), living.end(), p); if (radius > tr.dist) { radius = tr.dist; third_point = p; } } draw_edge(living); if (third_point != null) { edge ed = new edge(living.start(), third_point); int ind = living_edges.FindIndex(new Predicate <edge>(ee => ee == new edge(ed.end(), ed.start()))); if (ind >= 0) { draw_edge(living_edges[ind]); living_edges.RemoveAt(ind); } else { living_edges.Add(ed); //draw_edge(ed); } ed = new edge(third_point, living.end()); ind = living_edges.FindIndex(new Predicate <edge>(ee => ee == new edge(ed.end(), ed.start()))); if (ind >= 0) { draw_edge(living_edges[ind]); living_edges.RemoveAt(ind); } else { living_edges.Add(ed); //draw_edge(ed); } } } }