Exemple #1
0
 /// <summary>
 /// Если фигуры не находятся в стороне друг от друга, то true
 /// </summary>
 /// <param name="rect"></param>
 /// <returns></returns>
 public bool Collide(V4 rect)
 {
     return(Contains(rect.Origin) ||
            Contains(rect.Origin + new V2(rect.Destination.Y, 0)) ||
            Contains(rect.Origin + new V2(0, rect.Destination.X)) ||
            Contains(rect.Origin + rect.Destination) ||
            rect.Contains(this));
 }
Exemple #2
0
        /// <summary>
        /// На сколько нужно сдвинуть <paramref name="rect"/> чтобы он вошел в this. Если размер <paramref name="rect"/> больше, чем this, то выбрасывается исключение.
        /// </summary>
        /// <param name="rect"></param>
        /// <returns></returns>
        public V2 OffsetToContain(V4 rect, CoordSystem coordSystem)
        {
            if (coordSystem != CoordSystem.BOTTOM_LEFT)
            {
                throw new Exception();
            }

            double eps = 1E-9;

            if (rect.Size.X + eps > Size.X ||
                rect.Size.Y + eps > Size.Y)
            {
                throw new Exception();
            }
            else
            {
                var rectLeft   = Math.Min(rect.Origin.X, rect.Destination.X);
                var rectTop    = Math.Max(rect.Origin.Y, rect.Destination.Y);
                var rectRight  = Math.Max(rect.Origin.X, rect.Destination.X);
                var rectBottom = Math.Min(rect.Origin.Y, rect.Destination.Y);
                var thisLeft   = Math.Min(Origin.X, Destination.X);
                var thisTop    = Math.Max(Origin.Y, Destination.Y);
                var thisRight  = Math.Max(Origin.X, Destination.X);
                var thisBottom = Math.Min(Origin.Y, Destination.Y);
                var dLeft      = rectLeft - thisLeft;
                var dTop       = rectTop - thisTop;
                var dRight     = rectRight - thisRight;
                var dBottom    = rectBottom - thisBottom;
                var dx         = 0D;
                var dy         = 0D;
                dx = dLeft < 0 ? -dLeft : dx;
                dx = dRight > 0 ? -dRight : dx;
                dy = dTop > 0 ? -dTop : dy;
                dy = dBottom < 0 ? -dBottom : dy;

                return(new V2(dx, dy));
            }
        }
Exemple #3
0
        public Polygon(IEnumerable <V2> vertices)
        {
            Vertices = vertices.ToArray();
            Edges    = extractEdges();
            Center   = calcCenter();
            Rect     = calcRect();

            Edge[] extractEdges()
            {
                if (Vertices.Length < 2)
                {
                    return(new Edge[0]);
                }
                V2 LastVert = Vertices[0];
                V2 CurrVert = V2.Zero;

                Edge[] Buff = new Edge[] { };
                Array.Resize(ref Buff, Vertices.Length);
                for (int i = 0; i < Vertices.Length - 1; i++)
                {
                    CurrVert = Vertices[i + 1];
                    Buff[i]  = new Edge(LastVert, CurrVert);
                    LastVert = CurrVert;
                }
                CurrVert = Vertices[0];
                Buff[Vertices.Length - 1] = new Edge(LastVert, CurrVert);
                LastVert = CurrVert;

                return(Buff);
            }

            V2 calcCenter()
            {
                //POINT, MASS
                Dictionary <V2, double> Mass = new Dictionary <V2, double>();
                Edge E;

                for (int i = 0; i < Edges.Length; i++)
                {
                    E = Edges[i];
                    Mass.Add(E.Middle, E.Len);
                }
                double X = 0;
                double Y = 0;

                foreach (KeyValuePair <V2, double> KVP in Mass)
                {
                    X += KVP.Key.X;
                    Y += KVP.Key.Y;
                }
                X /= Edges.Length;
                Y /= Edges.Length;

                return(new V2(X, Y));
            }

            V4 calcRect()
            {
                if (Vertices.Length == 0)
                {
                    return(V4.Zero);
                }

                V2  V    = Vertices[0];
                var MinX = V.X;
                var MinY = V.Y;
                var MaxX = V.X;
                var MaxY = V.Y;

                for (int i = 1; i < Vertices.Length; i++)
                {
                    V = Vertices[i];
                    if (V.X < MinX)
                    {
                        MinX = V.X;
                    }
                    else if (V.X > MaxX)
                    {
                        MaxX = V.X;
                    }

                    if (V.Y < MinY)
                    {
                        MinY = V.Y;
                    }
                    else if (V.Y > MaxY)
                    {
                        MaxY = V.Y;
                    }
                }

                return(new V4(MinX, MinY, MaxX - MinX, MaxY - MinY));
            }
        }
Exemple #4
0
 public bool Contains(V4 rect)
 {
     return(Contains(rect, false));
 }
Exemple #5
0
 public bool Contains(V4 rect, bool strict)
 {
     return(Contains(rect.Origin, strict) &&
            Contains(rect.Destination, strict));
 }