void drawLineAngle(Mat& imgDraw, Point pointCenter, float fAngle, const Scalar& color, int thickness = 1, int lineType = LINE_8)
{
float fDec = fAngle - (int)fAngle; //将任意角度转换到(-180°到180°]之间
fAngle = (int)fAngle % 360 + fDec;
if (fAngle > 180.0)
fAngle = fAngle - 360;
if (fAngle <= -180.0)
fAngle = fAngle + 360;
int nWidth = imgDraw.cols;
int nHight = imgDraw.rows;
Point pointEnd(0, 0);
if (fAngle == 0)
{
pointEnd = Point(pointCenter.x, 0);
}
else if (fAngle == 180)
{
pointEnd = Point(pointCenter.x, nHight - 1);
}
else if (fAngle > 0 && fAngle <= 90)//y-y0=k(x-x0)
{
float k = -tan((90.0 - fAngle) * CV_PI / 180);
float x0 = nWidth - 1;
float y0 = k * (x0 - pointCenter.x) + pointCenter.y;
float y1 = 0;
float x1 = (y1 - pointCenter.y) / k + pointCenter.x;
if (y0 < 0)
pointEnd = Point(x0, y0);
else
pointEnd = Point(x1, y1);
}
else if (fAngle > 90.0 && fAngle < 180)
{
float k = tan((fAngle - 90.0) * CV_PI / 180);
float x0 = nWidth - 1;
float y0 = k * (x0 - pointCenter.x) + pointCenter.y;
float y1 = nHight - 1;
float x1 = (y1 - pointCenter.y) / k + pointCenter.x;
if (y0 < nHight - 1)
pointEnd = Point(x0, y0);
else
pointEnd = Point(x1, y1);
}
else if (fAngle >= -90 && fAngle < 0)//y-y0=k(x-x0)
{
float k = tan((90.0 + fAngle) * CV_PI / 180);
float x0 = 0;
float y0 = k * (x0 - pointCenter.x) + pointCenter.y;
float y1 = 0;
float x1 = (y1 - pointCenter.y) / k + pointCenter.x;
if (y0 < nHight - 1)
pointEnd = Point(x0, y0);
else
pointEnd = Point(x1, y1);
}
else if (fAngle > -180 && fAngle < -90)
{
float k = tan((fAngle + 90.0) * CV_PI / 180);
float x0 = 0;
float y0 = k * (x0 - pointCenter.x) + pointCenter.y;
float y1 = nHight - 1;
float x1 = (y1 - pointCenter.y) / k + pointCenter.x;
if (y0 < nHight - 1)
pointEnd = Point(x0, y0);
else
pointEnd = Point(x1, y1);
}
line(imgDraw, pointCenter, pointEnd, color, thickness, lineType);
}
06-23
5万+
11-18
4929
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交