예제 #1
0
파일: Triangle.cs 프로젝트: am1guma/BR
 public Triangle(Point a, Point b, Point c)
 {
     A = a;
     B = b;
     C = c;
     S1 = new Segment(a, b);
     S2 = new Segment(b, c);
     S3 = new Segment(a, c);
     Points = new List<Point> {a, b, c};
 }
예제 #2
0
 public static bool CheckIfASegmentIntersectsAnotherSegment(Segment s)
 {
     var ls = SegmentsManager.Segments;
     foreach (var seg in ls.Where(seg => !seg.Egale(s)))
     {
         VerificaIntersectia = new Intersectie(seg.A, seg.B, s.A, s.B);
         if (VerificaIntersectia.Intersectation())
             return false;
     }
     return true;
 }
예제 #3
0
 public static bool CheckIfSegmentIntersectTwoPointsFromAnInternalShape(Segment s)
 {
     for (var i = 1; i < ListOfPolygons.Count; i++)
     {
         var lp = GetAllPolygonPoints(ListOfPolygons[i]);
         if (lp.Contains(s.A) && lp.Contains(s.B))
         {
             if (!SegmentsManager.CheckIfSegmentAlreadyExists(s))
             {
                 return true;
             }
         }
     }
     return false;
 }
예제 #4
0
파일: Form1.cs 프로젝트: am1guma/BR
        private void b3_Click(object sender, EventArgs e)
        {
            try
            {
                _id = "b3";
                b3.Checked = true;
                b1.Checked = false;
                b2.Checked = false;
                b4.Checked = false;
                b5.Checked = false;

                if (_spDefined && _epDefined)
                {
                    PolygonManager.ListOfPolygons = _polygons;
                    SegmentsManager.Segments = PolygonManager.GetAllPolygonsSegments();
                    var allPoints = PolygonManager.GetAllPoints();
                    for (var i = 0; i < allPoints.Count; i++)
                    {
                        for (var j = 0; j < allPoints.Count; j++)
                        {
                            if (i != j)
                            {
                                var s = new Segment(allPoints[i], allPoints[j]);
                                if (!SegmentsManager.CheckIfSegmentAlreadyExists(s) &&
                                    IntersectieManager.CheckIfASegmentIntersectsAnotherSegment(s) &&
                                    !PolygonManager.CheckIfSegmentIntersectTwoPointsFromAnInternalShape(s))
                                {
                                    _g.DrawLine(new Pen(Color.Black), allPoints[i], allPoints[j]);
                                    SegmentsManager.Segments.Add(s);
                                }
                            }
                        }
                    }
                    DefineTriangles();
                    CreateGraph();
                    FindTheShortestPath();
                }
                else
                {
                    MessageBox.Show(@"StartPoint and EndPoint are not defined!");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
예제 #5
0
 public static bool CheckIfSegmentIntersectInteriorSegment(Segment s)
 {
     foreach (var seg in SegmentsManager.InteriorSegments)
     {
         if (!s.A.X.Equals(seg.A.X) && !s.A.X.Equals(seg.B.X) && !s.B.X.Equals(seg.A.X) && !s.B.X.Equals(seg.B.X))
         {
             VerificaIntersectia = new Intersectie(seg.A, seg.B, s.A, s.B);
             if (VerificaIntersectia.Intersectation())
             {
                 var pt = new Point(Convert.ToInt32(VerificaIntersectia.x), Convert.ToInt32(VerificaIntersectia.y));
                 if (!(pt.Equals(s.A) || pt.Equals(s.B)))
                     return true;
             }
         }
     }
     return false;
 }
예제 #6
0
파일: Form1.cs 프로젝트: am1guma/BR
        private void b4_Click(object sender, EventArgs e)
        {
            try
            {
                _id = "b4";
                b4.Checked = true;
                b1.Checked = false;
                b2.Checked = false;
                b3.Checked = false;
                b5.Checked = false;

                Pen greenPen = new Pen(Color.Purple, 3);

                if (_spDefined && _epDefined)
                {

                    PolygonManager.ListOfPolygons = _polygons.Where(p => !p.Equals(_polygons.First())).ToList();
                    PolygonManager.Contur = _polygons.First();
                    SegmentsManager.Segments = PolygonManager.GetAllPolygonsSegments();
                    var allPoints = PolygonManager.GetAllPoints();
                    SegmentsManager.InteriorSegments = new List<Segment>();
                    PolygonManager.ListOfMiddlePoints = new List<Point>();
                    NodManager.Noduri = new List<Nod>();
                    DrumManager.Drumuri = new List<Drum>();

                    NodManager.Noduri.Add(new Nod(Id, _startPoint));
                    Id++;
                    NodManager.Noduri.Add(new Nod(Id, _endPoint));
                    Id++;

                    for (var i = 0; i < allPoints.Count; i++)
                    {
                        var ySus = new Point(allPoints[i].X, allPoints[i].Y - 1);
                        var yJos = new Point(allPoints[i].X, allPoints[i].Y + 1);
                        if (IntersectieManager.CanDraw(allPoints[i], ySus))
                        {
                            var sus = new Segment(allPoints[i], new Point(allPoints[i].X, 0));
                            var ptSus = IntersectieManager.GetLastIntersectionPoint(sus);
                            var segSus = new Segment(allPoints[i], ptSus);
                            _g.DrawLine(new Pen(Color.Black), segSus.A, segSus.B);
                            SegmentsManager.InteriorSegments.Add(segSus);
                        }
                        if (IntersectieManager.CanDraw(allPoints[i], yJos))
                        {
                            var jos = new Segment(allPoints[i], new Point(allPoints[i].X, pictureBox1.Height));
                            var ptJos = IntersectieManager.GetFirstIntersectionPoint(jos);
                            var segJos = new Segment(allPoints[i], ptJos);
                            _g.DrawLine(new Pen(Color.Black), segJos.A, segJos.B);
                            SegmentsManager.InteriorSegments.Add(segJos);
                        }
                    }

                    foreach (var s in SegmentsManager.InteriorSegments)
                    {
                        _g.FillEllipse(new SolidBrush(Color.Blue), s.GetTheMiddlePoint().X - 6,
                            s.GetTheMiddlePoint().Y - 6, 12, 12);
                        PolygonManager.ListOfMiddlePoints.Add(new Point(s.GetTheMiddlePoint().X, s.GetTheMiddlePoint().Y));
                        NodManager.Noduri.Add(new Nod(Id, new Point(s.GetTheMiddlePoint().X, s.GetTheMiddlePoint().Y)));
                        Id++;
                    }

                    var newList = PolygonManager.ListOfMiddlePoints.OrderBy(p => p.X);
                    var max = newList.Last();

                    double lungimeMinimaStart = 10000;
                    double lungimeMinimaEnd = 10000;

                    var SegToBeDrawStart = new Segment(new Point(0, 0), new Point(0, 0));
                    var SegToBeDrawEnd = new Segment(new Point(0, 0), new Point(0, 0));

                    foreach (var p in newList)
                    {
                        var aux = p;
                        var notDraw = true;
                        var cnt = 0;
                        if (!p.X.Equals(max.X))
                        {
                            var lf = new List<int>();
                            while (notDraw)
                            {
                                var tempPoint =
                                    newList.FirstOrDefault(
                                        pt =>
                                            (pt.X >= aux.X && !pt.Y.Equals(aux.Y) && !pt.X.Equals(p.X) &&
                                             !lf.Contains(pt.Y)));
                                lf.Add(tempPoint.Y);
                                aux = tempPoint;

                                var tp = newList.Where(ptr => ptr.X == tempPoint.X).ToList();
                                if (tp.Count() == 1)
                                {
                                    var seg = new Segment(p, tempPoint);
                                    if (!IntersectieManager.CheckIfTwoPointsIntersectAShapeTrapez(seg.A, seg.B))
                                    {
                                        cnt++;
                                        _g.DrawLine(new Pen(Color.Blue), seg.A, seg.B);
                                        DrumManager.Drumuri.Add(new Drum(NodManager.GetNod(seg.A),
                                            NodManager.GetNod(seg.B), seg.GetLength()));
                                        notDraw = false;
                                    }
                                    if (tempPoint.Equals(max) && notDraw)
                                        notDraw = false;
                                }
                                else if (tp.Count() == 2)
                                {
                                    var seg1 = new Segment(p, tempPoint);
                                    var seg2 = new Segment(p, tp[1]);
                                    if (!IntersectieManager.CheckIfTwoPointsIntersectAShapeTrapez(seg1.A, seg1.B))
                                    {
                                        _g.DrawLine(new Pen(Color.Blue), seg1.A, seg1.B);
                                        DrumManager.Drumuri.Add(new Drum(NodManager.GetNod(seg1.A),
                                            NodManager.GetNod(seg1.B), seg1.GetLength()));
                                        notDraw = false;
                                    }
                                    if (tempPoint.Equals(max) && notDraw)
                                        notDraw = false;

                                    if (!IntersectieManager.CheckIfTwoPointsIntersectAShapeTrapez(seg2.A, seg2.B))
                                    {
                                        _g.DrawLine(new Pen(Color.Blue), seg2.A, seg2.B);
                                        DrumManager.Drumuri.Add(new Drum(NodManager.GetNod(seg2.A),
                                            NodManager.GetNod(seg2.B), seg2.GetLength()));
                                        notDraw = false;
                                    }
                                    if (tempPoint.Equals(max) && notDraw)
                                        notDraw = false;
                                }
                            }
                        }

                        var segment1 = new Segment(p, _startPoint);
                        if (!IntersectieManager.CheckIfTwoPointsIntersectAShapeTrapez(segment1.A, segment1.B))
                        {
                            if (segment1.GetLength() < lungimeMinimaStart)
                            {
                                lungimeMinimaStart = segment1.GetLength();
                                SegToBeDrawStart = segment1;
                            }
                        }

                        var segment2 = new Segment(p, _endPoint);
                        if (!IntersectieManager.CheckIfTwoPointsIntersectAShapeTrapez(segment2.A, segment2.B))
                        {
                            if (segment2.GetLength() < lungimeMinimaEnd)
                            {
                                lungimeMinimaEnd = segment2.GetLength();
                                SegToBeDrawEnd = segment2;
                            }
                        }
                    }
                    _g.DrawLine(new Pen(Color.Blue), SegToBeDrawStart.A, SegToBeDrawStart.B);
                    DrumManager.Drumuri.Add(new Drum(NodManager.GetNod(SegToBeDrawStart.A),
                        NodManager.GetNod(SegToBeDrawStart.B), SegToBeDrawStart.GetLength()));
                    var ptr1 = newList.FirstOrDefault(pz => pz.X == SegToBeDrawStart.A.X && pz.Y != SegToBeDrawStart.A.Y);
                    if (ptr1.X != 0 && ptr1.Y != 0)
                    {
                        var segz1 = new Segment(ptr1, _startPoint);
                        if (!IntersectieManager.CheckIfTwoPointsIntersectAShapeTrapez(segz1.A, segz1.B))
                        {
                            _g.DrawLine(new Pen(Color.Blue), segz1.A, segz1.B);
                            DrumManager.Drumuri.Add(new Drum(NodManager.GetNod(segz1.A), NodManager.GetNod(segz1.B),
                                segz1.GetLength()));
                        }
                    }

                    _g.DrawLine(new Pen(Color.Blue), SegToBeDrawEnd.A, SegToBeDrawEnd.B);
                    DrumManager.Drumuri.Add(new Drum(NodManager.GetNod(SegToBeDrawEnd.A),
                        NodManager.GetNod(SegToBeDrawEnd.B), SegToBeDrawEnd.GetLength()));
                    var ptr2 = newList.FirstOrDefault(pz => pz.X == SegToBeDrawEnd.A.X && pz.Y != SegToBeDrawEnd.A.Y);
                    if (ptr2.X != 0 && ptr2.Y != 0)
                    {
                        var segz2 = new Segment(ptr2, _endPoint);
                        if (!IntersectieManager.CheckIfTwoPointsIntersectAShapeTrapez(segz2.A, segz2.B))
                        {
                            _g.DrawLine(new Pen(Color.Blue), segz2.A, segz2.B);
                            DrumManager.Drumuri.Add(new Drum(NodManager.GetNod(segz2.A), NodManager.GetNod(segz2.B),
                                segz2.GetLength()));
                        }
                    }

                    var aFostGasit = false;

                    var primulSegment = new Segment(_startPoint, _endPoint);
                    if (!IntersectieManager.CheckIfTwoPointsIntersectAShapeTrapez(primulSegment.A, primulSegment.B))
                    {
                        _g.DrawLine(greenPen, primulSegment.A, primulSegment.B);
                        aFostGasit = true;
                    }

                    if (!aFostGasit)
                    {

                        var gr = new Graph();

                        foreach (var n in NodManager.Noduri)
                        {
                            var dict = new Dictionary<char, int>();
                            var nodesThatCanBeReached = NodManager.CheckWehereANodCanGo(n);
                            foreach (var nd in nodesThatCanBeReached)
                            {
                                dict.Add(nd.Id, Convert.ToInt32(new Segment(n.P, nd.P).GetLength()));
                            }
                            gr.add_vertex(n.Id, dict);
                        }

                        var ls = gr.shortest_path(NodManager.Noduri[0].Id, NodManager.Noduri[1].Id);
                        if (ls.Count > 0)
                        {
                            _g.DrawLine(greenPen, NodManager.Noduri[0].P, NodManager.GetNodWithId(ls[ls.Count - 1]).P);
                            for (var i = 0; i < ls.Count() - 1; i++)
                            {
                                var n = NodManager.GetNodWithId(ls[i]);
                                var n2 = NodManager.GetNodWithId(ls[i + 1]);
                                _g.DrawLine(greenPen, n.P, n2.P);
                            }
                        }
                    }

                }
                else
                {
                    MessageBox.Show(@"StartPoint and EndPoint are not defined!");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
예제 #7
0
 public static bool CheckIfTwoPointsIntersectOnlyASpecificSegment(Point a, Point b, Segment s)
 {
     Segment s1 = new Segment(a,b);
     var ls = SegmentsManager.Segments;
     foreach (var seg in ls.Where(seg => !seg.Egale(s1)))
     {
         if (!seg.Egale(s))
         {
             VerificaIntersectia = new Intersectie(seg.A, seg.B, s1.A, s1.B);
             if (VerificaIntersectia.Intersectation())
                 return false;
         }
     }
     return true;
 }
예제 #8
0
 private static List<Segment> GetAllSegmentesThatAreIntersectedByASegment(Segment s)
 {
     var ls = new List<Segment>();
     foreach (var seg in SegmentsManager.Segments)
     {
         var intersectie = new Intersectie(seg.A, seg.B, s.A, s.B);
         if (intersectie.Intersectation())
             ls.Add(seg);
     }
     foreach (var seg in PolygonManager.Contur.Segmente)
     {
         var intersectie = new Intersectie(seg.A, seg.B, s.A, s.B);
         if (intersectie.Intersectation())
             ls.Add(seg);
     }
     return ls;
 }
예제 #9
0
 public static Point GetLastIntersectionPoint(Segment s)
 {
     var ls = GetAllSegmentesThatAreIntersectedByASegment(s);
     var lp = new List<Point>();
     foreach (var seg in ls)
     {
         var intersectie = new Intersectie(seg.A, seg.B, s.A, s.B);
         if (intersectie.Intersectation())
         {
             intersectie.SetIntersectionPoint();
             if (!double.IsNaN(intersectie.x) && !double.IsNaN(intersectie.y))
                 lp.Add(new Point(Convert.ToInt32(intersectie.x), Convert.ToInt32(intersectie.y)));
         }
     }
     lp = lp.OrderBy(p => p.Y).ToList();
     return lp.Last();
 }
예제 #10
0
파일: Segment.cs 프로젝트: am1guma/BR
 public bool Egale(Segment x)
 {
     if((A.Equals(x.A) && B.Equals(x.B)) || (B.Equals(x.A) && A.Equals(x.B)))
         return true;
     return false;
 }
예제 #11
0
 public static bool CheckIfSegmentAlreadyExists(Segment s)
 {
     return Segments.Any(seg => seg.Egale(s));
 }