Exemplo n.º 1
0
        public void PointOnLineSide1()
        {
            var random = new Random(666);

            for (var i = 0; i < 1000; i++)
            {
                var startX = -1 - 666 * random.NextDouble();
                var endX   = +1 + 666 * random.NextDouble();

                var pointX     = 666 * random.NextDouble() - 333;
                var frontSideY = -1 - 666 * random.NextDouble();
                var backSideY  = -frontSideY;

                var vertex1 = new Vertex(Fixed.FromDouble(startX), Fixed.Zero);
                var vertex2 = new Vertex(Fixed.FromDouble(endX - startX), Fixed.Zero);

                var line = new LineDef(
                    vertex1,
                    vertex2,
                    0, 0, 0, null, null);

                var x = Fixed.FromDouble(pointX);
                {
                    var y = Fixed.FromDouble(frontSideY);
                    Assert.AreEqual(0, Geometry.PointOnLineSide(x, y, line));
                }
                {
                    var y = Fixed.FromDouble(backSideY);
                    Assert.AreEqual(1, Geometry.PointOnLineSide(x, y, line));
                }
            }
        }
Exemplo n.º 2
0
        public void PointOnSide2()
        {
            var random = new Random(666);

            for (var i = 0; i < 1000; i++)
            {
                var startY = +1 + 666 * random.NextDouble();
                var endY   = -1 - 666 * random.NextDouble();

                var pointY     = 666 * random.NextDouble() - 333;
                var frontSideX = -1 - 666 * random.NextDouble();
                var backSideX  = -frontSideX;

                var node = new Node(
                    Fixed.Zero,
                    Fixed.FromDouble(startY),
                    Fixed.Zero,
                    Fixed.FromDouble(endY - startY),
                    Fixed.Zero, Fixed.Zero, Fixed.Zero, Fixed.Zero,
                    Fixed.Zero, Fixed.Zero, Fixed.Zero, Fixed.Zero,
                    0, 0);

                var y = Fixed.FromDouble(pointY);
                {
                    var x = Fixed.FromDouble(frontSideX);
                    Assert.AreEqual(0, Geometry.PointOnSide(x, y, node));
                }
                {
                    var x = Fixed.FromDouble(backSideX);
                    Assert.AreEqual(1, Geometry.PointOnSide(x, y, node));
                }
            }
        }
Exemplo n.º 3
0
        public void PointOnDivLineSide2()
        {
            var random = new Random(666);

            for (var i = 0; i < 1000; i++)
            {
                var startY = +1 + 666 * random.NextDouble();
                var endY   = -1 - 666 * random.NextDouble();

                var pointY     = 666 * random.NextDouble() - 333;
                var frontSideX = -1 - 666 * random.NextDouble();
                var backSideX  = -frontSideX;

                var vertex1 = new Vertex(Fixed.Zero, Fixed.FromDouble(startY));
                var vertex2 = new Vertex(Fixed.Zero, Fixed.FromDouble(endY - startY));

                var line = new LineDef(
                    vertex1,
                    vertex2,
                    0, 0, 0, null, null);

                var divLine = new DivLine();
                divLine.MakeFrom(line);

                var y = Fixed.FromDouble(pointY);
                {
                    var x = Fixed.FromDouble(frontSideX);
                    Assert.AreEqual(0, Geometry.PointOnDivLineSide(x, y, divLine));
                }
                {
                    var x = Fixed.FromDouble(backSideX);
                    Assert.AreEqual(1, Geometry.PointOnDivLineSide(x, y, divLine));
                }
            }
        }
Exemplo n.º 4
0
        public void PointOnSegSide2()
        {
            var random = new Random(666);

            for (var i = 0; i < 1000; i++)
            {
                var startY = +1 + 666 * random.NextDouble();
                var endY   = -1 - 666 * random.NextDouble();

                var pointY     = 666 * random.NextDouble() - 333;
                var frontSideX = -1 - 666 * random.NextDouble();
                var backSideX  = -frontSideX;

                var vertex1 = new Vertex(Fixed.Zero, Fixed.FromDouble(startY));
                var vertex2 = new Vertex(Fixed.Zero, Fixed.FromDouble(endY - startY));

                var seg = new Seg(
                    vertex1,
                    vertex2,
                    Fixed.Zero, Angle.Ang0, null, null, null, null);

                var y = Fixed.FromDouble(pointY);
                {
                    var x = Fixed.FromDouble(frontSideX);
                    Assert.AreEqual(0, Geometry.PointOnSegSide(x, y, seg));
                }
                {
                    var x = Fixed.FromDouble(backSideX);
                    Assert.AreEqual(1, Geometry.PointOnSegSide(x, y, seg));
                }
            }
        }
Exemplo n.º 5
0
        public void DivLineSide3()
        {
            var random = new Random(666);

            for (var i = 0; i < 1000; i++)
            {
                var startX = -1 - 666 * random.NextDouble();
                var endX   = +1 + 666 * random.NextDouble();

                var pointX     = 666 * random.NextDouble() - 333;
                var frontSideY = -1 - 666 * random.NextDouble();
                var backSideY  = -frontSideY;

                for (var j = 0; j < 100; j++)
                {
                    var theta = 2 * Math.PI * random.NextDouble();
                    var ox    = 666 * random.NextDouble() - 333;
                    var oy    = 666 * random.NextDouble() - 333;

                    var vertex1 = new Vertex(
                        Fixed.FromDouble(ox + startX * Math.Cos(theta)),
                        Fixed.FromDouble(oy + startX * Math.Sin(theta)));

                    var vertex2 = new Vertex(
                        vertex1.X + Fixed.FromDouble((endX - startX) * Math.Cos(theta)),
                        vertex1.Y + Fixed.FromDouble((endX - startX) * Math.Sin(theta)));

                    var line = new LineDef(
                        vertex1,
                        vertex2,
                        0, 0, 0, null, null);

                    var divLine = new DivLine();
                    divLine.MakeFrom(line);

                    var node = new Node(
                        Fixed.FromDouble(ox + startX * Math.Cos(theta)),
                        Fixed.FromDouble(oy + startX * Math.Sin(theta)),
                        Fixed.FromDouble((endX - startX) * Math.Cos(theta)),
                        Fixed.FromDouble((endX - startX) * Math.Sin(theta)),
                        Fixed.Zero, Fixed.Zero, Fixed.Zero, Fixed.Zero,
                        Fixed.Zero, Fixed.Zero, Fixed.Zero, Fixed.Zero,
                        0, 0);

                    {
                        var x = Fixed.FromDouble(ox + pointX * Math.Cos(theta) - frontSideY * Math.Sin(theta));
                        var y = Fixed.FromDouble(oy + pointX * Math.Sin(theta) + frontSideY * Math.Cos(theta));
                        Assert.AreEqual(0, Geometry.DivLineSide(x, y, divLine));
                        Assert.AreEqual(0, Geometry.DivLineSide(x, y, node));
                    }
                    {
                        var x = Fixed.FromDouble(ox + pointX * Math.Cos(theta) - backSideY * Math.Sin(theta));
                        var y = Fixed.FromDouble(oy + pointX * Math.Sin(theta) + backSideY * Math.Cos(theta));
                        Assert.AreEqual(1, Geometry.DivLineSide(x, y, divLine));
                        Assert.AreEqual(1, Geometry.DivLineSide(x, y, node));
                    }
                }
            }
        }
Exemplo n.º 6
0
        public void BoxOnLineSide2()
        {
            var random = new Random(666);

            for (var i = 0; i < 1000; i++)
            {
                var radius = 33 + 33 * random.NextDouble();

                var startY = +1 + 666 * random.NextDouble();
                var endY   = -1 - 666 * random.NextDouble();

                var pointY     = 666 * random.NextDouble() - 333;
                var frontSideX = -1 - radius - 666 * random.NextDouble();
                var backSideX  = -frontSideX;
                var crossingX  = radius * 1.9 * (random.NextDouble() - 0.5);

                var frontBox = new Fixed[]
                {
                    Fixed.FromDouble(pointY + radius),
                    Fixed.FromDouble(pointY - radius),
                    Fixed.FromDouble(frontSideX - radius),
                    Fixed.FromDouble(frontSideX + radius)
                };

                var backBox = new Fixed[]
                {
                    Fixed.FromDouble(pointY + radius),
                    Fixed.FromDouble(pointY - radius),
                    Fixed.FromDouble(backSideX - radius),
                    Fixed.FromDouble(backSideX + radius)
                };

                var crossingBox = new Fixed[]
                {
                    Fixed.FromDouble(pointY + radius),
                    Fixed.FromDouble(pointY - radius),
                    Fixed.FromDouble(crossingX - radius),
                    Fixed.FromDouble(crossingX + radius)
                };

                var vertex1 = new Vertex(Fixed.Zero, Fixed.FromDouble(startY));
                var vertex2 = new Vertex(Fixed.Zero, Fixed.FromDouble(endY - startY));

                var line = new LineDef(
                    vertex1,
                    vertex2,
                    0, 0, 0, null, null);

                Assert.AreEqual(0, Geometry.BoxOnLineSide(frontBox, line));
                Assert.AreEqual(1, Geometry.BoxOnLineSide(backBox, line));
                Assert.AreEqual(-1, Geometry.BoxOnLineSide(crossingBox, line));
            }
        }
Exemplo n.º 7
0
        public void PointInSubsectorMap01()
        {
            using (var resource = CommonResource.CreateDummy(WadPath.Doom2))
            {
                var options = new GameOptions();
                var players = DoomTest.GetDefaultPlayers();
                var world   = new World(resource, options, players);
                var map     = new Map(resource, world);

                var ok    = 0;
                var count = 0;

                foreach (var subsector in map.Subsectors)
                {
                    for (var i = 0; i < subsector.SegCount; i++)
                    {
                        var seg = map.Segs[subsector.FirstSeg + i];

                        var p1x = seg.Vertex1.X.ToDouble();
                        var p1y = seg.Vertex1.Y.ToDouble();
                        var p2x = seg.Vertex2.X.ToDouble();
                        var p2y = seg.Vertex2.Y.ToDouble();

                        var dx     = p2x - p1x;
                        var dy     = p2y - p1y;
                        var length = Math.Sqrt(dx * dx + dy * dy);

                        var centerX = (p1x + p2x) / 2;
                        var centerY = (p1y + p2y) / 2;
                        var stepX   = dy / length;
                        var stepY   = -dx / length;

                        var targetX = centerX + 3 * stepX;
                        var targetY = centerY + 3 * stepY;

                        var fx = Fixed.FromDouble(targetX);
                        var fy = Fixed.FromDouble(targetY);

                        var result = Geometry.PointInSubsector(fx, fy, map);

                        if (result == subsector)
                        {
                            ok++;
                        }
                        count++;
                    }
                }

                Assert.IsTrue((double)ok / count >= 0.995);
            }
        }
Exemplo n.º 8
0
        public void Sign2()
        {
            var random = new Random(666);

            for (var i = 0; i < 100; i++)
            {
                var a = 666 * random.NextDouble() - 333;

                var fa = Fixed.FromDouble(a);

                Assert.AreEqual(+a, (+fa).ToDouble(), delta);
                Assert.AreEqual(-a, (-fa).ToDouble(), delta);
            }
        }
Exemplo n.º 9
0
        public void Abs2()
        {
            var random = new Random(666);

            for (var i = 0; i < 100; i++)
            {
                var a = 666 * random.NextDouble() - 333;
                var b = Math.Abs(a);

                var fa = Fixed.FromDouble(a);
                var fb = Fixed.Abs(fa);

                Assert.AreEqual(b, fb.ToDouble(), delta);
            }
        }
Exemplo n.º 10
0
        public void Multiplication4()
        {
            var random = new Random(666);

            for (var i = 0; i < 100; i++)
            {
                var a = 66 * random.NextDouble() - 33;
                var b = random.Next(66) - 33;
                var c = a * b;

                var fa = Fixed.FromDouble(a);
                var fc = fa * b;

                Assert.AreEqual(c, fc.ToDouble(), delta);
            }
        }
Exemplo n.º 11
0
        public void Division3()
        {
            var random = new Random(666);

            for (var i = 0; i < 100; i++)
            {
                var a = random.Next(66) - 33;
                var b = (2 * random.Next(2) - 1) * (33 * random.NextDouble() + 33);
                var c = a / b;

                var fb = Fixed.FromDouble(b);
                var fc = a / fb;

                Assert.AreEqual(c, fc.ToDouble(), delta);
            }
        }
Exemplo n.º 12
0
        public void Division4()
        {
            var random = new Random(666);

            for (var i = 0; i < 100; i++)
            {
                var a = 66 * random.NextDouble() - 33;
                var b = (2 * random.Next(2) - 1) * (random.Next(33) + 33);
                var c = a / b;

                var fa = Fixed.FromDouble(a);
                var fc = fa / b;

                Assert.AreEqual(c, fc.ToDouble(), delta);
            }
        }
Exemplo n.º 13
0
        public void ToInt1()
        {
            var random = new Random(666);

            for (var i = 0; i < 100; i++)
            {
                var a = random.Next(666);

                var fa       = Fixed.FromDouble(a);
                var ffloor   = fa.ToIntFloor();
                var fceiling = fa.ToIntCeiling();

                Assert.AreEqual(a, ffloor, 1.0E-9);
                Assert.AreEqual(a, fceiling, 1.0E-9);
            }
        }
Exemplo n.º 14
0
        public void Conversion()
        {
            var random = new Random(666);

            for (var i = 0; i < 100; i++)
            {
                var da = 666 * random.NextDouble() - 333;
                var sa = (float)da;

                var fda = Fixed.FromDouble(da);
                var fsa = Fixed.FromFloat(sa);

                Assert.AreEqual(da, fda.ToDouble(), delta);
                Assert.AreEqual(sa, fsa.ToFloat(), delta);
            }
        }
Exemplo n.º 15
0
        public void Subtraction2()
        {
            var random = new Random(666);

            for (var i = 0; i < 100; i++)
            {
                var a = 666 * random.NextDouble() - 333;
                var b = 666 * random.NextDouble() - 333;
                var c = a - b;

                var fa = Fixed.FromDouble(a);
                var fb = Fixed.FromDouble(b);
                var fc = fa - fb;

                Assert.AreEqual(c, fc.ToDouble(), delta);
            }
        }
Exemplo n.º 16
0
        public void PointOnSegSide3()
        {
            var random = new Random(666);

            for (var i = 0; i < 1000; i++)
            {
                var startX = -1 - 666 * random.NextDouble();
                var endX   = +1 + 666 * random.NextDouble();

                var pointX     = 666 * random.NextDouble() - 333;
                var frontSideY = -1 - 666 * random.NextDouble();
                var backSideY  = -frontSideY;

                for (var j = 0; j < 100; j++)
                {
                    var theta = 2 * Math.PI * random.NextDouble();
                    var ox    = 666 * random.NextDouble() - 333;
                    var oy    = 666 * random.NextDouble() - 333;

                    var vertex1 = new Vertex(
                        Fixed.FromDouble(ox + startX * Math.Cos(theta)),
                        Fixed.FromDouble(oy + startX * Math.Sin(theta)));

                    var vertex2 = new Vertex(
                        vertex1.X + Fixed.FromDouble((endX - startX) * Math.Cos(theta)),
                        vertex1.Y + Fixed.FromDouble((endX - startX) * Math.Sin(theta)));

                    var seg = new Seg(
                        vertex1,
                        vertex2,
                        Fixed.Zero, Angle.Ang0, null, null, null, null);

                    {
                        var x = Fixed.FromDouble(ox + pointX * Math.Cos(theta) - frontSideY * Math.Sin(theta));
                        var y = Fixed.FromDouble(oy + pointX * Math.Sin(theta) + frontSideY * Math.Cos(theta));
                        Assert.AreEqual(0, Geometry.PointOnSegSide(x, y, seg));
                    }
                    {
                        var x = Fixed.FromDouble(ox + pointX * Math.Cos(theta) - backSideY * Math.Sin(theta));
                        var y = Fixed.FromDouble(oy + pointX * Math.Sin(theta) + backSideY * Math.Cos(theta));
                        Assert.AreEqual(1, Geometry.PointOnSegSide(x, y, seg));
                    }
                }
            }
        }
Exemplo n.º 17
0
        public void AproxDistance()
        {
            var random = new Random(666);

            for (var i = 0; i < 1000; i++)
            {
                var dx = 666 * random.NextDouble() - 333;
                var dy = 666 * random.NextDouble() - 333;

                var adx      = Math.Abs(dx);
                var ady      = Math.Abs(dy);
                var expected = adx + ady - Math.Min(adx, ady) / 2;

                var actual = Geometry.AproxDistance(Fixed.FromDouble(dx), Fixed.FromDouble(dy));

                Assert.AreEqual(expected, actual.ToDouble(), 1.0E-3);
            }
        }
Exemplo n.º 18
0
        public void ToInt2()
        {
            var random = new Random(666);

            for (var i = 0; i < 100; i++)
            {
                var a       = (double)random.Next(666666) / 1000;
                var floor   = Math.Floor(a);
                var ceiling = Math.Ceiling(a);

                var fa       = Fixed.FromDouble(a);
                var ffloor   = fa.ToIntFloor();
                var fceiling = fa.ToIntCeiling();

                Assert.AreEqual(floor, ffloor, 1.0E-9);
                Assert.AreEqual(ceiling, fceiling, 1.0E-9);
            }
        }
Exemplo n.º 19
0
        public void PointOnSide3()
        {
            var random = new Random(666);

            for (var i = 0; i < 1000; i++)
            {
                var startX = -1 - 666 * random.NextDouble();
                var endX   = +1 + 666 * random.NextDouble();

                var pointX     = 666 * random.NextDouble() - 333;
                var frontSideY = -1 - 666 * random.NextDouble();
                var backSideY  = -frontSideY;

                for (var j = 0; j < 100; j++)
                {
                    var theta = 2 * Math.PI * random.NextDouble();
                    var ox    = 666 * random.NextDouble() - 333;
                    var oy    = 666 * random.NextDouble() - 333;

                    var node = new Node(
                        Fixed.FromDouble(ox + startX * Math.Cos(theta)),
                        Fixed.FromDouble(oy + startX * Math.Sin(theta)),
                        Fixed.FromDouble((endX - startX) * Math.Cos(theta)),
                        Fixed.FromDouble((endX - startX) * Math.Sin(theta)),
                        Fixed.Zero, Fixed.Zero, Fixed.Zero, Fixed.Zero,
                        Fixed.Zero, Fixed.Zero, Fixed.Zero, Fixed.Zero,
                        0, 0);

                    {
                        var x = Fixed.FromDouble(ox + pointX * Math.Cos(theta) - frontSideY * Math.Sin(theta));
                        var y = Fixed.FromDouble(oy + pointX * Math.Sin(theta) + frontSideY * Math.Cos(theta));
                        Assert.AreEqual(0, Geometry.PointOnSide(x, y, node));
                    }
                    {
                        var x = Fixed.FromDouble(ox + pointX * Math.Cos(theta) - backSideY * Math.Sin(theta));
                        var y = Fixed.FromDouble(oy + pointX * Math.Sin(theta) + backSideY * Math.Cos(theta));
                        Assert.AreEqual(1, Geometry.PointOnSide(x, y, node));
                    }
                }
            }
        }
Exemplo n.º 20
0
        public void PointToAngle()
        {
            var random = new Random(666);

            for (var i = 0; i < 100; i++)
            {
                var expected = 2 * Math.PI * random.NextDouble();
                for (var j = 0; j < 100; j++)
                {
                    var r      = 666 * random.NextDouble();
                    var ox     = 666 * random.NextDouble() - 333;
                    var oy     = 666 * random.NextDouble() - 333;
                    var x      = ox + r * Math.Cos(expected);
                    var y      = oy + r * Math.Sin(expected);
                    var fromX  = Fixed.FromDouble(ox);
                    var fromY  = Fixed.FromDouble(oy);
                    var toX    = Fixed.FromDouble(x);
                    var toY    = Fixed.FromDouble(y);
                    var angle  = Geometry.PointToAngle(fromX, fromY, toX, toY);
                    var actual = angle.ToRadian();
                    Assert.AreEqual(expected, actual, 0.01);
                }
            }
        }
Exemplo n.º 21
0
        public void PointToDist()
        {
            var random = new Random(666);

            for (var i = 0; i < 1000; i += 3)
            {
                var expected = i;
                for (var j = 0; j < 100; j++)
                {
                    var r     = i;
                    var theta = 2 * Math.PI * random.NextDouble();
                    var ox    = 666 * random.NextDouble() - 333;
                    var oy    = 666 * random.NextDouble() - 333;
                    var x     = ox + r * Math.Cos(theta);
                    var y     = oy + r * Math.Sin(theta);
                    var fromX = Fixed.FromDouble(ox);
                    var fromY = Fixed.FromDouble(oy);
                    var toX   = Fixed.FromDouble(x);
                    var toY   = Fixed.FromDouble(y);
                    var dist  = Geometry.PointToDist(fromX, fromY, toX, toY);
                    Assert.AreEqual(expected, dist.ToDouble(), (double)i / 100);
                }
            }
        }
Exemplo n.º 22
0
        public void BoxOnLineSide3()
        {
            var random = new Random(666);

            for (var i = 0; i < 1000; i++)
            {
                var radius = 33 + 33 * random.NextDouble();

                var startX = -1 - 666 * random.NextDouble();
                var endX   = +1 + 666 * random.NextDouble();

                var pointX     = 666 * random.NextDouble() - 333;
                var frontSideY = -1 - 1.5 * radius - 666 * random.NextDouble();
                var backSideY  = -frontSideY;
                var crossingY  = radius * 1.9 * (random.NextDouble() - 0.5);

                for (var j = 0; j < 100; j++)
                {
                    var theta = 2 * Math.PI * random.NextDouble();
                    var ox    = 666 * random.NextDouble() - 333;
                    var oy    = 666 * random.NextDouble() - 333;

                    var frontBox = new Fixed[]
                    {
                        Fixed.FromDouble(oy + pointX * Math.Sin(theta) + frontSideY * Math.Cos(theta) + radius),
                        Fixed.FromDouble(oy + pointX * Math.Sin(theta) + frontSideY * Math.Cos(theta) - radius),
                        Fixed.FromDouble(ox + pointX * Math.Cos(theta) - frontSideY * Math.Sin(theta) - radius),
                        Fixed.FromDouble(ox + pointX * Math.Cos(theta) - frontSideY * Math.Sin(theta) + radius)
                    };

                    var backBox = new Fixed[]
                    {
                        Fixed.FromDouble(oy + pointX * Math.Sin(theta) + backSideY * Math.Cos(theta) + radius),
                        Fixed.FromDouble(oy + pointX * Math.Sin(theta) + backSideY * Math.Cos(theta) - radius),
                        Fixed.FromDouble(ox + pointX * Math.Cos(theta) - backSideY * Math.Sin(theta) - radius),
                        Fixed.FromDouble(ox + pointX * Math.Cos(theta) - backSideY * Math.Sin(theta) + radius)
                    };

                    var crossingBox = new Fixed[]
                    {
                        Fixed.FromDouble(oy + pointX * Math.Sin(theta) + crossingY * Math.Cos(theta) + radius),
                        Fixed.FromDouble(oy + pointX * Math.Sin(theta) + crossingY * Math.Cos(theta) - radius),
                        Fixed.FromDouble(ox + pointX * Math.Cos(theta) - crossingY * Math.Sin(theta) - radius),
                        Fixed.FromDouble(ox + pointX * Math.Cos(theta) - crossingY * Math.Sin(theta) + radius)
                    };

                    var vertex1 = new Vertex(
                        Fixed.FromDouble(ox + startX * Math.Cos(theta)),
                        Fixed.FromDouble(oy + startX * Math.Sin(theta)));

                    var vertex2 = new Vertex(
                        vertex1.X + Fixed.FromDouble((endX - startX) * Math.Cos(theta)),
                        vertex1.Y + Fixed.FromDouble((endX - startX) * Math.Sin(theta)));

                    var line = new LineDef(
                        vertex1,
                        vertex2,
                        0, 0, 0, null, null);

                    Assert.AreEqual(0, Geometry.BoxOnLineSide(frontBox, line));
                    Assert.AreEqual(1, Geometry.BoxOnLineSide(backBox, line));
                    Assert.AreEqual(-1, Geometry.BoxOnLineSide(crossingBox, line));
                }
            }
        }