Пример #1
0
        /// <summary>
        /// Projects a point onto a circle</summary>
        /// <param name="p">Point to project</param>
        /// <param name="c">Circle to project onto</param>
        /// <param name="projection">Projected point</param>
        /// <returns>True iff projection is well defined</returns>
        public static bool Project(Vec2F p, CircleF c, ref Vec2F projection)
        {
            Vec2F d           = Vec2F.Sub(p, c.Center);
            float length      = d.Length;
            bool  wellDefined = false;

            if (length > 0.000001f * c.Radius)
            {
                wellDefined = true;
                float scale = c.Radius / length;
                projection = Vec2F.Add(c.Center, Vec2F.Mul(d, scale));
            }
            return(wellDefined);
        }
Пример #2
0
        /// <summary>
        /// Determines if 2 rays intersect, and if so, returns the ray parameters of the intersection point</summary>
        /// <param name="r1">Ray 1</param>
        /// <param name="r2">Ray 2</param>
        /// <param name="t1">Returned ray parameter 1</param>
        /// <param name="t2">Returned ray parameter 2</param>
        /// <returns>True iff the rays intersect (are not parallel)</returns>
        public static bool Intersect(Ray2F r1, Ray2F r2, ref double t1, ref double t2)
        {
            double denom = (r2.Direction.Y * r1.Direction.X - r2.Direction.X * r1.Direction.Y);

            if (Math.Abs(denom) < 0.000001f)
            {
                return(false);
            }

            Vec2F d = Vec2F.Sub(r1.Origin, r2.Origin);

            t1 = (r2.Direction.X * d.Y - r2.Direction.Y * d.X) / denom;
            t2 = (r1.Direction.X * d.Y - r1.Direction.Y * d.X) / denom;
            return(true);
        }
Пример #3
0
        /// <summary>
        /// Constructs the circle containing 3 points</summary>
        /// <param name="p1">Point 1</param>
        /// <param name="p2">Point 2</param>
        /// <param name="p3">Point 3</param>
        public CircleF(Vec2F p1, Vec2F p2, Vec2F p3)
        {
            Vec2F  o1 = Vec2F.Add(p1, p2); o1 *= 0.5f;
            Vec2F  o2 = Vec2F.Add(p3, p2); o2 *= 0.5f;
            Vec2F  d1 = Vec2F.Sub(p2, p1); d1 = d1.Perp;
            Vec2F  d2 = Vec2F.Sub(p3, p2); d2 = d2.Perp;
            double t1 = 0;
            double t2 = 0;

            if (Ray2F.Intersect(new Ray2F(o1, d1), new Ray2F(o2, d2), ref t1, ref t2))
            {
                Center = o1 + d1 * (float)t1;
                Radius = Vec2F.Distance(Center, p1);
            }
            else
            {
                Center = new Vec2F(float.PositiveInfinity, float.PositiveInfinity);
                Radius = float.PositiveInfinity;
            }
        }
Пример #4
0
        /// <summary>
        /// Determines if point is inside circle</summary>
        /// <param name="p">Point</param>
        /// <returns>True iff point is inside circle</returns>
        public bool Contains(Vec2F p)
        {
            Vec2F d = Vec2F.Sub(p, Center);

            return(d.LengthSquared < (Radius * Radius));
        }