I'm trying to create a bounding box around a given dataset.
My Idea therefore was to use a PCA. I read that it won't always find optimal solutions but this doesn't matter.
What I've done so far is that I calculate the covariance-matrix and use it to calculate a SVD of this matrix.
Lets say we have a sample input like
[40, 20], [-40, -20],[40, -20],[-40, 20],[30, 30]
The covariance matrix will become
[1780.0, 180.0] [180.0, 580.0]
With the SVD I get the rotation matrix U:
[0.99, 0.15] [0.15, -0.99]
and the diagonal matrix D:
[1806.41, 0] [0, 553.58]
With my eigenvectors I'm able to calculate the slope of the lines representing the box.
I now need to get the center of the PCA in the original space not in the 0-centered space. And I also need to find out the length of those to vectors.
Does anyone has an idea how to get them?
I found a solution. The idea was to use the two eigenvectors to calculte the maximum distance of all point to it.
The maximum distance will than be half the length of the rectangles width and height. As shown in the picture below
To position the rectangle I calculate the 4 points by
p1.x = max1 * eigenvector1(0) + max2 * eigenvector1(1)
p1.y = max1 * eigenvector2(0) + max2 * eigenvector2(1)
for all points.
Than I just had to transform the vertices and all datapoints by meanX and meanY and the rectangle enclosing the original dataset.