private static bool GetRectAndInvertMatrix(ref P3D_Matrix matrix, ref P3D_Rect rect, int canvasW, int canvasH)
    {
        // Grab transformed corners
        var a = matrix.MultiplyPoint(0.0f, 0.0f);
        var b = matrix.MultiplyPoint(1.0f, 0.0f);
        var c = matrix.MultiplyPoint(0.0f, 1.0f);
        var d = matrix.MultiplyPoint(1.0f, 1.0f);

        // Find min/max x/y
        var xMin = Mathf.Min(Mathf.Min(a.x, b.x), Mathf.Min(c.x, d.x));
        var xMax = Mathf.Max(Mathf.Max(a.x, b.x), Mathf.Max(c.x, d.x));
        var yMin = Mathf.Min(Mathf.Min(a.y, b.y), Mathf.Min(c.y, d.y));
        var yMax = Mathf.Max(Mathf.Max(a.y, b.y), Mathf.Max(c.y, d.y));

        // Has volume?
        if (xMin < xMax && yMin < yMax)
        {
            // Make sure rect doesn't go outside canvas
            rect.XMin = Mathf.Clamp(Mathf.FloorToInt(xMin), 0, canvasW);
            rect.XMax = Mathf.Clamp(Mathf.CeilToInt(xMax), 0, canvasW);
            rect.YMin = Mathf.Clamp(Mathf.FloorToInt(yMin), 0, canvasH);
            rect.YMax = Mathf.Clamp(Mathf.CeilToInt(yMax), 0, canvasH);

            matrix = matrix.Inverse;

            return(true);
        }

        return(false);
    }
    private static bool IsInsideShape(P3D_Matrix inverseMatrix, int x, int y, ref Vector2 shapeCoord)
    {
        shapeCoord = inverseMatrix.MultiplyPoint(x, y);

        if (shapeCoord.x >= 0.0f && shapeCoord.x < 1.0f)
        {
            if (shapeCoord.y >= 0.0f && shapeCoord.y < 1.0f)
            {
                return(true);
            }
        }

        return(false);
    }
Example #3
0
 private static bool IsInsideShape(P3D_Matrix inverseMatrix, int x, int y, ref Vector2 shapeCoord)
 {
     shapeCoord = inverseMatrix.MultiplyPoint((float)x, (float)y);
     return((shapeCoord.x >= 0f) && ((shapeCoord.x < 1f) && ((shapeCoord.y >= 0f) && (shapeCoord.y < 1f))));
 }