Ejemplo n.º 1
0
 // Поиск пересечения отрезков
 //public static List<Point> GetIntersection(Segment first, Segment second)
 //{
 //    double a1, b1; // параметрические координаты первой прямой
 //    double a2, b2; // параметрические координаты второй прямой
 //    double x, y;// коодинаты пересечения двух прямых
 //    if ((first.Begin.X != first.End.X) && (second.Begin.X != second.End.X)) // проверка на совпадение коээфицентов при X
 //    {
 //        a1 = (first.Begin.Y - first.End.Y) / (first.Begin.X - first.End.X);
 //        b1 = first.Begin.Y - first.Begin.X * a1;
 //        a2 = (second.Begin.Y - second.End.Y) / (second.Begin.X - second.End.X);
 //        b2 = second.Begin.Y - second.Begin.X * a2;
 //        if (a1 != a2)
 //        {
 //            x = (b2 - b1) / (a1 - a2);
 //            y = a1 * x + b1;
 //            Point inter = new Point(x, y);
 //            if ((Intersect.IsIntersected(inter, first)) && (Intersect.IsIntersected(inter, second)))
 //            {
 //                return new List<Point> { inter };
 //            }
 //        }
 //    }
 //    return new List<Point>();
 //}
 //Поиск точки основания стрелочки
 public static Point GetArrowBase(Point begin, Point end)
 {
     Segment segment = new Segment(begin, end);
     Circle circle = new Circle(end, 1);
     var c = GetIntersection(segment, circle);
     return c[0];
 }
Ejemplo n.º 2
0
        ///<summary>
        /// Поиск пересечения отрезков
        ///</summary>        
        public static List<Point> GetIntersection(Segment first, Segment second)
        {
            Point vectorFirst = new Point(first.End.X - first.Begin.X, first.End.Y - first.Begin.Y);
            Point vectorSecond = new Point(second.End.X - second.Begin.X, second.End.Y - second.Begin.Y);
            var poList = new List<Point>();
            if (vectorFirst.X / vectorSecond.X == vectorFirst.Y / vectorSecond.Y)
            {
                if (Point.Length(first.Begin, first.End) > Point.Length(second.Begin, second.End))
                {
                    if (IsIntersected(second.Begin, first)) poList.Add(second.Begin);
                    if (IsIntersected(second.End, first)) poList.Add(second.End);
                }
                else
                {
                    if (IsIntersected(first.Begin, second)) poList.Add(first.Begin);
                    if (IsIntersected(first.End, second)) poList.Add(first.End);
                }
            }
            double p = 0, t = 0;
            double a1 = 0, a2 = 0, b1 = 0, b2 = 0, d1 = 0, d2 = 0;
            //Point vectorFirst = new Point(first.End.X - first.Begin.X, first.End.Y - first.Begin.Y);
            //Point vectorSecond = new Point(second.End.X - second.Begin.X, second.End.Y - second.Begin.Y);
            //var poList = new List<Point>();
            d1 = second.Begin.X - first.Begin.X;
            d2 = second.Begin.Y - first.Begin.Y;
            a1 = vectorFirst.X;
            a2 = vectorFirst.Y;
            b1 = vectorSecond.X;
            b2 = vectorSecond.Y;
            if (a1 == 0)
            {
                p = d1 / b1;
                t = (d2 - p * b2) / a2;
            }
            else
            {
                p = d2 - (d1 / a1) * a2;
                p = p / (b2 - (b1 * a2) / a1);

                t = (d1 - b1 * p) / a1;

            }
            p = -p;
            if ((IsIntersected(new Point(first.Begin.X + vectorFirst.X * t, first.Begin.Y + vectorFirst.Y * t), first))
                && (IsIntersected(new Point(second.Begin.X + vectorSecond.X * p, second.Begin.Y + vectorSecond.Y * p), second)))
            {
                Point pointInter = new Point(first.Begin.X + vectorFirst.X * t, first.Begin.Y + vectorFirst.Y * t);
                poList.Add(pointInter);
            }
            //if ((p <= 1) && (p >= 0) && (t <= 1) && (t >= 0))

            return poList;
        }
Ejemplo n.º 3
0
        IDrawable generate(VariablesSet pars)
        {
            double angle1 = Math.PI * pars["Angle1"].Value / 180;
            double angle2 = Math.PI * pars["Angle2"].Value / 180;
            double l = pars["Length"].Value;

            Segment a = new Segment(l * Math.Cos(angle2), 40, 40, l * Math.Sin(angle2));
            Point p = new Point(2 * l * Math.Cos(angle1), 2 * l * Math.Sin(angle1));
            Segment b = new Segment(40, 40, p.X, p.Y);

            //Segment b = new Segment(0, -20, 50, -20);
            //Segment b = new Segment(25, -25, 25, 25);
            //Segment a = new Segment(new Point(0,0),p );

            return new SegmentSet(a,b);
        }
Ejemplo n.º 4
0
 private static Point LeftNormal(Segment segment)
 {
     double x = segment.End.X - segment.Begin.X;
     double y = segment.End.Y - segment.Begin.Y;
     return new Point(-y, x);
 }
Ejemplo n.º 5
0
 // Находится ли точка слева по напр. левой нормали (для intersect(ConvexPolygon polygon, params Point[] arr) )
 private static bool isLeft(Segment segment, Point normal, Point point)
 {
     var vector = new Point(point.X - segment.Begin.X, point.Y - segment.Begin.Y);
     var scalar = vector.X * normal.X + vector.Y * normal.Y;
     return scalar >= 0;
 }
Ejemplo n.º 6
0
        // Пересечение точки и отрезка
        public static bool IsIntersected(Point cur, Segment line)
        {
            var begin = line.Begin;
            var end = line.End;
            var v = new Point(end.X - begin.X, end.Y - begin.Y);
            var t = new Point(0, 0);
            if (v.X != 0)
            {
                if (v.Y !=0)
                {
                    t = new Point(((cur.X - begin.X)/v.X), ((cur.Y - begin.Y)/v.Y));

                }
                else t = new Point(((cur.X - begin.X) / v.X), 0);
            }
            else
            {
                if (v.Y != 0)
                {
                    t = new Point(0, ((cur.Y - begin.Y)/v.Y));
                }
                else
                {
                    return false;
                }
            }
            return t.Y>=0 && t.Y<=1 && t.X <= 1 && t.X >= 0;
        }
Ejemplo n.º 7
0
 // Пересечение двух отрезков
 public static bool IsIntersected(Segment first, Segment second)
 {
     return GetIntersection(first, second).Count > 0;
 }
Ejemplo n.º 8
0
        /// <summary>
        /// Проверка пересечения отрезка и многоугольника
        /// </summary>
        /// <returns>Возвращает список точек пересечения</returns>
        public static List<Point> GetIntersection(Segment segment, ConvexPolygon polygon)
        {
            var poList = new List<Point>();
            var polySegments = Polyline.PointsToSegments(polygon.Points);
            var normals = polygon.Normals;

            for (int index = 0; index < polySegments.Count; index++)
            {
                 if ((!isLeft(polySegments[index], normals[index], segment.Begin)) && (!isLeft(polySegments[index], normals[index], segment.End)))
                    return poList;
                else
                {
                    if (GetIntersection(segment, polySegments[index]).Count > 0) poList.Add((GetIntersection(segment, polySegments[index]))[0]);
                }
            }
            if (IsIntersected(segment.Begin, polygon)) poList.Add(segment.Begin);
            if (IsIntersected(segment.End, polygon)) poList.Add(segment.End);
            for (int j = 0; j < poList.Count; j++)
            {
                for (int k = j + 1; k < poList.Count; k++)
                {
                    if ((poList[j].X == poList[k].X) && (poList[j].Y == poList[k].Y)) poList.Remove(poList[j]);
                }
            }

            return poList;
        }
Ejemplo n.º 9
0
 private Point GetLeftNormal(Segment segment)
 {
     double x = segment.End.X - segment.Begin.X;
     double y = segment.End.Y - segment.Begin.Y;
     return new Point(-y,x); // TODO!!!!!
 }
Ejemplo n.º 10
0
 //public static List<Point> GetIntersection(ConvexPolygon polygon, Segment segment)
 //{
 //}
 // Пересечение отрезка и круга
 public static List<Point> GetIntersection(Segment segment, Circle circle)
 {
     double a, b, t, diskr;
     Point vector = new Point(segment.End.X - segment.Begin.X, segment.End.Y - segment.Begin.Y);
     t = Math.Pow(circle.Radius, 2) - Math.Pow(circle.Center.X, 2) - Math.Pow(circle.Center.Y, 2);
     t = t - segment.Begin.X * segment.Begin.X - segment.Begin.Y * segment.Begin.Y + 2 * vector.X * circle.Center.X + 2 * vector.Y * circle.Center.Y;
     a = vector.X + vector.Y;
     b = segment.Begin.X * vector.X + segment.Begin.Y * vector.Y - vector.X * circle.Center.X -
         vector.Y * circle.Center.Y;
     diskr = b * b - 4 * a * t;
     if (diskr < 0) return new List<Point>();
     else if (diskr == 0)
     {
         double k;// Коэффицент при векторе
         k = -(b / (2 * a));
         if ((k > 0) || (k < 1)) return new List<Point> { new Point(segment.Begin.X + vector.X * k, segment.Begin.Y + vector.Y * k) };
         else return new List<Point>();
     }
     else
     {
         double k1, k2;
         k1 = (-b - diskr) / (2 * a); k2 = (diskr - b) / (2 * a);
         Point point1 = new Point(segment.Begin.X + k1 * vector.X, segment.Begin.Y + k1 * vector.Y);
         Point point2 = new Point(segment.Begin.X + k2 * vector.X, segment.Begin.Y + k2 * vector.Y);
         List<Point> spisk = new List<Point>();
         if ((k1 > 0) || (k1 < 1)) spisk.Add(point1);
         if ((k2 > 0) || (k2 < 1)) spisk.Add(point2);
         return spisk;
     }
 }
Ejemplo n.º 11
0
        // Поиск точек пересечений Полигона со списком точек (array)
        public static List<Point> GetIntersection(ConvexPolygon polygon, params Point[] arr)
        {
            int type = 0; // flag of ConvexPolygon type intersect (Polyline,Segment,Point)
            var points = new List<Point>();
            var segments = Polyline.PointsToSegments(polygon.Points);
            var normals = polygon.Normals;
            switch (arr.Count())
            {
                case 0: // Nothing
                    return new List<Point>();
                case 1: // Point
                    break;
                case 2: // Segment
                    type = 1;
                    break;
                default: // Polyline
                    type = 2;

                    var PolylineSegments = Polyline.PointsToSegments(arr.ToList());

                    foreach (var polylineSegment in PolylineSegments)
                    {
                        points.AddRange(Intersect.GetIntersection(polygon, polylineSegment.Begin, polylineSegment.End));
                    }
                    break;
            }

            for (int index = 0; index < segments.Count; index++)
            {
                var t = segments[index];

                if (type == 0)
                {
                    if (!isLeft(t, normals[index], arr[0])) return new List<Point>();
                }
                else if (type == 1)
                {
                    if (isLeft(t, normals[index], arr[0]) && !isLeft(t, normals[index], arr[1]) || !isLeft(t, normals[index], arr[0]) && isLeft(t, normals[index], arr[1]))
                    {
                        foreach (var segment in segments)
                        {
                                //if (Intersect.GetIntersection(new Segment(arr[0], arr[1]), segment).Count > 0) points.AddRange(Intersect.GetIntersection(new Segment(arr[0], arr[1]), segment));

                            var newSegment = new Segment(arr[0], arr[1]);

                            if (GetIntersection(newSegment, segment).Count>0) points.AddRange(Intersect.GetIntersection(new Segment(arr[0], arr[1]), segment));

                        }
                        break;
                    }
                    else if (!isLeft(t, normals[index], arr[0]) && !isLeft(t, normals[index], arr[1]))
                    {
                        return new List<Point>();
                    }
                }
            }

            if (type == 0)
                points.Add(arr[0]);
            if (type == 1)
                points.AddRange(new List<Point> { arr[0], arr[1] });

            return points;
        }
Ejemplo n.º 12
0
 // Пересечение отрезка и круга
 private static bool IsIntersected(Segment segment, Circle circle)
 {
     return GetIntersection(segment, circle).Count > 0;
 }
Ejemplo n.º 13
0
 // Пересечение отрезка и полигона
 private static bool IsIntersected(Segment segment, ConvexPolygon polygon)
 {
     return GetIntersection(segment, polygon).Count > 0;
 }
Ejemplo n.º 14
0
        /// <summary>
        /// Проверка принадлежности точки отрезку
        /// </summary>
        public static bool IsIntersected(Point cur, Segment line)
        {
            var begin = line.Begin;
            var end = line.End;
            var v = new Point(end.X - begin.X, end.Y - begin.Y);

            if (Point.Length(line.Begin, cur) + Point.Length(line.End, cur) == Point.Length(line.Begin, line.End))
                return true;
            return false;

            //var t = new Point(0, 0); // !!
            //if (v.X != 0)
            //{
            //    if (v.Y != 0)
            //    {
            //        t = new Point(((cur.X - begin.X) / v.X), ((cur.Y - begin.Y) / v.Y));

            //    }
            //    else t = new Point(((cur.X - begin.X) / v.X), 0);
            //}
            //else
            //{
            //    if (v.Y != 0)
            //    {
            //        t = new Point(0, ((cur.Y - begin.Y) / v.Y));
            //    }
            //    else
            //    {
            //        return false;
            //    }
            //}
            //return t.Y >= 0 && t.Y <= 1 && t.X <= 1 && t.X >= 0;
        }
Ejemplo n.º 15
0
        IDrawable run(IDrawable input)
        {
            if (!(input is PointSet))
            {
                throw new Exception("SegmentCircleTest. Input is not SegmentCircleTest.");
            }

            var inp = input as PointSet;

            Point a = inp[0];
            Point b = inp[1];
            Point c = inp[2];

            Circle d = new Circle(b.X, b.Y, c.X);
            Segment e = new Segment(new Point(0, 0), a);

            bool x = Intersect.IsIntersected(a,d);
            if (x) return new DrawableSet(new List<IDrawable>() { new SegmentSet(e), new CircleSet(d), DrawableElement.Text(0, 0, "Vse horosho") });
            else return new DrawableSet(new List<IDrawable>() { new SegmentSet(e), new CircleSet(d), DrawableElement.Text(0, 0, "Vse Ploho") });
        }
Ejemplo n.º 16
0
        public static List<Point> GetIntersection(Segment segment, ConvexPolygon polygon)
        {
            var poList = new List<Point>();
            var polySegments = Polyline.PointsToSegments(polygon.Points);
            var normals = polygon.Normals;

                for (int index = 0; index < polySegments.Count; index++)
            {
                if ((!isLeft(polySegments[index], normals[index], segment.Begin))&&(!isLeft(polySegments[index], normals[index], segment.End)))
                return poList;
                else
                {
                    if (GetIntersection(segment, polySegments[index]).Count > 0) poList.Add((GetIntersection(segment, polySegments[index]))[0]);
                }
            }
            //if (poList.Count==0)
            //{
            //    poList.Add(segment.Begin);
            //    poList.Add(segment.End);
            //}
            if (IsIntersected(segment.Begin, polygon)) poList.Add(segment.Begin);
            if (IsIntersected(segment.End, polygon)) poList.Add(segment.End); ;

            return poList;
        }
Ejemplo n.º 17
0
        IDrawable run(IDrawable input)
        {
            if (!(input is PointSet))
            {
                throw new Exception("PointSegmentTest. Input is not PointSegmentTest.");
            }

            var inp = input as PointSet;

            Point a = inp[0];
            Point b = inp[1];
            Point c = inp[2];
            Segment d = new Segment(b, c);

            bool x = Intersect.IsIntersected(a,d);
            if (x)
                return
                    new DrawableSet(new List<IDrawable>() {new SegmentSet(d), DrawableElement.Text(0, 0, "Vse horosho")});
            else return new DrawableSet(new List<IDrawable>() { new SegmentSet(d), DrawableElement.Text(0, 0, "Vse ploho") });
        }
Ejemplo n.º 18
0
        // Функция для поиска пересечений ломанной с фигурами
        // Общего вида для пути
        public static List<Point> GetIntersection(Figure figure, Segment segment)
        {
            if (figure is Circle)
            {
                return GetIntersection(figure as Circle, segment);
            }
            else if (figure is ConvexPolygon)
            {
                return GetIntersection(figure as ConvexPolygon, segment);
            }

            return new List<Point>();
        }
Ejemplo n.º 19
0
        IDrawable run(IDrawable input)
        {
            if (!(input is PointSet))
            {
                throw new Exception("SegmentPolygonTest. Input is not SegmentPolygonTest.");
            }

            var inp = input as PointSet;

            Point a = inp[0]; Point c = inp[1]; Point d = inp[2]; Point e = inp[3];

            //a = new Point(10, 10); e = new Point(40, 10); Point ee = new Point(60, 40);
            //Segment b = new Segment(a, e); //Segment bbb = new Segment(e, ee);
            Segment b = new Segment(10, (c.Y + d.Y) / 2.5, 100, (c.Y + d.Y) / 2.5);
            var f = new ConvexPolygon(new Point((c.X + d.X) / 2, c.Y), new Point(d.X, (c.Y + d.Y) / 3), new Point(d.X, 2 * ((d.Y + c.Y) / 3)), new Point((c.X + d.X) / 2, d.Y), new Point(c.X, 2*((d.Y + c.Y) / 3)), new Point(c.X, ((d.Y + c.Y) / 3)));
            var fa = new ConvexPolygon(new Point((c.X + d.X) / 2 + 40, c.Y), new Point(d.X + 40, (c.Y + d.Y) / 3), new Point(d.X + 40, 2 * ((d.Y + c.Y) / 3)), new Point((c.X + d.X) / 2 + 40, d.Y), new Point(c.X + 40, 2 * ((d.Y + c.Y) / 3)), new Point(c.X + 40, ((d.Y + c.Y) / 3)));
            //var f = new ConvexPolygon(new Point(c.X, c.Y), new Point(d.X, c.Y), new Point(d.X, d.Y), new Point(c.X, d.Y));

            //var f = new ConvexPolygon(new Point(40,40), new Point(70,40), new Point(70,70), new Point(40,70));
            //b = new Segment(30,50,40,50);

            var x = Intersect.GetIntersection(b, f);// var y = Intersect.GetIntersection(f);
            var xx = Intersect.GetIntersection(b, fa);
            if (x.Count + xx.Count > 0) return new DrawableSet(new List<IDrawable>()
            { new PointSet(x.Concat(xx).ToArray()),
                new SegmentSet(b),
                new PolygonSet(f,fa),
                DrawableElement.Text(0, 0, "Vse horosho")
            });
            else return new DrawableSet(new List<IDrawable>()
            { new PointSet(x.Concat(xx).ToArray()),
                new SegmentSet(b),
                new PolygonSet(f, fa),
                DrawableElement.Text(0, 0, "Vse ploho")
            });
            //return new PointSet(x.ToArray());
        }
Ejemplo n.º 20
0
        IDrawable run(IDrawable input)
        {
            if (!(input is PointSet))
            {
                throw new Exception("PolylinePoligonTest. Input is not PolylinePoligonTest.");
            }

            var inp = input as PointSet;

            Point a = inp[0]; Point c = inp[1]; Point d = inp[2]; Point e = inp[4];
            Point polyPoint1 = inp[4];

            Point aa = new Point(20, 20); e = new Point(30, 20); Point ee = new Point(50,30);
            Segment b = new Segment(aa, e); Segment eee = new Segment(e, ee); Segment eae = new Segment(new Point(50, 30), a);
            Polyline polyline = new Polyline(20, 20, 30, 20, 50, 30, a.X, a.Y);

            //Segment b = new Segment(a,e);
            //Segment eee = new Segment(e,new Point(3*(a.X+e.X)/2,4*(a.Y+e.Y)/2));
            //var polyline = new Polyline(new List<Point>() { a, e, new Point((a.X + e.X) / 2, (a.Y + e.Y) / 2) });

            //var f = new ConvexPolygon(new Point(c.X, c.Y), new Point(d.X, c.Y), new Point(d.X, d.Y), new Point(c.X, d.Y));

            var f = new ConvexPolygon(new Point((c.X + d.X) / 2, c.Y), new Point(d.X, (c.Y + d.Y) / 3), new Point(d.X, 2 * ((d.Y + c.Y) / 3)), new Point((c.X + d.X) / 2, d.Y), new Point(c.X, 2 * ((d.Y + c.Y) / 3)), new Point(c.X, ((d.Y + c.Y) / 3)));

            var x = Intersect.GetIntersection(polyline,f);
            if (x.Count > 0) return new DrawableSet(new List<IDrawable>() { new PointSet(x.ToArray()), new SegmentSet(b,eee,eae),new PolygonSet(f), DrawableElement.Text(0, 0, "Vse horosho") });
            else return new DrawableSet(new List<IDrawable>() { new PointSet(x.ToArray()),new SegmentSet(b,eee,eae), new PolygonSet(f), DrawableElement.Text(0, 0, "Vse ploho") });
        }