Revit.SDK.Samples.ObjectViewer.CS.Sketch3D.CalculateCanvasRegion C# (CSharp) Method

CalculateCanvasRegion() private method

get the display region, adjust the proportion and location
private CalculateCanvasRegion ( float previewWidth, float previewHeigh, RectangleF bbox ) : System.Drawing.PointF[]
previewWidth float
previewHeigh float
bbox System.Drawing.RectangleF
return System.Drawing.PointF[]
        private PointF[] CalculateCanvasRegion(float previewWidth, float previewHeigh, RectangleF bbox)
        {
            // get the area without margin
            float realWidth = previewWidth * (1 - 2 * MarginRatio);
            float realHeight = previewHeigh * (1 - 2 * MarginRatio);
            float minX = previewWidth * MarginRatio;
            float minY = previewHeigh * MarginRatio;
            // ratio of width to height
            float originRate = bbox.Width / bbox.Height;
            float displayRate = realWidth / realHeight;

            if (originRate > displayRate)
            {
                // display area in canvas need move to center in height
                float goalHeight = realWidth / originRate;
                minY = minY + (realHeight - goalHeight) / 2;
                realHeight = goalHeight;
            }
            else
            {
                // display area in canvas need move to center in width
                float goalWidth = realHeight * originRate;
                minX = minX + (realWidth - goalWidth) / 2;
                realWidth = goalWidth;
            }

            PointF[] plgpts = new PointF[3];
            plgpts[0] = new PointF(minX, realHeight + minY);                // upper-left point
            plgpts[1] = new PointF(realWidth + minX, realHeight + minY);    // upper-right point
            plgpts[2] = new PointF(minX, minY);                             // lower-left point

            return plgpts;
        }