示例#1
0
        private void Window_Activated_1(object sender, EventArgs e)
        {
            var pol1 = new MyPolygon(new List<Vector>() {
                new Vector(0,3),
                new Vector(3,6),
                new Vector(6,3),
                new Vector(3,0)});

            var pol2 = new MyPolygon(new List<Vector>() {
                new Vector(4,0),
                new Vector(15, 5),
                new Vector(15,1)});

            DrawPolygon(pol1, Colors.LightSalmon);
            DrawPolygon(pol2, Colors.HotPink);

            MyPolygon pol3;
            try
            {
                pol3 = MyPolygon.Intersection(pol1, pol2);
            }
            catch (DisjointPolygons)
            {
                return;
            }
            DrawPolygon(pol3, Colors.DarkBlue);
        }
示例#2
0
 public void DrawPolygon(MyPolygon poly, Color fill)
 {
     var polygon = new Polygon();
     polygon.Points = new PointCollection(poly.Points.Select(a => new Point(Math.Round(a.X * step + canvasSize / 2),
                                                                            Math.Round(-a.Y * step + canvasSize / 2))));
     polygon.Stroke = Brushes.Black;
     polygon.Fill = new SolidColorBrush(fill);
     polygon.StrokeThickness = 1;
     canvas.Children.Add(polygon);            
 }
示例#3
0
 public static MyPolygon Intersection(MyPolygon a, MyPolygon b)
 {
     b.points.Add(b.points.First());
     for (int i = 0; i < b.points.Count - 1; i++)
     {
         a = Intersection(a, new Segment(b.points[i], b.points[i + 1]));
         if (a.Points.Count == 0)
             throw new DisjointPolygons();
     }
     return a; 
 }
示例#4
0
 public static MyPolygon Intersection(MyPolygon a, Segment b)
 {
     List<Vector> result = new List<Vector>();
     a.points.Add(a.points.First());
     for (int i = 0; i < a.points.Count - 1; i++)
     {
         Vector start = a.points[i];
         Vector end = a.points[i + 1];
         bool s = b.PlaceOfPoint(start) <= 0; //справа
         bool e = b.PlaceOfPoint(end) <= 0; 
         if (s)
             result.Add(start);
         if (s ^ e)
             result.Add(b.CrossingPoint(new Segment(start, end)));
     }
     return new MyPolygon(result);
 }