Ejemplo n.º 1
0
        public static bool IntersectCircle(Vector2 position, Vector2 center, float radius,
                                           float minAngle = 0, float maxAngle = 0, bool smallSector = true)
        {
            NormalizeMinMaxAngle(ref minAngle, ref maxAngle, smallSector);
            var fullCircle       = minAngle == 0 && maxAngle == 0;
            var distanceToCenter = Vector2.Distance(position, center);

            if (distanceToCenter <= radius)
            {
                if (!fullCircle)
                {
                    var angle = MathExtension.NormalizeAngle(GetAngle(position - center));

                    if (angle >= minAngle && angle <= maxAngle ||
                        angle + MathHelper.TwoPI >= minAngle && angle + MathHelper.TwoPI <= maxAngle)
                    {
                        return(true);
                    }
                }
                else
                {
                    return(true);
                }
            }

            return(false);
        }
Ejemplo n.º 2
0
        private static void NormalizeMinMaxAngle(ref float minAngle, ref float maxAngle, bool smallSector)
        {
            minAngle = MathExtension.NormalizeAngle(minAngle);
            maxAngle = MathExtension.NormalizeAngle(maxAngle);

            if (minAngle > maxAngle)
            {
                var swap = minAngle;
                minAngle = maxAngle;
                maxAngle = swap;
            }

            if (maxAngle - minAngle > MathHelper.PI && smallSector)
            {
                var oldMinAngle = minAngle;
                minAngle = maxAngle;
                maxAngle = oldMinAngle + MathHelper.TwoPI;
            }
        }
Ejemplo n.º 3
0
        public static bool IntersectCircle(Area area, Vector2 center, float radius,
                                           float minAngle = 0, float maxAngle = 0, bool smallSector = true)
        {
            NormalizeMinMaxAngle(ref minAngle, ref maxAngle, smallSector);
            var fullCircle = minAngle == 0 && maxAngle == 0;
            var lines      = new List <Vector2[]>()
            {
                new Vector2[] { area.BottomRight, area.BottomLeft },
                new Vector2[] { area.BottomLeft, area.TopLeft },
                new Vector2[] { area.TopLeft, area.TopRight },
                new Vector2[] { area.TopRight, area.BottomRight },
            };

            foreach (var line in lines)
            {
                var distanceToCenter = FindDistanceToSegment(center, line[0], line[1]);

                if (distanceToCenter <= radius)
                {
                    if (!fullCircle)
                    {
                        var corner = line[0];
                        var angle  = MathExtension.NormalizeAngle(GetAngle(corner - center));

                        if (angle >= minAngle && angle <= maxAngle ||
                            angle + MathHelper.TwoPI >= minAngle && angle + MathHelper.TwoPI <= maxAngle)
                        {
                            return(true);
                        }
                    }
                    else
                    {
                        return(true);
                    }
                }
            }

            return(false);
        }