Matrix (?) to Rectangle and vise versa

63 views Asked by At

I'm currently working with files that define rectangular shapes in a way that I'm unfamiliar with. Someone told me it might be a matrix, but knowing that doesn't particularly help me with my problem, converting it to points and back.

For example, I have these values:

0.95, -0.28, -0.28, -0.95, 250.0234, 172.1973, -589.0131, 604.8696

These 8 floats make up a rect with the following coordinates, the center being 0:

{X=-778,Y=838}
{X=-303,Y=698}
{X=-399,Y=372}
{X=-874,Y=512}

enter image description here

To get these points I have used the following function, that someone else wrote for use with these files:

static Point[] GetPoints(double d01, double d02, double d03, double d04, double l01, double l02, double p01, double p02)
{
    var points = new Point[4];

    double a00 = d01 * l01;
    double a01 = d02 * l01;
    double a02 = d03 * l02;
    double a03 = d04 * l02;

    double sx1 = p01 - a00 - a02; if (sx1 < p01) sx1 = Math.Ceiling(sx1);
    double sy1 = p02 - a01 - a03; if (sy1 < p02) sy1 = Math.Ceiling(sy1);
    double sx2 = p01 + a00 - a02; if (sx2 < p01) sx2 = Math.Ceiling(sx2);
    double sy2 = p02 + a01 - a03; if (sy2 < p02) sy2 = Math.Ceiling(sy2);
    double sx3 = p01 + a00 + a02; if (sx3 < p01) sx3 = Math.Ceiling(sx3);
    double sy3 = p02 + a01 + a03; if (sy3 < p02) sy3 = Math.Ceiling(sy3);
    double sx4 = p01 - a00 + a02; if (sx4 < p01) sx4 = Math.Ceiling(sx4);
    double sy4 = p02 - a01 + a03; if (sy4 < p02) sy4 = Math.Ceiling(sy4);

    if (a02 * a01 > a03 * a00)
    {
        points[0] = new Point((int)sx1, (int)sy1);
        points[1] = new Point((int)sx2, (int)sy2);
        points[2] = new Point((int)sx3, (int)sy3);
        points[3] = new Point((int)sx4, (int)sy4);
    }
    else
    {
        points[0] = new Point((int)sx1, (int)sy1);
        points[3] = new Point((int)sx2, (int)sy2);
        points[2] = new Point((int)sx3, (int)sy3);
        points[1] = new Point((int)sx4, (int)sy4);
    }

    return points;
}

What I'm looking for now is possibly an explanation what these numbers even are, is this something common that I can read up on or is it custom, and a way to convert the points back to the 8 float values.

Can someone help me with this? Without knowing what this even is, it's really hard to find anything :/

1

There are 1 answers

2
MBo On BEST ANSWER

p01 and p02 are center coordinates (CenterX and CenterY)
d01-d04 represent cosine and sine of rotation angle (or dx,dy components of unit-length direction vector)
l01 and l02 are half-width and half-height of initial axis-aligned rectangle.
sxi and syi are X and Y-coordinates of ith vertice.
These coordinates are rounded toward the center coordinate.
Finally the vertices are numerated in certain order - either clockwise or counterclockwise (I did not checked)

To reconstruct initial parameters from vertice set:
You can determine center point as middle of two opposite vertices

p01 = (points[0].X + points[2].X) / 2
p02 = (points[0].Y + points[2].Y) / 2

and rotation angle

Angle = atan2(points[1].Y - points[0].Y, points[1].X - points[0].X)
Then correct Angle by +-Pi/2 or +-Pi to the smallest magnitude value (for example, 3/4*Pi=> Pi/4) Note that initial parameter set angle may differ by +-Pi/2
Find d01 = Cos(Angle) etc

Note that angle may differ by +-Pi/2 or +-Pi from initial parameter set.

l01 = Abs((points[1].X - points[0].X) * 0.5 / Cos(Angle))
l02 = Abs((points[2].Y - points[1].Y) * 0.5 / Cos(Angle))

Note that l01, l02 may be interchanged due to ambiguity of angle value.