Intersection of ellipse with line

spring·2020년 11월 9일
0
template<typename Real> std::array<Real, 6> EllipseAsQuadric(cv::RotatedRect ellipse) {
	//https://en.wikipedia.org/wiki/Conic_section
	Real xc = static_cast<Real>(ellipse.center.x);
	Real yc = static_cast<Real>(ellipse.center.y);
	Real a = static_cast<Real>(ellipse.size.width / 2);
	Real b = static_cast<Real>(ellipse.size.height / 2);
	Real r = static_cast<Real>(ellipse.angle * CV_PI / 180.0);
	std::array<Real, 6> equ;
	Real A, B, C, D, E, F;
	float sinr = sin(r);// *180 / CV_PI;
	float cosr = cos(r);// *180 / CV_PI;
	A=equ[0] = a*a*sinr*sinr + b*b*cosr*cosr;
	B=equ[1] = 2 * (b*b - a*a)*sinr*cosr;
	C=equ[2] = a*a*cosr*cosr + b*b*sinr*sinr;
	D = equ[3] = -2 * A*xc - B*yc;
	E = equ[4] = -B*xc - 2 * C*yc;
	F = equ[5]=A*xc*xc + B*xc*yc + C*yc*yc - a*a*b*b;
	return equ;
}
std::vector<cv::Point> IntersectConicAndLine(float A, float B, float C, float D, float E, float F, cv::Point pt1, cv::Point pt2) {
http://csharphelper.com/blog/2014/11/see-where-a-line-intersects-a-conic-section-in-c/
	// Get dx and dy;
	float x1 = pt1.x;
	float y1 = pt1.y;
	float x2 = pt2.x;
	float y2 = pt2.y;
	float dx = x2 - x1;
	float dy = y2 - y1;

	// Calculate the coefficients for the quadratic formula.
	float a = A * dx * dx + B * dx * dy + C * dy * dy;
	float b = A * 2 * x1 * dx + B * x1 * dy + B * y1 * dx +
		C * 2 * y1 * dy + D * dx + E * dy;
	float c = A * x1 * x1 + B * x1 * y1 + C * y1 * y1 +
		D * x1 + E * y1 + F;

	// Check the determinant to see how many solutions there are.
	std::vector<cv::Point> solutions;
	float det = b * b - 4 * a * c;
	if (det == 0) {
		float t = -b / (2 * a);
		solutions.push_back(cv::Point(x1 + t * dx, y1 + t * dy));
	} else if (det > 0) {
		float root = (float)std::sqrt(b * b - 4 * a * c);
		float t1 = (-b + root) / (2 * a);
		solutions.push_back(cv::Point(x1 + t1 * dx, y1 + t1 * dy));
		float t2 = (-b - root) / (2 * a);
		solutions.push_back(cv::Point(x1 + t2 * dx, y1 + t2 * dy));
	}
	return solutions;
}

https://www.desmos.com/calculator/42lngkxil6

profile
Researcher & Developer @ NAVER Corp | Designer @ HONGIK Univ.

0개의 댓글