示例#1
0
        public void MovePoint(int pointIndx, Point newPos)
        {
            Point oldP = points[pointIndx];

            for (int i = 0; i < edges.Count; i++)
            {
                if (edges[i].p == oldP)
                {
                    MyEdge temp = edges[i];
                    edges.RemoveAt(i);
                    edges.Insert(i, new MyEdge(newPos, temp.q, temp.color));
                }
                else if (edges[i].q == oldP)
                {
                    MyEdge temp = edges[i];
                    edges.RemoveAt(i);
                    edges.Insert(i, new MyEdge(temp.p, newPos, temp.color));
                }
            }

            for (int i = 0; i < points.Count; i++)
            {
                if (points[i] == oldP)
                {
                    points.RemoveAt(i);
                    points.Insert(i, newPos);
                }
            }
        }
示例#2
0
        private MyEdge CyrusBeck(MyEdge line, Vector[] normal, List <Point> figurePoints)
        {
            int    n     = normal.Length;
            Vector P1_P0 = new Vector(line.q.X - line.p.X, line.q.Y - line.p.Y);

            Vector[] PEi_P0 = new Vector[n];
            for (int i = 0; i < n; i++)
            {
                PEi_P0[i].X = figurePoints[i].X - line.p.X;
                PEi_P0[i].Y = figurePoints[i].Y - line.p.Y;
            }

            int[] numerator   = new int[n];
            int[] denominator = new int[n];

            for (int i = 0; i < n; i++)
            {
                numerator[i]   = dot(normal[i], PEi_P0[i]);
                denominator[i] = dot(normal[i], P1_P0);
            }

            double[]      t  = new double[n];
            List <double> tE = new List <double>();
            List <double> tL = new List <double>();

            for (int i = 0; i < n; i++)
            {
                t[i] = (double)numerator[i] / denominator[i];

                if (denominator[i] > 0)
                {
                    tE.Add(t[i]);
                }
                else
                {
                    tL.Add(t[i]);
                }
            }

            double tEMax, tLMin;

            tE.Add(0);
            tL.Add(1);

            tEMax = tE.Max();
            tLMin = tL.Min();

            if (tEMax > tLMin)
            {
                return(null);
            }

            Point p = new Point(line.p.X + P1_P0.X * tEMax, line.p.Y + P1_P0.Y * tEMax);
            Point q = new Point(line.p.X + P1_P0.X * tLMin, line.p.Y + P1_P0.Y * tLMin);

            return(new MyEdge(p, q, cllipedColor));
        }
示例#3
0
        public void Clipping()
        {
            clippedLines = new List <MyEdge>();

            // clipping only for convex polygons
            if (!clippingPolygon.CheckIfConvex())
            {
                return;
            }

            if (clipToRectangle)
            {
                if (clippingRectangle != null)
                {
                    // calculate normals
                    int      nOfPoints = clippingRectangle.points.Count;
                    Vector[] normal    = new Vector[nOfPoints];
                    for (int i = 0; i < nOfPoints; i++)
                    {
                        normal[i].X = clippingRectangle.points[i].Y - clippingRectangle.points[(i + 1) % nOfPoints].Y;
                        normal[i].Y = clippingRectangle.points[(i + 1) % nOfPoints].X - clippingRectangle.points[i].X;
                    }

                    foreach (var edge in edges)
                    {
                        MyEdge result = this.CyrusBeck(edge, normal, clippingRectangle.points);
                        if (result != null)
                        {
                            clippedLines.Add(result);
                        }
                    }
                }
                else
                {
                    clipToRectangle = false;
                }
            }

            else if (clipToPolygon)
            {
                if (clippingPolygon != null)
                {
                    int      nOfPoints = clippingPolygon.points.Count - 1;
                    Vector[] normal    = new Vector[nOfPoints];
                    for (int i = 0; i < nOfPoints; i++)
                    {
                        normal[i].X = clippingPolygon.points[i].Y - clippingPolygon.points[(i + 1) % nOfPoints].Y;
                        normal[i].Y = clippingPolygon.points[(i + 1) % nOfPoints].X - clippingPolygon.points[i].X;
                    }

                    foreach (var edge in edges)
                    {
                        MyEdge result = this.CyrusBeck(edge, normal, clippingPolygon.points);
                        if (result != null)
                        {
                            clippedLines.Add(result);
                        }
                    }
                }
                else
                {
                    clipToPolygon = false;
                }
            }
        }