#include <opencv2/opencv.hpp>
#include <iostream>
int main() {
cv::Point2f center(100.0f, 100.0f); // 圆的中心坐标
float radius = 50.0f; // 圆的半径
cv::Vec4f line(1.0f, -1.0f, -50.0f, 0.0f); // 直线的一般方程 Ax + By + C = 0
cv::Point2f intersection1, intersection2;
// 使用OpenCV的函数计算圆和直线的交点
bool success = cv::fitLine(cv::Mat(line), line, cv::DIST_L2, 0, 0.01, 0.01);
if (success) {
cv::Point2f dir(line[0], line[1]);
cv::Point2f point(line[2], line[3]);
cv::Point2f p1 = point + 1000.0f * dir;
cv::Point2f p2 = point - 1000.0f * dir;
// 使用直线上的两个点创建线段
cv::Vec4f lineSegment(p1.x, p1.y, p2.x, p2.y);
// 使用cv::circle函数绘制圆
cv::Mat image(200, 200, CV_8UC3, cv::Scalar(255, 255, 255)); // 创建白色背景的图像
cv::circle(image, center, radius, cv::Scalar(0, 0, 0), 2); // 绘制圆
cv::line(image, cv::Point(lineSegment[0], lineSegment[1]), cv::Point(lineSegment[2], lineSegment[3]), cv::Scalar(0, 0, 0), 2); // 绘制直线
// 使用cv::circle函数绘制交点
bool intersects = cv::intersection(center, radius, lineSegment, intersection1, intersection2);
if (intersects) {
cv::circle(image, intersection1, 4, cv::Scalar(255, 0, 0), -1); // 绘制交点1为红色
cv::circle(image, intersection2, 4, cv::Scalar(0, 255, 0), -1); // 绘制交点2为绿色
}
// 显示图像
cv::imshow("Circle and Line Intersection", image);
cv::waitKey(0);
} else {
std::cout << "FitLine failed." << std::endl;
}
return 0;
}