Beispiel #1
0
        public static bool CloseTo(double a, double b, double tolerance = .00001)
        {
            var epsilon =
                Math.Max(Math.Max(Math.Abs(a), Math.Abs(b)) * tolerance,
                         tolerance); // .001% of the smaller value for the epsilon check as per MSDN reference suggestion

            return(Math.Abs(a - b) <= epsilon);
        }
Beispiel #2
0
        /// <summary>
        /// calculates the smallest AABB that will encompass the rotated box. The AABB is in local space.
        /// </summary>
        public Box2 CalcBoundingBox()
        {
            // https://stackoverflow.com/a/19830964

            var(X0, Y0) = Box.BottomLeft;
            var(X1, Y1) = Box.TopRight;

            var Fi = Rotation.Theta;

            var CX = (X0 + X1) / 2;  //Center point
            var CY = (Y0 + Y1) / 2;
            var WX = (X1 - X0) / 2;  //Half-width
            var WY = (Y1 - Y0) / 2;

            var SF = Math.Sin(Fi);
            var CF = Math.Cos(Fi);

            var NH = Math.Abs(WX * SF) + Math.Abs(WY * CF);                                           //boundrect half-height
            var NW = Math.Abs(WX * CF) + Math.Abs(WY * SF);                                           //boundrect half-width

            return(new Box2((float)(CX - NW), (float)(CY - NH), (float)(CX + NW), (float)(CY + NH))); //draw bound rectangle
        }