Example #1
0
        public void addStation(Station s, Dictionary <ushort, MapTransportLine> lines)
        {
            bool   inverse  = false;
            bool   vertical = false;
            string name     = s.name;
            float  angle    = s.writeAngle;

            switch (CardinalPoint.GetCardinalPoint(angle).Value)
            {
            case CardinalPoint.CardinalInternal.SW:
            case CardinalPoint.CardinalInternal.S:
            case CardinalPoint.CardinalInternal.SE:
                inverse = true;
                break;

            case CardinalPoint.CardinalInternal.E:
            case CardinalPoint.CardinalInternal.W:
                vertical = true;
                break;
            }

            foreach (ushort pos in s.getAllStationOffsetPoints())
            {
                Vector2          point = s.centralPos - offset;// + pos.Key;
                MapTransportLine line  = lines[pos];
                svgPart.Append(getStationPointTemplate(s.getLineIdx(pos), line.lineColor, point.x * multiplier, point.y * multiplier, line.lineId, s.id));
            }
            Vector2 namePoint = s.writePoint - offset;

            htmlStationsPart.Append(vertical ?
                                    getStationNameVerticalTemplate(s.name, s.getAllStationOffsetPoints().Count, s.id, (namePoint.x + 0.5f) * multiplier, ((namePoint.y + 0.5f) * multiplier), angle, s.linesPassing) : inverse ?
                                    getStationNameInverseTemplate(s.name, s.getAllStationOffsetPoints().Count, s.id, (namePoint.x + 0.5f) * multiplier, ((namePoint.y + 0.5f) * multiplier), angle, s.linesPassing) :
                                    getStationNameTemplate(s.name, s.getAllStationOffsetPoints().Count, s.id, (namePoint.x + 0.5f) * multiplier, ((namePoint.y + 0.5f) * multiplier), angle, s.linesPassing));
        }
        public void addStation(Station s, Dictionary <ushort, MapTransportLine> lines)
        {
            bool   inverse = false;
            string name    = s.name;
            var    angle   = s.writeAngle;

            switch (CardinalPoint.getCardinalPoint(angle).Value)
            {
            case CardinalPoint.CardinalInternal.NW:
            case CardinalPoint.CardinalInternal.W:
            case CardinalPoint.CardinalInternal.SW:
                inverse = true;
                break;
            }

            foreach (var pos in s.getAllStationOffsetPoints())
            {
                var point = s.centralPos - offset;// + pos.Key;
                var line  = lines[pos];
                svgPart.AppendFormat(getStationPointTemplate(s.getLineIdx(pos), line.lineColor), point.x * multiplier, (point.y * multiplier));
            }
            var namePoint = s.writePoint - offset;

            htmlPart.AppendFormat(inverse ? getStationNameInverseTemplate(s.getAllStationOffsetPoints().Count) : getStationNameTemplate(s.getAllStationOffsetPoints().Count), (namePoint.x + 0.5) * multiplier, ((namePoint.y + 0.5) * multiplier), angle, s.name);
        }
        protected List <Vector2> getPathForStations(Station s1, Station s2)
        {
            TLMUtils.doLog("Calculando caminho entre '{0}' e '{1}' ", s1.name, s2.name);
            Vector2 originalPos = s1.centralPos;
            Vector2 toPos       = s2.centralPos;

            CardinalPoint s1Exit = s1.reserveExit(s2);
            CardinalPoint s2Exit = s2.reserveExit(s1);

            return(getPath(originalPos, toPos, s1Exit, s2Exit));
        }
Example #4
0
 public static int Compare(CardinalPoint left, CardinalPoint right)
 {
     if (object.ReferenceEquals(left, right))
     {
         return(0);
     }
     if (object.ReferenceEquals(left, null))
     {
         return(-1);
     }
     return(left.CompareTo(right));
 }
        public CardinalPoint reserveExit(Station s2)
        {
            if (stationConnections.Count >= 8)
            {
                return(CardinalPoint.ZERO);
            }
            if (stopsWithWorldPos.ContainsKey(s2.stopId) || s2.stopsWithWorldPos.ContainsKey(stopId))
            {
                return(CardinalPoint.ZERO);
            }
            var s = stationConnections.FirstOrDefault(x => x.Value.stopsWithWorldPos.ContainsKey(s2.stopId));

            if (!s.Equals(default(KeyValuePair <CardinalPoint, Station>)))
            {
                return(s.Key);
            }

            CardinalPoint direction   = CardinalPoint.getCardinal2D(centralPos, s2.centralPos);
            CardinalPoint directionOr = direction;

            CardinalPoint directionAlt = CardinalPoint.getCardinal2D4(centralPos, s2.centralPos);

            bool isForward = direction > directionAlt;

            if (stationConnections.ContainsKey(direction))
            {
                if (isForward)
                {
                    direction++;
                }
                else
                {
                    direction--;
                }
            }

            if (stationConnections.ContainsKey(direction))
            {
                direction = directionOr;
                if (isForward)
                {
                    direction--;
                }
                else
                {
                    direction++;
                }
            }

            stationConnections[direction] = s2;
            return(direction);
        }
Example #6
0
        public int CompareTo(CardinalPoint other)
        {
            if (this == other)
            {
                return(0);
            }
            var a = getCardinalAngle();
            var b = other.getCardinalAngle() + 360;

            if (b - a > 180)
            {
                return(-1);
            }
            else
            {
                return(1);
            }
        }
Example #7
0
        public List <Vector2> getPath(Vector2 originalPos, Vector2 toPos, CardinalPoint s1Exit, CardinalPoint s2Exit)
        {
            //var angle = originalPos.GetAngleToPoint(toPos);
            //var tan = Math.Tan(angle / Mathf.Rad2Deg - 0.000001);
            Vector2       p1, p2;
            CardinalPoint c1, c2;

            //if (angle == 90 || angle == 270 || tan >= 0)
            //{
            p1 = originalPos;
            p2 = toPos;
            c1 = s1Exit;
            c2 = s2Exit;
            //}
            //else
            //{
            //    p1 = toPos;
            //    p2 = originalPos;
            //    c1 = s2Exit;
            //    c2 = s1Exit;
            //}



            List <Vector2> saida = new List <Vector2>();

            GetPointsLine(ref p1, ref p2, ref c1, ref c2, ref saida);

            for (int i = 1; i < saida.Count; i++)
            {
                addSegmentToIndex(saida[i - 1], saida[i]);
            }
            if (TransportLinesManagerMod.DebugMode)
            {
                string points = string.Join(",", saida.Select(x => "(" + x.x + "," + x.y + ")").ToArray());
                TLMUtils.doLog("Points: [{0}]", points);
            }

            return(saida);
        }
Example #8
0
        public int stepsTo(CardinalPoint other)
        {
            if (other.InternalValue == InternalValue)
            {
                return(0);
            }
            if ((((int)other.InternalValue) & ((int)other.InternalValue - 1)) != 0 || (((int)InternalValue) & ((int)InternalValue - 1)) != 0)
            {
                return(int.MaxValue);
            }
            CardinalPoint temp  = other;
            int           count = 0;

            while (temp.InternalValue != this.InternalValue)
            {
                temp++;
                count++;
            }
            if (count > 4)
            {
                count = count - 8;
            }
            return(count);
        }
        internal void drawAllLines()
        {
            TransportLine[] tls       = Singleton <TransportManager> .instance.m_lines.m_buffer;
            var             segments  = segmentManager.getSegments();
            string          mainStyle = "<polyline points=\"{0}\" class=\"path{4}\" style='stroke:rgb({1},{2},{3});' stroke-linejoin=\"round\" stroke-linecap=\"round\" marker-mid=\"url(#5)\"/> ";

            foreach (var segment in segments)
            {
                List <Vector2> basePoints = segment.path;
                for (int i = 0; i < basePoints.Count; i++)
                {
                    basePoints[i] = (basePoints[i] - offset) * multiplier;
                }
                float         offsetNeg = 0;
                float         offsetPos = 0;
                CardinalPoint dir       = CardinalPoint.getCardinal2D(segment.s1.centralPos, segment.s2.centralPos);
                dir++;
                dir++;
                Vector2 offsetDir = dir.getCardinalOffset2D();
                foreach (var line in segment.lines)
                {
                    float width = 0;
                    TransportInfo.TransportType tt = tls[line.Key.lineId].Info.m_transportType;
                    switch (tt)
                    {
                    case TransportInfo.TransportType.Tram:
                    case TransportInfo.TransportType.Bus:
                        width = 1;
                        break;

                    case TransportInfo.TransportType.Train:
                        width = 5;
                        break;

                    case TransportInfo.TransportType.Metro:
                        width = 2.5f;
                        break;

                    case TransportInfo.TransportType.Ship:
                        width = 8;
                        break;
                    }
                    float coordMultiplier = 0;
                    if (offsetNeg > offsetPos)
                    {
                        coordMultiplier = (offsetPos + width / 2);
                        offsetPos      += width;
                    }
                    else if (offsetNeg < offsetPos)
                    {
                        coordMultiplier = -(offsetNeg + width / 2);
                        offsetNeg      += width;
                    }
                    else
                    {
                        offsetPos = width / 2;
                        offsetNeg = width / 2;
                    }

                    var       lineTotalOffset = offsetDir * coordMultiplier * 2;
                    Vector2[] points          = new Vector2[basePoints.Count];
                    for (int i = 0; i < basePoints.Count; i++)
                    {
                        if (i == 0)
                        {
                            var cp = segment.s1.getDirectionForStation(segment.s2);
                            cp++;
                            cp++;
                            points[i] = basePoints[i] + cp.getCardinalOffset2D() * coordMultiplier * 2;
                        }
                        else if (i == basePoints.Count - 1)
                        {
                            var cp = segment.s2.getDirectionForStation(segment.s1);
                            cp++;
                            cp++;
                            points[i] = basePoints[i] + cp.getCardinalOffset2D() * coordMultiplier * 2;
                        }
                        else
                        {
                            points[i] = basePoints[i] + lineTotalOffset;
                        }
                    }
                    svgPart.AppendFormat(mainStyle, string.Join(" ", points.Select(x => "" + x.x + "," + x.y).ToArray()), line.Key.lineColor.r, line.Key.lineColor.g, line.Key.lineColor.b, tt.ToString(), line.Value);
                }
            }
        }
Example #10
0
        internal void drawAllLines()
        {
            TransportLine[] tls = Singleton <TransportManager> .instance.m_lines.m_buffer;
            List <LineSegmentStationsManager.LineSegmentStations> segments = segmentManager.getSegments();
            var segmentDict = new List <Tuple <float, string> >();

            foreach (LineSegmentStationsManager.LineSegmentStations segment in segments)
            {
                List <Vector2> basePoints = segment.path;
                for (int i = 0; i < basePoints.Count; i++)
                {
                    basePoints[i] = (basePoints[i] - offset) * multiplier;
                }
                float offsetNeg = 0;
                float offsetPos = 0;
                var   dir       = CardinalPoint.GetCardinal2D(segment.s1.centralPos, segment.s2.centralPos);
                dir++;
                dir++;
                Vector2 offsetDir = dir.GetCardinalOffset2D();
                foreach (KeyValuePair <MapTransportLine, LineSegmentStationsManager.Direction> line in segment.lines)
                {
                    float width = 0;
                    TransportInfo.TransportType tt = tls[line.Key.lineId].Info.m_transportType;
                    switch (tt)
                    {
                    case TransportInfo.TransportType.Tram:
                    case TransportInfo.TransportType.Bus:
                        width = 1;
                        break;

                    case TransportInfo.TransportType.Train:
                        width = 5;
                        break;

                    case TransportInfo.TransportType.Metro:
                        width = 2.5f;
                        break;

                    case TransportInfo.TransportType.Monorail:
                        width = 4;
                        break;

                    case TransportInfo.TransportType.Ship:
                        width = 8;
                        break;
                    }
                    float coordMultiplier = 0;
                    if (offsetNeg > offsetPos)
                    {
                        coordMultiplier = (offsetPos + width / 2);
                        offsetPos      += width;
                    }
                    else if (offsetNeg < offsetPos)
                    {
                        coordMultiplier = -(offsetNeg + width / 2);
                        offsetNeg      += width;
                    }
                    else
                    {
                        offsetPos = width / 2;
                        offsetNeg = width / 2;
                    }

                    Vector2 lineTotalOffset = offsetDir * coordMultiplier * 2;
                    var     points          = new Vector2[basePoints.Count];
                    for (int i = 0; i < basePoints.Count; i++)
                    {
                        if (i == 0)
                        {
                            CardinalPoint cp = segment.s1.getDirectionForStation(segment.s2);
                            cp++;
                            cp++;
                            points[i] = basePoints[i] + cp.GetCardinalOffset2D() * coordMultiplier * 2;
                        }
                        else if (i == basePoints.Count - 1)
                        {
                            CardinalPoint cp = segment.s2.getDirectionForStation(segment.s1);
                            cp++;
                            cp++;
                            points[i] = basePoints[i] + cp.GetCardinalOffset2D() * coordMultiplier * 2;
                        }
                        else
                        {
                            points[i] = basePoints[i] + lineTotalOffset;
                        }
                    }
                    segmentDict.Add(Tuple.New(width, getLineElement(points, line.Key, tt)));
                }
            }
            segmentDict.Sort((x, y) => (int)(y.First - x.First));
            svgPart.Append(string.Join("\n", segmentDict.Select(x => x.Second).ToArray()));
        }
        public List <Vector2> getPath(Vector2 originalPos, Vector2 toPos, CardinalPoint s1Exit, CardinalPoint s2Exit)
        {
            CardinalPoint currentDirection = s1Exit;

            List <Vector2> saida = new List <Vector2>();

            saida.Add(originalPos);



            //int iterationCount = 0;
            //DiagCheck:
            //iterationCount++;
            var     dirS1      = s1Exit.getCardinalOffset2D();
            var     dirS2      = s2Exit.getCardinalOffset2D();
            Vector2 currentPos = originalPos + dirS1;
            Vector2 targetPos  = toPos + dirS2;

            saida.Add(currentPos);

            TLMUtils.doLog("s1Exit = {0};dirS1 = {1}", s1Exit, dirS1);
            TLMUtils.doLog("s2Exit = {0};dirS2 = {1}", s2Exit, dirS2);

            var isHorizontalS1 = Math.Abs(dirS1.x) > Math.Abs(dirS1.y);
            var isVerticalS1   = Math.Abs(dirS1.x) < Math.Abs(dirS1.y);
            var isD1S1         = (dirS1.y + dirS1.x) == 0 && (dirS1.x - dirS1.y) != 0;
            var isD2S1         = (dirS1.x - dirS1.y) == 0 && (dirS1.y + dirS1.x) != 0;

            var isHorizontalS2 = Math.Abs(dirS2.x) > Math.Abs(dirS2.y);
            var isVerticalS2   = Math.Abs(dirS2.x) < Math.Abs(dirS2.y);
            var isD1S2         = (dirS2.y + dirS2.x) == 0 && (dirS2.x - dirS2.y) != 0;
            var isD2S2         = (dirS2.x - dirS2.y) == 0 && (dirS2.y + dirS2.x) != 0;

            #region D1S1
            if (isD1S1)
            {
                int indexSumS1 = (int)(currentPos.x + currentPos.y);
                if (isD1S2)
                {
                    TLMUtils.doLog("(D1 - D1)");
                    int indexSumS2 = (int)(targetPos.x + targetPos.y);
                    if (indexSumS1 != indexSumS2)
                    {
                        saida.AddRange(pathParallelDiag(dirS1, dirS2, currentPos, targetPos));
                    }
                }
                else if (isD2S2)
                {
                    TLMUtils.doLog("(D1 - D2)");
                    int indexSubS2 = (int)(targetPos.x - targetPos.y);
                    saida.AddRange(getMidPointsD1D2(currentPos, targetPos, dirS1, dirS2, indexSumS1, indexSubS2));
                }
                else if (isHorizontalS2)
                {
                    TLMUtils.doLog("(D1 - H)");
                    int s2y     = (int)targetPos.y;
                    int targetX = indexSumS1 - s2y;
                    if (!calcIntersecHV(currentPos, targetPos, dirS1, dirS2, targetX, s2y, ref saida))
                    {
                        TLMUtils.doLog("WORST CASE!");
                    }
                }
                else if (isVerticalS2)
                {
                    TLMUtils.doLog("(D1 - V)");
                    int s2x     = (int)targetPos.x;
                    int targetY = indexSumS1 - s2x;
                    if (!calcIntersecHV(targetPos, currentPos, dirS2, dirS1, s2x, targetY, ref saida))
                    {
                        TLMUtils.doLog("WORST CASE!");
                    }
                }
            }
            #endregion
            #region D2S1
            else if (isD2S1)
            {
                int indexSubS1 = (int)(currentPos.x - currentPos.y);
                if (isD1S2)
                {
                    TLMUtils.doLog("(D2 - D1)");
                    int indexSumS2 = (int)(currentPos.x + currentPos.y);
                    var points     = getMidPointsD1D2(targetPos, currentPos, dirS2, dirS1, indexSumS2, indexSubS1);
                    points.Reverse();
                    saida.AddRange(points);
                }
                else if (isD2S2)
                {
                    TLMUtils.doLog("(D2 - D2)");
                    int indexSubS2 = (int)(targetPos.x - targetPos.y);
                    if (indexSubS1 != indexSubS2)
                    {
                        saida.AddRange(pathParallelDiag(dirS1, dirS2, currentPos, targetPos));
                    }
                }
                else if (isHorizontalS2)
                {
                    TLMUtils.doLog("(D2 - H)");
                    int s2y     = (int)targetPos.y;
                    int targetX = indexSubS1 + s2y;
                    if (!calcIntersecHV(currentPos, targetPos, dirS1, dirS2, targetX, s2y, ref saida))
                    {
                        TLMUtils.doLog("WORST CASE!");
                    }
                }
                else if (isVerticalS2)
                {
                    TLMUtils.doLog("(D2 - V)");
                    int s2x     = (int)targetPos.x;
                    int targetY = indexSubS1 + s2x;
                    if (!calcIntersecHV(targetPos, currentPos, dirS2, dirS1, s2x, targetY, ref saida))
                    {
                        TLMUtils.doLog("WORST CASE!");
                    }
                }
            }
            #endregion
            #region HS1
            else if (isHorizontalS1)
            {
                int s1y = (int)(currentPos.y);
                if (isD1S2)
                {
                    TLMUtils.doLog("(H - D1)");
                    int indexSumS2 = (int)(targetPos.x + targetPos.y);
                    int targetX    = indexSumS2 - s1y;
                    if (!calcIntersecHV(targetPos, currentPos, dirS2, dirS1, targetX, s1y, ref saida))
                    {
                        TLMUtils.doLog("WORST CASE!");
                    }
                }
                else if (isD2S2)
                {
                    TLMUtils.doLog("(H - D2)");
                    int indexSubS2 = (int)(targetPos.x - targetPos.y);
                    int targetX    = indexSubS2 + s1y;
                    if (!calcIntersecHV(targetPos, currentPos, dirS2, dirS1, targetX, s1y, ref saida))
                    {
                        TLMUtils.doLog("WORST CASE!");
                    }
                }
                else if (isHorizontalS2)
                {
                    TLMUtils.doLog("(H - H)");
                    if (currentPos.x != targetPos.x)
                    {
                        saida.AddRange(pathParallel(dirS1, dirS2, currentPos, targetPos));
                    }
                }
                else if (isVerticalS2)
                {
                    TLMUtils.doLog("(H - V)");
                    int s2x = (int)targetPos.x;
                    if (!calcIntersecHV(targetPos, currentPos, dirS2, dirS1, s2x, s1y, ref saida))
                    {
                        TLMUtils.doLog("WORST CASE!");
                    }
                }
            }
            #endregion
            #region VS1
            else if (isVerticalS1)
            {
                int s1x = (int)(currentPos.x);
                if (isD1S2)
                {
                    TLMUtils.doLog("(V - D1)");
                    int indexSumS2 = (int)(targetPos.x + targetPos.y);
                    int targetY    = indexSumS2 - s1x;
                    if (!calcIntersecHV(currentPos, targetPos, dirS1, dirS2, s1x, targetY, ref saida))
                    {
                        TLMUtils.doLog("WORST CASE!");
                    }
                }
                else if (isD2S2)
                {
                    TLMUtils.doLog("(V - D2)");
                    int indexSubS2 = (int)(targetPos.x - targetPos.y);
                    int targetY    = s1x - indexSubS2;
                    if (!calcIntersecHV(currentPos, targetPos, dirS1, dirS2, s1x, targetY, ref saida))
                    {
                        TLMUtils.doLog("WORST CASE!");
                    }
                }
                else if (isHorizontalS2)
                {
                    TLMUtils.doLog("(V - H)");
                    int s2y = (int)targetPos.y;
                    if (!calcIntersecHV(currentPos, targetPos, dirS1, dirS2, s1x, s2y, ref saida))
                    {
                        TLMUtils.doLog("WORST CASE!");
                    }
                }
                else if (isVerticalS2)
                {
                    TLMUtils.doLog("(V - V)");
                    if (currentPos.y != targetPos.y)
                    {
                        saida.AddRange(pathParallel(dirS1, dirS2, currentPos, targetPos));
                    }
                }
            }
            #endregion
            //Vector2 offsetRemove = Vector2.zero;
            //if (isD1)
            //{
            //    //diag
            //    int index = (int)(diagPointEnd.y + diagPointEnd.x);
            //    var targetPointD1 = getFreeD1Point(currentPos, diagPointEnd);
            //    if (iterationCount > 4)
            //    {
            //        targetPointD1 = targetPos;
            //    }
            //    if (targetPointD1 == currentPos)
            //    {
            //        currentPos += currentDirection.getCardinalOffset2D();
            //        goto DiagCheck;
            //    }

            //    if (targetPointD1 != targetPos)
            //    {
            //        saida.Add(currentPos);
            //        saida.Add(targetPointD1);
            //        currentPos = targetPointD1;
            //        currentDirection = CardinalPoint.getCardinal2D(currentPos, targetPos);
            //        goto DiagCheck;
            //    }
            //}
            //else if (isD2)
            //{
            //    //diag
            //    int index = (int)(diagPointEnd.x - diagPointEnd.y);
            //    var targetPointD2 = getFreeD2Point(currentPos, diagPointEnd);
            //    if (iterationCount > 4)
            //    {
            //        targetPointD2 = targetPos;
            //    }
            //    if (targetPointD2 == currentPos)
            //    {
            //        currentPos += currentDirection.getCardinalOffset2D();
            //        goto DiagCheck;
            //    }
            //    if (targetPointD2 != targetPos)
            //    {
            //        saida.Add(currentPos);
            //        saida.Add(targetPointD2);
            //        currentPos = targetPointD2;
            //        currentDirection = CardinalPoint.getCardinal2D(currentPos, targetPos);
            //        goto DiagCheck;
            //    }
            //}
            //else if (isHorizontal)
            //{
            //    var targetPointX = getFreeHorizontal(currentPos, targetPos);
            //    if(iterationCount > 4)
            //    {
            //        targetPointX = targetPos;
            //    }
            //    if (targetPos != targetPointX)
            //    {
            //        if (targetPointX == currentPos)
            //        {
            //            currentPos += 0.5f * currentDirection.getCardinalOffset2D();
            //            saida.Add(currentPos);
            //            currentPos = targetPos - 0.5f * currentDirection.getCardinalOffset2D();
            //            saida.Add(currentPos);
            //            saida.Add(targetPos);
            //        }
            //        else {
            //            saida.Add(targetPointX);
            //            currentDirection++;
            //            currentPos = targetPointX + currentDirection.getCardinalOffset2D();
            //            saida.Add(currentPos);
            //            goto DiagCheck;
            //        }
            //    }
            //}
            //else if (isVertical)
            //{
            //    var targetPointY = getFreeVertical(currentPos, targetPos);
            //    if (iterationCount > 4)
            //    {
            //        targetPointY = targetPos;
            //    }
            //    if (targetPos != targetPointY)
            //    {
            //        if (targetPointY == currentPos)
            //        {
            //            currentPos += 0.5f * currentDirection.getCardinalOffset2D();
            //            saida.Add(currentPos);
            //            currentPos = targetPos - 0.5f * currentDirection.getCardinalOffset2D();
            //            saida.Add(currentPos);
            //            saida.Add(targetPos);
            //        }
            //        else {
            //            saida.Add(targetPointY);
            //            currentDirection++;
            //            currentPos = targetPointY + currentDirection.getCardinalOffset2D();
            //            saida.Add(currentPos);
            //            goto DiagCheck;
            //        }
            //    }
            //}

            saida.Add(targetPos);
            saida.Add(toPos);

            for (int i = 1; i < saida.Count; i++)
            {
                addSegmentToIndex(saida[i - 1], saida[i]);
            }
            if (TransportLinesManagerMod.instance == null || TransportLinesManagerMod.debugMode)
            {
                string points = string.Join(",", saida.Select(x => "(" + x.x + "," + x.y + ")").ToArray());
                TLMUtils.doLog("Points: [{0}]", points);
            }

            return(saida);
        }
Example #12
0
        private void GetPointsLine(ref Vector2 p1, ref Vector2 p2, ref CardinalPoint c1, ref CardinalPoint c2, ref List <Vector2> saida, int retrying = 0)
        {
            if (retrying > 7)
            {
                TLMUtils.doLog("MAX RETRYING REACHED!");
                return;
            }

            saida.Add(p1);

            CardinalPoint currentDirection = c1;


            //int iterationCount = 0;
            //DiagCheck:
            //iterationCount++;
            var dirS1 = c1.GetCardinalOffset2D();
            var dirS2 = c2.GetCardinalOffset2D();

            TLMUtils.doLog("c1 = {0};dirS1 = {1}", c1, dirS1);
            TLMUtils.doLog("c2 = {0};dirS2 = {1}", c2, dirS2);


            Vector2 currentPos = p1 + dirS1;
            Vector2 targetPos  = p2 + dirS2;

            saida.Add(currentPos);

            var isHorizontalS1 = Math.Abs(dirS1.x) > Math.Abs(dirS1.y);
            var isVerticalS1   = Math.Abs(dirS1.x) < Math.Abs(dirS1.y);
            var isD1S1         = (dirS1.y + dirS1.x) == 0 && (dirS1.x - dirS1.y) != 0;
            var isD2S1         = (dirS1.x - dirS1.y) == 0 && (dirS1.y + dirS1.x) != 0;

            var isHorizontalS2 = Math.Abs(dirS2.x) > Math.Abs(dirS2.y);
            var isVerticalS2   = Math.Abs(dirS2.x) < Math.Abs(dirS2.y);
            var isD1S2         = (dirS2.y + dirS2.x) == 0 && (dirS2.x - dirS2.y) != 0;
            var isD2S2         = (dirS2.x - dirS2.y) == 0 && (dirS2.y + dirS2.x) != 0;

            var Δx = targetPos.x - currentPos.x;
            var Δy = targetPos.y - currentPos.y;

            var iΔx = Math.Abs(Δx);
            var iΔy = Math.Abs(Δy);

            #region D1S1
            if (isD1S1)
            {
                int indexSumS1 = (int)(currentPos.x + currentPos.y);
                if (isD1S2)
                {
                    TLMUtils.doLog("(D1 - D1)");
                    int indexSumS2 = (int)(targetPos.x + targetPos.y);
                    if (indexSumS1 != indexSumS2)
                    {
                        saida.AddRange(pathParallelDiag(dirS1, dirS2, currentPos, targetPos));
                    }
                }
                else if (isD2S2)
                {
                    TLMUtils.doLog("(D1 - D2)");
                    int indexSubS2 = (int)(targetPos.x - targetPos.y);
                    saida.AddRange(getMidPointsD1D2(currentPos, targetPos, dirS1, dirS2, indexSumS1, indexSubS2));
                }
                else if (isHorizontalS2)
                {
                    TLMUtils.doLog("(D1 - H)");
                    int s2y     = (int)targetPos.y;
                    int targetX = indexSumS1 - s2y;
                    if (!calcIntersecHV(currentPos, targetPos, dirS1, dirS2, targetX, s2y, ref saida))
                    {
                        TLMUtils.doLog("WORST CASE! - RETRYING");
                        c1++;
                        GetPointsLine(ref currentPos, ref p2, ref c1, ref c2, ref saida, retrying++);
                    }
                }
                else if (isVerticalS2)
                {
                    TLMUtils.doLog("(D1 - V)");
                    int s2x     = (int)targetPos.x;
                    int targetY = indexSumS1 - s2x;
                    if (!calcIntersecHV(targetPos, currentPos, dirS2, dirS1, s2x, targetY, ref saida))
                    {
                        TLMUtils.doLog("WORST CASE - RETRYING");
                        c1--;
                        GetPointsLine(ref currentPos, ref p2, ref c1, ref c2, ref saida, retrying++);
                    }
                }
            }
            #endregion
            #region D2S1
            else if (isD2S1)
            {
                int indexSubS1 = (int)(currentPos.x - currentPos.y);
                if (isD1S2)
                {
                    TLMUtils.doLog("(D2 - D1)");
                    int indexSumS2 = (int)(currentPos.x + currentPos.y);
                    var points     = getMidPointsD1D2(targetPos, currentPos, dirS2, dirS1, indexSumS2, indexSubS1);
                    points.Reverse();
                    saida.AddRange(points);
                }
                else if (isD2S2)
                {
                    TLMUtils.doLog("(D2 - D2)");
                    int indexSubS2 = (int)(targetPos.x - targetPos.y);
                    if (indexSubS1 != indexSubS2)
                    {
                        saida.AddRange(pathParallelDiag(dirS1, dirS2, currentPos, targetPos));
                    }
                }
                else if (isHorizontalS2)
                {
                    TLMUtils.doLog("(D2 - H)");
                    int s2y     = (int)targetPos.y;
                    int targetX = indexSubS1 + s2y;
                    if (!calcIntersecHV(currentPos, targetPos, dirS1, dirS2, targetX, s2y, ref saida))
                    {
                        TLMUtils.doLog("WORST CASE! - RETRYING");
                        c1--;
                        GetPointsLine(ref currentPos, ref p2, ref c1, ref c2, ref saida, retrying++);
                    }
                }
                else if (isVerticalS2)
                {
                    TLMUtils.doLog("(D2 - V)");
                    int s2x     = (int)targetPos.x;
                    int targetY = indexSubS1 + s2x;
                    if (!calcIntersecHV(targetPos, currentPos, dirS2, dirS1, s2x, targetY, ref saida))
                    {
                        TLMUtils.doLog("WORST CASE! - RETRYING");
                        c1++;
                        GetPointsLine(ref currentPos, ref p2, ref c1, ref c2, ref saida, retrying++);
                    }
                }
            }
            #endregion
            #region HS1
            else if (isHorizontalS1)
            {
                int s1y = (int)(currentPos.y);
                if (isD1S2)
                {
                    TLMUtils.doLog("(H - D1)");
                    int indexSumS2 = (int)(targetPos.x + targetPos.y);
                    int targetX    = indexSumS2 - s1y;
                    if (!calcIntersecHV(targetPos, currentPos, dirS2, dirS1, targetX, s1y, ref saida))
                    {
                        TLMUtils.doLog("WORST CASE! - RETRYING");
                        c2++;
                        GetPointsLine(ref p1, ref targetPos, ref c1, ref c2, ref saida, retrying++);
                    }
                }
                else if (isD2S2)
                {
                    TLMUtils.doLog("(H - D2)");
                    int indexSubS2 = (int)(targetPos.x - targetPos.y);
                    int targetX    = indexSubS2 + s1y;
                    if (!calcIntersecHV(targetPos, currentPos, dirS2, dirS1, targetX, s1y, ref saida))
                    {
                        TLMUtils.doLog("WORST CASE!- RETRYING");
                        c2--;
                        GetPointsLine(ref p1, ref targetPos, ref c1, ref c2, ref saida, retrying++);
                    }
                }
                else if (isHorizontalS2)
                {
                    TLMUtils.doLog("(H - H)");
                    if (currentPos.x != targetPos.x)
                    {
                        saida.AddRange(pathParallel(dirS1, dirS2, currentPos, targetPos));
                    }
                }
                else if (isVerticalS2)
                {
                    TLMUtils.doLog("(H - V)");

                    if (iΔx != iΔy)
                    {
                        int s2x = (int)currentPos.x;
                        var ΔiΔ = Math.Abs(iΔx - iΔy);
                        if (iΔx > iΔy)
                        {
                            s2x += (int)(Math.Sign(Δx) * (iΔx - ΔiΔ));
                        }
                        else
                        {
                            s1y += (int)(Math.Sign(Δy) * (iΔy - ΔiΔ));
                        }
                        if (!calcIntersecHV(targetPos, currentPos, dirS2, dirS1, s2x, s1y, ref saida))
                        {
                            TLMUtils.doLog("WORST CASE! - RETRYING");
                            var nextP1 = new Vector2(p1.x + dirS1.x, p1.y + Math.Sign(Δy));
                            if (Math.Sign(Δy) == Math.Sign(dirS1.x))
                            {
                                c1++;
                            }
                            else
                            {
                                c1--;
                            }
                            GetPointsLine(ref nextP1, ref p2, ref c1, ref c2, ref saida, retrying++);
                        }
                    }
                }
            }
            #endregion
            #region VS1
            else if (isVerticalS1)
            {
                int s1x = (int)(currentPos.x);
                if (isD1S2)
                {
                    TLMUtils.doLog("(V - D1)");
                    int indexSumS2 = (int)(targetPos.x + targetPos.y);
                    int targetY    = indexSumS2 - s1x;
                    if (!calcIntersecHV(currentPos, targetPos, dirS1, dirS2, s1x, targetY, ref saida))
                    {
                        TLMUtils.doLog("WORST CASE! - RETRYING");
                        c2--;
                        GetPointsLine(ref p1, ref targetPos, ref c1, ref c2, ref saida, retrying++);
                    }
                }
                else if (isD2S2)
                {
                    TLMUtils.doLog("(V - D2)");
                    int indexSubS2 = (int)(targetPos.x - targetPos.y);
                    int targetY    = s1x - indexSubS2;
                    if (!calcIntersecHV(currentPos, targetPos, dirS1, dirS2, s1x, targetY, ref saida))
                    {
                        TLMUtils.doLog("WORST CASE! - RETRYING");
                        c2++;
                        GetPointsLine(ref p1, ref targetPos, ref c1, ref c2, ref saida, retrying++);
                    }
                }
                else if (isHorizontalS2)
                {
                    TLMUtils.doLog("(V - H)");
                    if (iΔx != iΔy)
                    {
                        int s2y = (int)targetPos.y;
                        var ΔiΔ = Math.Abs(iΔx - iΔy);
                        if (iΔx > iΔy)
                        {
                            s1x += (int)(Math.Sign(Δx) * (iΔx - ΔiΔ));
                        }
                        else
                        {
                            s2y += (int)(Math.Sign(Δy) * (iΔy - ΔiΔ));
                        }
                        if (!calcIntersecHV(targetPos, currentPos, dirS2, dirS1, s1x, s2y, ref saida))
                        {
                            TLMUtils.doLog("WORST CASE - RETRYING!");
                            var nextP1 = new Vector2(p1.x + Math.Sign(Δx), p1.y + dirS1.y);
                            if (Math.Sign(Δx) == Math.Sign(dirS1.y))
                            {
                                c1--;
                            }
                            else
                            {
                                c1++;
                            }
                            GetPointsLine(ref nextP1, ref p2, ref c1, ref c2, ref saida, retrying++);
                        }
                    }
                }
                else if (isVerticalS2)
                {
                    TLMUtils.doLog("(V - V)");
                    if (currentPos.y != targetPos.y)
                    {
                        saida.AddRange(pathParallel(dirS1, dirS2, currentPos, targetPos));
                    }
                }
            }
            #endregion

            saida.Add(targetPos);
            saida.Add(p2);
        }