コード例 #1
0
        public IntersectionResult GetSegmentSimplifiedRectangleIntersection_TestCase(bool filled, params double[] nums)
        {
            var seg  = new Segment(nums[0], nums[1], nums[2], nums[3]);
            var rect = new SimplifiedRectangle(nums[4], nums[5], nums[6], nums[7]);

            return(GeometryMethods.GetSegmentSimplifiedRectangleIntersection(seg, rect, out var result, filled));
        }
コード例 #2
0
        public bool IsVectorCollinear_TestCase(double x1, double y1, double x2, double y2)
        {
            var v1 = new Vector2(x1, y1);
            var v2 = new Vector2(x2, y2);

            return(GeometryMethods.IsVectorsCollinear(v1, v2));
        }
コード例 #3
0
        public IntersectionResult GetRayCircleIntersection(double x1, double y1, double x2, double y2, double xc, double yc, double r)
        {
            var ray    = new Ray(new Vector2(x1, y1), new Vector2(x2, y2));
            var circle = new Circle(xc, yc, r);

            return(GeometryMethods.GetRayCircleIntersection(ray, circle, out var _));
        }
コード例 #4
0
        private void UpdateCombatUnit(GameState state, ICombatUnit combat)
        {
            var unit = (Unit)combat;

            if (!unit.EnemyId.HasValue)
            {
                return;
            }

            var enemyId = unit.EnemyId.Value;
            var enemy   = state.GetUnit(enemyId);

            if (GeometryMethods.GetDistance(unit.Position, enemy.Position) > combat.FireRadius)
            {
                return;
            }

            unit.State = UnitState.Attacking;
            unit.MovingTargets.Clear();
            enemy.Heal -= combat.Damage;

            if (enemy.Heal <= 0)
            {
                //GameState.DeleteUnit(enemy.Id); - collection modified exception
                unit.EnemyId = null;
                unit.State   = UnitState.Idle;
            }
        }
コード例 #5
0
        private static bool IsSegmentIntersectingRectangle(Segment seg, SimplifiedRectangle rect)
        {
            var intersection =
                GeometryMethods.GetSegmentSimplifiedRectangleIntersection(seg, rect, out var intres, true);

            if (intersection == IntersectionResult.None)
            {
                return(false);
            }

            if (intersection == IntersectionResult.Point)
            {
                if (intres.Equals(seg.Begin) || intres.Equals(seg.End))
                {
                    return(false);
                }
            }

            if (intersection == IntersectionResult.Points)
            {
                var points = (Vector2[])intres;
                if (seg.Begin.Equals(points[0]) && seg.End.Equals(points[1]) ||
                    seg.Begin.Equals(points[1]) && seg.End.Equals(points[0]))
                {
                    return(false);
                }
            }

            return(true);
        }
コード例 #6
0
        public IntersectionResult GetSegmentsIntersection_TestCase(double x1, double y1, double x2, double y2,
                                                                   double x3, double y3, double x4, double y4)
        {
            var seg1 = new Segment(x1, y1, x2, y2);
            var seg2 = new Segment(x3, y3, x4, y4);

            return(GeometryMethods.GetSegmentsIntersection(seg1, seg2, out var result));
        }
コード例 #7
0
        public bool IsSimplifiedRectanglesInterescting_TestCase(double x1, double y1, double x2, double y2,
                                                                double x3, double y3, double x4, double y4)
        {
            var rect1 = new SimplifiedRectangle(x1, y1, x2, y2);
            var rect2 = new SimplifiedRectangle(x3, y3, x4, y4);

            return(GeometryMethods.IsSimplifiedRectanglesInterescting(rect1, rect2));
        }
コード例 #8
0
        public IntersectionResult GetLinesIntersection_Type_TestCase(double x1, double y1, double x2, double y2,
                                                                     double x3, double y3, double x4, double y4)
        {
            var line1 = new Line(x1, y1, x2, y2);
            var line2 = new Line(x3, y3, x4, y4);

            return(GeometryMethods.GetLinesIntersection(line1, line2, out var result));
        }
コード例 #9
0
        /// <summary>
        /// Юзаем бфс :)
        /// </summary>
        public static Vector2[] FindPathAmongRectangles(Vector2 start, Vector2 finish, params SimplifiedRectangle[] rectangles)
        {
            //var begInRect = GeometryMethods.IsPointInsideSimplifiedRectangle(start, rect);
            //var endInRect = GeometryMethods.IsPointInsideSimplifiedRectangle(finish, rect);
            //if (begInRect && !endInRect || !begInRect && endInRect)
            //    return new Vector2[0];
            if (rectangles.Length == 0)
            {
                return new Vector2[] { finish }
            }
            ;

            var searchRect = new SimplifiedRectangle(start, finish);

            rectangles = rectangles
                         .Where(e => GeometryMethods.IsSimplifiedRectanglesInterescting(searchRect, e))
                         .ToArray();

            var queue = new Queue <Vector2>();

            queue.Enqueue(start);
            var visited = new HashSet <Vector2> {
                start
            };
            var parent = new Dictionary <Vector2, Vector2?>();

            while (queue.Count > 0)
            {
                var current = queue.Dequeue();

                var seg = new Segment(current, finish);

                if (!rectangles.Any(r => IsSegmentIntersectingRectangle(seg, r)))
                {
                    parent[finish] = current;
                    break;
                }

                foreach (var n in GetNeighbors(current, rectangles, visited))
                {
                    parent[n] = current;
                    visited.Add(n);
                    queue.Enqueue(n);
                    // проверка на visited в GetNeighbors - для ускорения
                }
            }

            var result = new LinkedList <Vector2>();
            var a      = finish;

            while (parent.ContainsKey(a))
            {
                result.AddFirst(a);
                a = parent[a].Value;
            }

            return(result.ToArray());
        }
コード例 #10
0
        public void GetLineCircleIntersection_OnePoint()
        {
            var line   = new Line(0, 1, 1, 1);
            var circle = new Circle(0, 0, 1);

            var inter = GeometryMethods.GetLineCircleIntersection(line, circle, out var result);

            Assert.AreEqual(new Vector2(0, 1), (Vector2)result);
        }
コード例 #11
0
        public void GetLineCircleIntersection_NoPoints()
        {
            var line   = new Line(0, 2, 1, 2);
            var circle = new Circle(0, 0, 1);

            var inter = GeometryMethods.GetLineCircleIntersection(line, circle, out var result);

            Assert.AreEqual(IntersectionResult.None, inter);
        }
コード例 #12
0
        public void GetAngleBetweenVectorCww_TestCase(double x1, double y1, double x2, double y2, double exp)
        {
            var v1 = new Vector2(x1, y1);
            var v2 = new Vector2(x2, y2);

            var result = GeometryMethods.GetAngleBetweenVectorCww(v1, v2);

            Assert.AreEqual(exp, result, 0.01);
        }
コード例 #13
0
        public void GetLinesIntersection_Value_TestCase(double x1, double y1, double x2, double y2,
                                                        double x3, double y3, double x4, double y4, double expx, double expy)
        {
            var line1 = new Line(x1, y1, x2, y2);
            var line2 = new Line(x3, y3, x4, y4);

            GeometryMethods.GetLinesIntersection(line1, line2, out var result);

            Assert.AreEqual(new Vector2(expx, expy), (Vector2)result);
        }
コード例 #14
0
        public void GetNearestPointOfSegment_TestCase(double xp, double yp, double xs1, double ys1,
                                                      double xs2, double ys2, double expx, double expy)
        {
            var point    = new Vector2(xp, yp);
            var seg      = new Segment(xs1, ys1, xs2, ys2);
            var expPoint = new Vector2(expx, expy);

            var intPoint = GeometryMethods.GetNearestPointOfSegment(point, seg);

            Assert.True(intPoint.Equals(expPoint));
        }
コード例 #15
0
        public void RotationForMoving_Simple()
        {
            var unit  = new SimpleMovableUnit(0);
            var start = new Vector2(0, 0);
            var end   = new Vector2(10, 5);

            State.AddUnit(unit, start);
            var expected = Math.Acos(10 / GeometryMethods.GetDistance(start, end));

            Engine.SetUnitMovingTo(State, unit, end);
            FullUpdate();

            Assert.AreEqual(expected, unit.Rotation, 0.01);
        }
コード例 #16
0
    private void Update()
    {
        UnitController unitController = (UnitController)controller;

        if (unitController.state == UnitState.LANING && currentWaypointId < waypoints.Length)
        {
            bool withinDetectionRange = GeometryMethods.IsWithinRange(waypoints[currentWaypointId].transform, transform, 2);
            if (withinDetectionRange)
            {
                currentWaypointId++;
                GoToWaypoint();
            }
        }
    }
コード例 #17
0
        public void OneBarrier()
        {
            var unit     = new SimpleMovableUnit(0);
            var building = new SimpleBuildingUnit(1);
            var start    = new Vector2(0, 0);

            State.AddUnit(unit, start);
            State.AddUnit(building, new Vector2(25, 25));
            var finish = new Vector2(100, 100);

            Engine.SetUnitMovingTo(State, unit, finish);
            var количествоЦиклов = (int)(GeometryMethods.GetDistance(start, finish) / unit.Speed) + 1;

            FullUpdate(количествоЦиклов);

            Assert.AreNotEqual(finish, State.GetUnit(0).Position);
        }
コード例 #18
0
        private void UpdateUnitRotation(Unit unit, double rotationSpeed)
        {
            if (unit.RotationTarget.HasValue)
            {
                var diff = GeometryMethods.GetAnglesDifference(unit.Rotation, unit.RotationTarget.Value);

                if (Math.Abs(diff).LessOrEqual(rotationSpeed))
                {
                    unit.Rotation       = unit.RotationTarget.Value;
                    unit.RotationTarget = null;
                }
                else
                {
                    unit.Rotation += rotationSpeed * Math.Sign(diff);
                }
            }
        }
コード例 #19
0
        private void UpdateUnitPosition(Unit unit, double speed)
        {
            if (unit.MovingTargets.Count == 0)
            {
                unit.State = UnitState.Idle;
                return;
            }

            var target = unit.MovingTargets.Peek();

            // угол между осью OX и вектором движения
            var angle = GeometryMethods.GetAngleBetweenVectorCww(new Vector2(1, 0), target - unit.Position);

            if (!unit.RotationTarget.HasValue && !unit.Rotation.Equal(angle))
            {
                SetUnitRotation(unit, angle);
            }

            if (unit.RotationTarget.HasValue)
            {
                // юнит поворачивается, не надо ему мешать
                return;
            }

            unit.State = UnitState.Moving;

            var dist = GeometryMethods.GetDistance(unit.Position, target);

            if (dist <= speed)
            {
                unit.Position = target;
                unit.MovingTargets.Dequeue();
            }
            else
            {
                var vector = (target - unit.Position).GetNormalized();
                unit.Position += vector * speed;
            }
        }
コード例 #20
0
        /// <summary>
        /// Атака одним юнитом другого.
        /// </summary>
        public void SetUnitAttacking(GameState state, Unit unit, Unit target)
        {
            if (!(unit is ICombatUnit combat))
            {
                throw new ArgumentException($"Unit with {unit.Id} is not ICombatUnit");
            }

            if (GeometryMethods.GetDistance(unit.Position, target.Position) > combat.FireRadius)
            {
                if (!(unit is IMovableUnit))
                {
                    return;
                }

                SetUnitMovingTo(state, unit, target.Position);
            }
            else
            {
                unit.MovingTargets.Clear();
            }

            unit.EnemyId = target.Id;
        }
コード例 #21
0
ファイル: MethodsDemo.cs プロジェクト: skirov/TA-CSharp
    /// <summary>
    /// The entry point of the program.
    /// </summary>
    private static void Main()
    {
        Student ivan = new Student();

        ivan.FirstName   = "Ivan";
        ivan.LastName    = "Ivanov";
        ivan.DateOfBirth = new DateTime(1992, 03, 17);

        Student ivanka = new Student();

        ivanka.FirstName   = "Ivanka";
        ivanka.LastName    = "Ivanova";
        ivanka.DateOfBirth = new DateTime(1993, 11, 3);

        Student asd = new Student();

        Console.WriteLine(
            "Is {0} older than {1}? -> {2}",
            ivan.FirstName,
            ivanka.FirstName,
            ivan.DateOfBirth.IsEarlierThan(ivanka.DateOfBirth));

        Console.WriteLine(GeometryMethods.CalcTriangleArea(3, 4, 5));

        Console.WriteLine(LanguageMethods.DigitToText(5));

        Console.WriteLine(StatisticalMethods.Max(5, -1, 3, 2, 14, 2, 3));

        ConsolePrinter.PrintNumber(1.3, 2);
        ConsolePrinter.PrintPercent(0.75, 0);
        ConsolePrinter.PrintAligned(2.30, 8);

        Console.WriteLine(GeometryMethods.CalcDistance(3, -1, 3, 2.5));
        Console.WriteLine("Horizontal? -> " + GeometryMethods.IsLineHorizontal(3, -1, 3, 2.5));
        Console.WriteLine("Vertical? -> " + GeometryMethods.IsLineVertical(3, -1, 3, 2.5));
    }
コード例 #22
0
        public void GetNormalizedAngle_TestCase(double angle, double expected)
        {
            var result = GeometryMethods.GetNormalizedAngle(angle);

            Assert.AreEqual(expected, result, 0.01);
        }
コード例 #23
0
 public void UncorrectTriangle()
 {
     Assert.IsFalse(GeometryMethods.IsTriangleExist(100, 2, 4));
 }
コード例 #24
0
 public void NegativeSidesTriangle()
 {
     Assert.IsFalse(GeometryMethods.IsTriangleExist(-3, -4, -5));
 }
コード例 #25
0
 public bool TrianglesTest(int firstSide, int secondeSide, int thridSide)
 {
     return(GeometryMethods.IsTriangleExist(firstSide, secondeSide, thridSide));
 }
コード例 #26
0
 public void CorrectTriangle()
 {
     Assert.IsTrue(GeometryMethods.IsTriangleExist(3, 4, 5));
 }
コード例 #27
0
        public void GetAnglesDifference_TestCase(double a1, double a2, double expected)
        {
            var result = GeometryMethods.GetAnglesDifference(a1, a2);

            Assert.AreEqual(expected, result, 0.01);
        }