I have four coordinates of the detected rectangle. I want to know which of them are upper left, upper right, lower left and lower right points. The method that I wrote myself (unnecessarily) for a long time and actually did not work (perhaps because I was mistaken somewhere). In any case, I'm sure there is an easier way, but I cannot find a method using Google. Therefore any help would be appreciated.
My method is to find angles with max y (most-top), min y (most-bottom), max x (rightmost), min x (leftmost). And then, if the most-left.y> the most-right.y, then the upper left point is the leftmost, the upper right is the highest, etc. Is this method correct? And is there an easier way?
My code, sorry for its messy and confusing ... squareX [0] is the first x coordinate. [0] is the first y coordinate, etc.
Edit: It turns out that my method is fine, it just max () does not do what I thought it did (return the maximum number of all entered numbers).
maxx = max(squareX[0], squareX[1], squareX[2], squareX[3]);
maxy = max(squareY[0], squareY[1], squareY[2], squareY[3]);
minx = min(squareX[0], squareX[1], squareX[2], squareX[3]);
miny = min(squareY[0], squareY[1], squareY[2], squareY[3]);
if (squareX[0] == maxx)
{
mx = 0;
}
if (squareX[1] == maxx)
{
mx = 1;
}
if (squareX[2] == maxx)
{
mx = 2;
}
if (squareX[3] == maxx)
{
mx =3;
}
if (squareX[0] == minx)
{
mix = 0;
}
if (squareX[1] == minx)
{
mix = 1;
}
if (squareX[2] == minx)
{
mix = 2;
}
if (squareX[3] == minx)
{
mix = 3;
}
if (squareY[0] == maxy)
{
my = 0;
}
if (squareY[1] == maxy)
{
my = 1;
}
if (squareY[2] == maxy)
{
my = 2;
}
if (squareY[3] == maxy)
{
my = 3;
}
if (squareY[0] == miny)
{
miy = 0;
}
if (squareY[1] == miny)
{
miy = 1;
}
if (squareY[2] == miny)
{
miy = 2;
}
if (squareY[3] == miny)
{
miy = 3;
}
if (squareY[mix] > squareY[mx])
{
Pt1.x = squareX[mix];
Pt1.y = squareY[mix];
Pt2.x = squareX[my];
Pt2.y = squareY[my];
Pt3.x = squareX[mx];
Pt3.y = squareY[mx];
Pt4.x = squareX[miy];
Pt4.y = squareY[miy];
}
else
{
Pt1.x = squareX[my];
Pt1.y = squareY[my];
Pt2.x = squareX[mx];
Pt2.y = squareY[mx];
Pt3.x = squareX[miy];
Pt3.y = squareY[miy];
Pt4.x = squareX[mix];
Pt4.y = squareY[mix];
}
thank
source
share