Beispiel #1
0
 /// <summary>
 /// Returns true iff the point is contained in the geometric primitive.
 /// </summary>
 public static bool Intersect(Vector2 point, IGeomPrimitive prim)
 {
     if (prim is Circle) return Intersect(point, (Circle)prim);
     if (prim is Rectangle) return Intersect(point, (Rectangle)prim);
     if (prim is Triangle) return Intersect(point, (Triangle)prim);
     if (prim is Polygon) return Intersect(point, (Polygon)prim);
     throw new NotImplementedException("Geometry.Intersect not implemented for Vector2 and " + prim.GetType().Name);
 }
Beispiel #2
0
        /// <summary>
        /// Returns a random location inside an area.
        /// </summary>
        /// <param name="prim">The area.</param>
        /// <returns>A random location inside the area.</returns>
        public static Vector2 GetRandomLocation(IGeomPrimitive prim)
        {
            var type = prim.GetType();
            if (type == typeof(Circle)) return GetRandomLocation((Circle)prim);
            if (type == typeof(Rectangle))
            {
                var rectangle = (Rectangle)prim;
                return RandomHelper.GetRandomVector2(rectangle.Min, rectangle.Max);
            }

            // For any other geometric primitive, randomise points in its bounding box
            // until we hit inside the primitive. This is generic but can be increasingly
            // inefficient with certain primitives.
            var boundingBox = prim.BoundingBox;
            for (int i = 0; i < 1000000; ++i)
            {
                var pos = RandomHelper.GetRandomVector2(boundingBox.Min, boundingBox.Max);
                if (Intersect(pos, prim)) return pos;
            }
            throw new NotImplementedException("GetRandomLocation not properly implemented for " + type.Name);
        }