c++ project with opencv mathematical problem with drawing circle tangential points.
I’m deploying a project with c++ opencv that i have to display tangential lines on a circle from specific point which is mentioned by opencv mouse callback function. so i get the location of mouse point on screen by a point variable inside callback function like :
Point2f location;
location.x = x;
location.y = y;
now i need a function that draw tangential lines with example radius = 4 by opencv !!
something like this :
circle tangential points
thank you.
>Solution :
in your case, first you have to declare a function that calculate tangential points on a sample circle and then use specific points in order to draw line matrixs with opencv.
so let’s try with this function :
vector <double> Coverage::tangentCircle (const Point2f &mouse, const Point2f ¢er, double radius) {
vector <double> points (4,0);
// Line between point mouse and center
double directLine = sqrt(pow((mouse.x - center.x), 2) + pow((mouse.y - center.y), 2));
// angle of point mouse and center
double directAngle = pointAngle(mouse, center);
// angle between radius and directLine
double theta = acos(radius / directLine);
// direction angle of point tangent 1 and center
double d1 = directAngle + theta;
// direction angle of point tangent 2 and center
double d2 = directAngle - theta;
points[0] = (int)(center.x + radius * cos(d1));
points[1] = (int)(center.y + radius * sin(d1));
points[2] = (int)(center.x + radius * cos(d2));
points[3] = (int)(center.y + radius * sin(d2));
return points;
}
data recursion of this function is vector that contains our tangential points.
there are 3 parameters in entry section which are mouse point(x,y) and circle(x,y) and specific radius that we need.
distance formula between mouse and center calculate with Euclidean way.
i comment every part of function to make it undestandable.
after return tangential points, you can use circle line function of opencv to draw results :
line(screen, mouse, Point(points[0], points[1]), Scalar(100, 100, 100), 2, 8, 0);
line(screen, mouse, Point(points[2], points[3]), Scalar(100, 100, 100), 2, 8, 0);
Hope to be useful