Ejemplo n.º 1
0
        public void ParseObject()
        {
            object value  = "{X=1,Y=2}";
            var    actual = PointExtensions.Parse(value);

            Assert.AreEqual(new Point(1, 2), actual);
        }
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is not MouseEventArgs e)
            {
                throw new Exception("Wrong value type");
            }

            System.Windows.Point p = e.GetPosition((IInputElement)e.Source);
            bool modKey            = Keyboard.Modifiers.HasFlag(ModifierKeys.Shift);

            if (value is MouseButtonEventArgs b)
            {
                if (b.LeftButton == MouseButtonState.Pressed)
                {
                    return(new CanvasEventArgs(PointExtensions.CreateFromPoint(p), MouseState.DOWN, modKey));
                }

                if (b.LeftButton == MouseButtonState.Released)
                {
                    return(new CanvasEventArgs(PointExtensions.CreateFromPoint(p), MouseState.UP, modKey));
                }
            }

            return(new CanvasEventArgs(PointExtensions.CreateFromPoint(p), MouseState.MOVE, modKey));
        }
Ejemplo n.º 3
0
        public void GetHashCodeTest3()
        {
            var pointA = new Point(0, (1 << 16) + 1);
            var pointB = new Point(0, 1);

            Assert.AreNotEqual(PointExtensions.GetHashCode(pointA), PointExtensions.GetHashCode(pointB));
        }
        public void PolarToCartesian_Returns()
        {
            var angle         = 22.6;
            var radius        = 13;
            var expectedPoint = new Point(12, 5);

            Assert.AreEqual(expectedPoint, PointExtensions.PolarToCartesian(angle, radius));
        }
Ejemplo n.º 5
0
        public void GetHashCodeTest1()
        {
            Point point    = new Point();
            int   expected = 0;
            int   actual;

            actual = PointExtensions.GetHashCode(point);
            Assert.AreEqual(expected, actual);
        }
Ejemplo n.º 6
0
        public void AreCollinearTest1()
        {
            Point[] points   = null;
            bool    expected = true;
            bool    actual;

            actual = PointExtensions.AreCollinear(points);
            Assert.AreEqual(expected, actual);
        }
Ejemplo n.º 7
0
        public void AreCollinearTest3()
        {
            Point[] points   = new Point[] { new Point(0, 0), new Point(1, 1), new Point(2, 2) };
            bool    expected = true;
            bool    actual;

            actual = PointExtensions.AreCollinear(points);
            Assert.AreEqual(expected, actual);
        }
Ejemplo n.º 8
0
        private List <DPoint> GetHullPoints(PointLabels[] include, PointLabels[] exclude)
        {
            var result = new List <DPoint>();

            var exclusionZones = GetExclusionPolygons();

            for (int y = 0; y < Space.StandardHeight; y++)
            {
                for (int x = 0; x < Space.StandardWidth; x++)
                {
                    var addPt = false;

                    //Check if included
                    for (int inc = 0; inc < include.Length; inc++)
                    {
                        if (PointExtensions.IsInConvexPolygon(x, y, ConvexHullsStandard[include[inc]]))
                        {
                            addPt = true;
                            break;
                        }
                    }

                    //Check if excluded
                    if (addPt)
                    {
                        for (int exc = 0; exc < exclude.Length; exc++)
                        {
                            if (PointExtensions.IsInConvexPolygon(x, y, ConvexHullsStandard[exclude[exc]]))
                            {
                                addPt = false;
                                break;
                            }
                        }
                    }

                    //Check if in one of the user exclusion zones
                    if (addPt)
                    {
                        for (int exc = 0; exc < exclusionZones.Count; exc++)
                        {
                            if (PointExtensions.IsPointInPolygon(x, y, exclusionZones[exc]))
                            {
                                addPt = false;
                                break;
                            }
                        }
                    }

                    if (addPt)
                    {
                        result.Add(new DPoint(x, y));
                    }
                }
            }

            return(result);
        }
Ejemplo n.º 9
0
        public void EqualsTest1()
        {
            Point pointA   = new Point();
            Point pointB   = new Point();
            bool  expected = true;
            bool  actual;

            actual = PointExtensions.Equals(pointA, pointB);
            Assert.AreEqual(expected, actual);
        }
Ejemplo n.º 10
0
        public void EqualsTest3()
        {
            Point pointA   = new Point(1, 0);
            Point pointB   = new Point(0, 1);
            bool  expected = false;
            bool  actual;

            actual = PointExtensions.Equals(pointA, pointB);
            Assert.AreEqual(expected, actual);
        }
Ejemplo n.º 11
0
        public void Given_CollinearPointsOnDiagonal_When_ArePointsCollinearCalled_Then_ResultIsTrue()
        {
            //given
            var p1 = new Point(0, 0, PointType.Golfer);
            var p2 = new Point(1, 1, PointType.Golfer);
            var p3 = new Point(10, 10, PointType.Hole);

            //when
            var result = PointExtensions.ArePointsCollinear(p1, p2, p3);

            //then
            Assert.IsTrue(result);
        }
Ejemplo n.º 12
0
        public void Given_NoncollinearPoints_When_ArePointsCollinearCalled_Then_ResultIsFalse()
        {
            //given
            var p1 = new Point(0, 0, PointType.Golfer);
            var p2 = new Point(1, 0, PointType.Golfer);
            var p3 = new Point(10, 7.5, PointType.Hole);

            //when
            var result = PointExtensions.ArePointsCollinear(p1, p2, p3);

            //then
            Assert.IsFalse(result);
        }
        private Texture2D GetScreenshot(string filePath)
        {
            Texture2D texture   = null;
            var       completed = false;
            var       timeout   = DateTime.Now.AddMilliseconds(FileTimeOutMilliseconds);

            while (!completed)
            {
                if (!File.Exists(filePath))
                {
                    return(null);
                }
                try {
                    using (var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read)) {
                        using (var source = Image.FromStream(fs)) {
                            var maxWidth  = GameService.Graphics.Resolution.X - 100;
                            var maxHeight = GameService.Graphics.Resolution.Y - 100;
                            var(width, height) = PointExtensions.ResizeKeepAspect(
                                new Point(source.Width, source.Height), maxWidth,
                                maxHeight);
                            using (var target = new Bitmap(source, width, height)) {
                                using (var graphic = Graphics.FromImage(target)) {
                                    graphic.CompositingQuality = CompositingQuality.HighSpeed;
                                    graphic.InterpolationMode  = InterpolationMode.HighQualityBicubic;
                                    graphic.SmoothingMode      = SmoothingMode.HighSpeed;
                                    graphic.DrawImage(target, 0, 0, width, height);
                                }
                                using (var textureStream = new MemoryStream()) {
                                    target.Save(textureStream, ImageFormat.Jpeg);
                                    var buffer = new byte[textureStream.Length];
                                    textureStream.Position = 0;
                                    textureStream.Read(buffer, 0, buffer.Length);
                                    texture = Texture2D.FromStream(GameService.Graphics.GraphicsDevice, textureStream);
                                }
                            }
                        }
                    }
                    completed = true;
                } catch (IOException e) {
                    if (DateTime.Now < timeout)
                    {
                        continue;
                    }
                    Logger.Error(e.Message + e.StackTrace);
                    return(null);
                }
            }

            return(texture);
        }
Ejemplo n.º 14
0
        /// <summary>
        /// Initializes a discrete grid using the information provided.
        /// </summary>
        /// <param name="startPoint">The point to start the route at</param>
        /// <param name="endPoint">The point to end the route at</param>
        /// <param name="field">The field that needs to be discretised</param>
        /// <param name="gridSize">The size of the grid to be palced placed over <paramref name="field"/></param>
        /// <param name="movingObject">The object that will be moving.</param>
        /// <returns>A GridSquare array, initialized to represent <paramref name="field"/>.</returns>
        ///
        /// Produces a GridSquare array that is set up for use by <see cref="FindPath"/>.
        ///
        /// Uses the ObjectClearance and <paramref name="movingObject" />'s <see cref="IPositionedObject.Size"/> property to determine the
        /// apparent size of opponents, and marks the squares that contain them as <see cref="SquareType.Obstacle"/>.
        ///
        /// Marks the square that contains <paramref name="endPoint" /> as <see cref="SquareType.Destination"/>.
        ///
        /// Marks the square that contains <paramref name="startPoint" /> as <see cref="SquareType.Origin"/>.
        ///
        /// Works in parallel as far as possible, using the Microsoft Task Parallel Library (http://msdn.microsoft.com/en-us/library/dd460717.aspx).
        protected GridSquare[] InitGrid(PointF startPoint, PointF endPoint, Field field, Size gridSize, IPositionedObject movingObject)
        {
            var playersize = (movingObject.Size + new Size(ObjectClearance, ObjectClearance)).Scale(Resolution).Scale(2.0f).Ceiling();
            var clearance  = Math.Max(playersize.Width, playersize.Height); // The amount to increase an obstacle's size by to allow for the player's size

            var grid = new GridSquare[gridSize.Height * gridSize.Width];

            // Initialize the grid
            Parallel.For(0, grid.Length, i => {
                grid[i] = new GridSquare
                {
                    Location = PointExtensions.FromIndex(i, gridSize.Width)
                };
            });

            Parallel.ForEach(from p in field.Players where p.Team == Team.Opposition select p, player =>
            {
                var centerGridPoint = player.Position.Scale(Resolution).Floor();

                var minX = Math.Max(0, centerGridPoint.X - playersize.Width - clearance);
                var maxX = Math.Min(centerGridPoint.X + playersize.Width + clearance, gridSize.Width);
                var minY = Math.Max(0, centerGridPoint.Y - playersize.Height - clearance);
                var maxY = Math.Min(centerGridPoint.Y + playersize.Height + clearance, gridSize.Height);

                for (var i = minX; i < maxX; i++)
                {
                    for (var j = minY; j < maxY; j++)
                    {
                        if (i < 0 || j < 0)
                        {
                            continue;
                        }

                        var gridPoint = new Point(i, j);
                        grid[gridPoint.ToIndex(gridSize.Width)].Type = SquareType.Obstacle;
                    }
                }
            });

            var gridEndPoint = endPoint.Scale(Resolution).Floor();

            grid[gridEndPoint.ToIndex(gridSize.Width)].Type = SquareType.Destination;

            var gridStartPoint = startPoint.Scale(Resolution).Floor();

            grid[gridStartPoint.ToIndex(gridSize.Width)].Type = SquareType.Origin;

            return(grid);
        }
Ejemplo n.º 15
0
        public void ParseEmptyString()
        {
            var actual = PointExtensions.Parse(String.Empty);

            Assert.AreEqual(Point.Empty, actual);
        }
Ejemplo n.º 16
0
        public void ParseNull()
        {
            var actual = PointExtensions.Parse(null);

            Assert.AreEqual(Point.Empty, actual);
        }
Ejemplo n.º 17
0
        public void ParsePositives()
        {
            var actual = PointExtensions.Parse("{X=1,Y=2}");

            Assert.AreEqual(new Point(1, 2), actual);
        }
Ejemplo n.º 18
0
        public void ParseZeroes()
        {
            var actual = PointExtensions.Parse("{X=0,Y=0}");

            Assert.AreEqual(Point.Empty, actual);
        }
Ejemplo n.º 19
0
        public void ParseManyDigits()
        {
            var actual = PointExtensions.Parse("{X=12345,Y=67890}");

            Assert.AreEqual(new Point(12345, 67890), actual);
        }
Ejemplo n.º 20
0
        public void ParseNegatives()
        {
            var actual = PointExtensions.Parse("{X=-1,Y=-2}");

            Assert.AreEqual(new Point(-1, -2), actual);
        }
Ejemplo n.º 21
0
 public void ParseOneNegativeOnePositive()
 {
     Assert.AreEqual(new Point(1, -2), PointExtensions.Parse("{X=1,Y=-2}"));
     Assert.AreEqual(new Point(-1, 2), PointExtensions.Parse("{X=-1,Y=2}"));
 }
Ejemplo n.º 22
0
        public void ParseWithSpaceAfterComma()
        {
            var actual = PointExtensions.Parse("{X=1, Y=2}");

            Assert.AreEqual(new Point(1, 2), actual);
        }
Ejemplo n.º 23
0
 /// <summary>
 /// TODO: write documentation
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <typeparam name="C"></typeparam>
 /// <param name="xValues"></param>
 /// <param name="yValues"></param>
 /// <returns></returns>
 public static Vector <T, C> EstimateLeastSquaresCoefficients <T, C>(IList <T> xValues, IList <T> yValues) where C : ICalc <T>, new()
 {
     return(EstimateLeastSquaresCoefficients <T, C>(PointExtensions.convertToListOfPairs(xValues, yValues)));
 }