Example #1
0
        //make the given poly all points orthonogonal to each other
        internal static Polygon2d MakePolyPointsOrtho(Polygon2d poly)
        {
            Polygon2d      polyReg        = new Polygon2d(poly.Points);
            List <Point2d> ptForOrthoPoly = new List <Point2d>();

            for (int i = 0; i < polyReg.Points.Count; i++)
            {
                Point2d pt = Point2d.ByCoordinates(polyReg.Points[i].X, polyReg.Points[i].Y);
                ptForOrthoPoly.Add(pt);
            }

            for (int i = 0; i < polyReg.Points.Count; i++)
            {
                int    a = i, b = i + 1;
                double eps = 50;
                if (i == polyReg.Points.Count - 1)
                {
                    b = 0;
                }
                Line2d line = new Line2d(polyReg.Points[a], polyReg.Points[b]);
                if (ValidateObject.CheckLineOrient(line) == -1)
                {
                    //double diffX = Math.Abs(line.StartPoint.X - line.EndPoint.X);
                    //double diffY = Math.Abs(line.StartPoint.Y - line.EndPoint.Y);
                    Point2d cenPoly  = PolygonUtility.CentroidOfPoly(polyReg);
                    Point2d ptEndA   = Point2d.ByCoordinates(polyReg.Points[a].X + eps, polyReg.Points[a].Y);
                    Line2d  refLineA = Line2d.ByStartPointEndPoint(polyReg.Points[a], ptEndA);

                    Point2d ptEndB   = Point2d.ByCoordinates(polyReg.Points[b].X + eps, polyReg.Points[b].Y);
                    Line2d  refLineB = Line2d.ByStartPointEndPoint(polyReg.Points[b], ptEndB);

                    Point2d projectedPtA = GraphicsUtility.ProjectedPointOnLine(refLineB, polyReg.Points[a]);
                    Point2d projectedPtB = GraphicsUtility.ProjectedPointOnLine(refLineA, polyReg.Points[b]);

                    Vector2d vecA       = new Vector2d(projectedPtA, cenPoly);
                    Vector2d vecB       = new Vector2d(projectedPtB, cenPoly);
                    double   vecALength = vecA.Length;
                    double   vecBLength = vecB.Length;
                    if (vecALength > vecBLength)
                    {
                        //ptForOrthoPoly[i] = projectedPtA;
                        ptForOrthoPoly.Insert(b, projectedPtB);
                    }
                    else
                    {
                        //ptForOrthoPoly[i] = projectedPtB;
                        ptForOrthoPoly.Insert(b, projectedPtA);
                    }

                    /*
                     * if (diffX > diffY)
                     * {
                     *  Point2d ptEndA = Point2d.ByCoordinates(polyReg.Points[a].X, polyReg.Points[a].Y + eps);
                     *  Line2d refLineA = Line2d.ByStartPointEndPoint(polyReg.Points[a], ptEndA);
                     *  refLineA = LineUtility.extend(refLineA);
                     *
                     *  Point2d ptEndB = Point2d.ByCoordinates(polyReg.Points[b].X, polyReg.Points[b].Y + eps);
                     *  Line2d refLineB = Line2d.ByStartPointEndPoint(polyReg.Points[b], ptEndB);
                     *  refLineB = LineUtility.extend(refLineB);
                     *
                     *  Point2d projectedPtA = GraphicsUtility.ProjectedPointOnLine(refLineB, polyReg.Points[a]);
                     *  Point2d projectedPtB = GraphicsUtility.ProjectedPointOnLine(refLineA, polyReg.Points[b]);
                     *
                     *  Vector2d vecA = new Vector2d(projectedPtA, cenPoly);
                     *  Vector2d vecB = new Vector2d(projectedPtB, cenPoly);
                     *  double vecALength = vecA.Length;
                     *  double vecBLength = vecB.Length;
                     *  if(vecALength < vecBLength)
                     *  {
                     *      //ptForOrthoPoly[i] = projectedPtA;
                     *      ptForOrthoPoly.Insert(b, projectedPtB);
                     *  }
                     *  else
                     *  {
                     *      //ptForOrthoPoly[i] = projectedPtB;
                     *      ptForOrthoPoly.Insert(b, projectedPtA);
                     *  }
                     * }
                     * else
                     * {
                     *
                     *  Point2d ptEndA = Point2d.ByCoordinates(polyReg.Points[a].X + eps, polyReg.Points[a].Y);
                     *  Line2d refLineA = Line2d.ByStartPointEndPoint(polyReg.Points[a], ptEndA);
                     *  refLineA = LineUtility.extend(refLineA);
                     *
                     *  Point2d ptEndB = Point2d.ByCoordinates(polyReg.Points[b].X + eps, polyReg.Points[b].Y);
                     *  Line2d refLineB = Line2d.ByStartPointEndPoint(polyReg.Points[b], ptEndB);
                     *  refLineB = LineUtility.extend(refLineB);
                     *
                     *  Point2d projectedPtA = GraphicsUtility.ProjectedPointOnLine(refLineB, polyReg.Points[a]);
                     *  Point2d projectedPtB = GraphicsUtility.ProjectedPointOnLine(refLineA, polyReg.Points[b]);
                     *
                     *  Vector2d vecA = new Vector2d(projectedPtA, cenPoly);
                     *  Vector2d vecB = new Vector2d(projectedPtB, cenPoly);
                     *  double vecALength = vecA.Length;
                     *  double vecBLength = vecB.Length;
                     *  if (vecALength < vecBLength)
                     *  {
                     *      //ptForOrthoPoly[i] = projectedPtA;
                     *      ptForOrthoPoly.Insert(b, projectedPtB);
                     *  }
                     *  else
                     *  {
                     *      //ptForOrthoPoly[i] = projectedPtB;
                     *      ptForOrthoPoly.Insert(b, projectedPtB);
                     *  }
                     * }
                     */
                }
            }
            return(new Polygon2d(ptForOrthoPoly));
        }
Example #2
0
        internal static Dictionary <string, object> PolygonPolygonCommonEdgeDict(Polygon2d poly, Polygon2d other)
        {
            bool check = false;

            if (poly == null || other == null)
            {
                return(null);
            }

            double    eps      = 200;
            Polygon2d polyReg  = new Polygon2d(poly.Points);
            Polygon2d otherReg = new Polygon2d(other.Points);
            Dictionary <string, object> UpdatedCenters = ComputePolyCentersAlign(polyReg, otherReg);

            Point2d centerPoly  = (Point2d)UpdatedCenters["CenterPolyA"];
            Point2d centerOther = (Point2d)UpdatedCenters["CenterPolyB"];

            polyReg  = (Polygon2d)UpdatedCenters["PolyA"];
            otherReg = (Polygon2d)UpdatedCenters["PolyB"];
            //make vectors
            Vector2d centerToCen  = new Vector2d(centerPoly, centerOther);
            Vector2d centerToCenX = new Vector2d(centerToCen.X, 0);
            Vector2d centerToCenY = new Vector2d(0, centerToCen.Y);
            //make centerLine
            Line2d   centerLine = new Line2d(centerPoly, centerOther);
            Vector2d keyVec;

            if (centerToCenX.Length > centerToCenY.Length)
            {
                keyVec = new Vector2d(centerToCenX.X, centerToCenX.Y);
            }
            else
            {
                keyVec = new Vector2d(centerToCenY.X, centerToCenY.Y);
            }
            //check line poly intersection between centertocen vector and each polys
            Line2d lineInPolyReg  = CodeToBeTested.LinePolygonIntersectionReturnLine(polyReg.Points, centerLine, centerOther);
            Line2d lineInOtherReg = CodeToBeTested.LinePolygonIntersectionReturnLine(otherReg.Points, centerLine, centerPoly);

            //find distance d1 and d2 from two centers to linepolyintersection line
            Point2d projectedPtOnPolyReg  = GraphicsUtility.ProjectedPointOnLine(lineInPolyReg, centerPoly);
            Point2d projectedPtOnOtherReg = GraphicsUtility.ProjectedPointOnLine(lineInOtherReg, centerOther);

            double dist1 = PointUtility.DistanceBetweenPoints(centerPoly, projectedPtOnPolyReg);
            double dist2 = PointUtility.DistanceBetweenPoints(centerOther, projectedPtOnOtherReg);

            double totalDistance = 2 * (dist1 + dist2);
            Line2d lineMoved     = new Line2d(lineInPolyReg.StartPoint, lineInPolyReg.EndPoint);

            lineMoved = LineUtility.Move(lineMoved, centerPoly);
            Point2d projectedPt = GraphicsUtility.ProjectedPointOnLine(lineMoved, centerOther);
            double  distance    = PointUtility.DistanceBetweenPoints(projectedPt, centerOther);

            bool isNeighbour = false;

            if (totalDistance - eps < distance && distance < totalDistance + eps)
            {
                isNeighbour = true;
            }
            else
            {
                isNeighbour = false;
            }

            return(new Dictionary <string, object>
            {
                { "Neighbour", (isNeighbour) },
                { "SharedEdgeA", (lineInPolyReg) },
                { "SharedEdgeB", (lineInOtherReg) },
                { "LineMoved", (lineMoved) },
                { "CenterToCenterLine", (centerLine) },
                { "CenterPolyPoint", (centerPoly) },
                { "CenterPolyOtherPoint", (centerOther) },
            });
        }