Beispiel #1
0
        public static Astroid CalcDegree(Astroid station, Astroid astroid)
        {
            var origin = station.Point;
            var target = astroid.Point;
            var degree = Math.Atan2(target.X - origin.X, target.Y - origin.Y) * (180 / Math.PI);

            astroid.Degree = degree;
            return(astroid);
        }
Beispiel #2
0
        private static int CheckSight(Astroid a, Astroid b)
        {
            if (!a.Candidates.Contains(b.Point))
            {
                return(0);
            }
            a.Candidates.Remove(b.Point);

            if (a.Equals(b))
            {
                return(0);
            }


            a.Debug.Add($"Checking line of sight for: {a.Point} -> {b.Point}");

            //if (a.Point.Equals(new Point(0, 0)) && b.Point.Equals(new Point(6, 2)))
            //{
            //    Debugger.Break();
            //}

            var xDistance = Math.Abs(a.Point.X - b.Point.X);
            var yDistance = Math.Abs(a.Point.Y - b.Point.Y);
            var xDivisor  = 1;
            var yDivisor  = 1;

            if (xDistance != yDistance)
            {
                var gcd = GCD(xDistance, yDistance);
                xDivisor = xDistance / gcd;
                yDivisor = yDistance / gcd;
            }

            var xDirection = (a.Point.X == b.Point.X) ? 0 : (a.Point.X < b.Point.X) ? 1 : -1;
            var yDirection = (a.Point.Y == b.Point.Y) ? 0 : (a.Point.Y < b.Point.Y) ? 1 : -1;

            if (a.Point.X == b.Point.X)
            {
                yDivisor = 1;
            }
            if (a.Point.Y == b.Point.Y)
            {
                xDivisor = 1;
            }

            a.Debug.Add($"Step: {xDivisor * xDirection} {yDivisor * yDirection}");
            var step = BuildStep(xDivisor * xDirection, yDivisor * yDirection);

            var current           = a.Point;
            var minX              = Math.Min(a.Point.X, b.Point.X);
            var maxX              = Math.Max(a.Point.X, b.Point.X);
            var minY              = Math.Min(a.Point.Y, b.Point.Y);
            var maxY              = Math.Max(a.Point.Y, b.Point.Y);
            var potentialBlockers = new List <Point>();

            do
            {
                current = step(current);
                if (current.Equals(b.Point))
                {
                    if (potentialBlockers.Count == 0)
                    {
                        a.Debug.Add($"Visible (1): {b.Point}");
                        a.Visible.Add(b.Point);
                    }
                    else
                    {
                        a.Debug.Add($"BLOCKED: {b.Point} (by {string.Join(",", potentialBlockers)})");
                        a.Blocked.Add(b.Point);
                    }

                    return(0);
                }
                if (a.AstroidsDic.TryGetValue(current, out var blockingAstroid))
                {
                    potentialBlockers.Add(current);
                }
                else
                {
                    a.Debug.Add($"{current} not blocking: {b.Point}");
                }
            } while (current != b.Point && minX <= current.X && current.X <= maxX && minY <= current.Y && current.Y <= maxY);


            a.Debug.Add($"Visible (2): {b.Point}");
            a.Visible.Add(b.Point);

            return(0);
        }
Beispiel #3
0
 protected bool Equals(Astroid other)
 {
     return(Point.Equals(other.Point));
 }