¿øÀ» Áö³ª´Â Á÷¼±ÀÇ ±³Á¡À» ±¸ÇØ º¸ÀÚ Á÷¼±ÀÇ °ø½ÄÀº ÀÏÂ÷¹æÁ¤½ÄÀ¸·Î ´ÙÀ½°ú °°´Ù. ax + bx + c = 0 ( a, b, c´Â »ó¼ö a´Â 0ÀÌ ¾Æ´Ï´Ù) ¿øÀÇ °ø½ÄÀº ÀÌÂ÷¹æÁ¤½ÄÀ¸·Î ´ÙÀ½°ú °°´Ù. Á÷¼±ÀÇ ¹æÁ¤½ÄÀ» ¿øÀÇ ¹æÁ¤½Ä¿¡ ´ëÀÔÇÏ¿© ÆǺ°½ÄÀ» ±¸ÇÑ´Ù. ´ÙÀ½°ú °°ÀÌ ¿ø°ú Á÷¼±ÀÌ ¸¸³ª´Â ±³Á¡ t(¾Æ·¡¿¡¼ x)´Â ´ÙÀ½°ú °°ÀÌ ±¸ÇÑ´Ù. 1. ÆǺ°½Ä ÀÌÇØDÀÇ °ªÀ¸·Î ±³Á¡(ÇØ) °¹¼ö¸¦ ±¸ÇÒ¼ö ÀÖ´Ù. D < 0 À̸é ÇØ°¡ ¾ø´Ù D = 0 À̸é ÇϳªÀÇ Çظ¦ °¡Áø´Ù. D > 0 ÀÌ¸é µÎ°³ÀÇ Çظ¦ °¡Áø´Ù. 2. ¿ø°ú Á÷¼±ÀÇ °Å¸® ±¸ÇϱâÀ̶§, Á¡(x0, y0)°ú Á÷¼±ÀÇ °Å¸®´Â ´ÙÀ½°ú °°ÀÌ ±¸ÇÑ´Ù. 3. Àüü ¼Ò½º // mydelTest.cpp : ÄÜ¼Ö ÀÀ¿ë ÇÁ·Î±×·¥¿¡ ´ëÇÑ ÁøÀÔÁ¡À» Á¤ÀÇÇÕ´Ï´Ù.
// #include "stdafx.h" #include <float.h> #include <math.h> class Point2 { public: float x, y; Point2() : x(0.0f), y(0.0f) { } Point2(float x, float y) : x(x), y(y) { } const float& operator[] (int i) const { return ((&x)[i]); } float& operator[] (int i) { return ((&x)[i]); } Point2& operator =(const Point2& v) { x = v.x; y = v.y; return (*this); } Point2 operator +(const Point2& v) const { return Point2(x + v.x, y + v.y); } Point2 operator *(float num) const { return Point2(x * num, y * num); } }; // Find the points of intersection. output intersection1, intersection2 int FindLineCircleIntersections(float cx, float cy, float radius, Point2 point1, Point2 point2, Point2& intersection1, Point2& intersection2) { float dx, dy, A, B, C, det, t; dx = point2.x - point1.x; dy = point2.y - point1.y; A = dx * dx + dy * dy; B = 2 * (dx * (point1.x - cx) + dy * (point1.y - cy)); C = (point1.x - cx) * (point1.x - cx) + (point1.y - cy) * (point1.y - cy) - radius * radius; det = B * B - 4 * A * C; if ((A <= 0.0000001) || (det < 0)) { // No real solutions. intersection1 = Point2(-FLT_MAX, -FLT_MAX); intersection2 = Point2(-FLT_MAX, -FLT_MAX); return 0; } else if (det == 0) { // One solution. t = -B / (2 * A); intersection1 = Point2(point1.x + t * dx, point1.y + t * dy); intersection2 = Point2(-FLT_MAX, -FLT_MAX); return 1; } else { // Two solutions. t = (float)((-B + sqrt(det)) / (2 * A)); intersection1 = Point2(point1.x + t * dx, point1.y + t * dy); t = (float)((-B - sqrt(det)) / (2 * A)); intersection2 = Point2(point1.x + t * dx, point1.y + t * dy); return 2; } } int _tmain(int argc, _TCHAR* argv[]) { Point2 pt1 = Point2(6, -5); Point2 pt2 = Point2(3, -2); Point2 out1, out2; int count = FindLineCircleIntersections(3, -5, 3, pt1, pt2, out1, out2); //count1 = 2, out1 = 3, -2, out2 = 6, -5 //y = -x - 1/2 //(x-2)^2 + (y+3)^2 = 4 Point2 pt3 = Point2(0, -0.5f); Point2 pt4 = Point2(10, -10.5); Point2 out3, out4; int count2 = FindLineCircleIntersections(2, -3, 2, pt3, pt4, out3, out4); //count2 = 2, out3 = 3.64, -4.14 out4 = 0.85, -1.35 return 0; } ¼Ò½º´Â ¾Æ·¡¿¡¼ °¡Áö°í ¿Ô´Ù. Äڵ尡 Àß µ¿ÀÛÇÏ´ÂÁö¸¸ È®ÀÎ Çß°í, ¿ÏÀüÇÏ°Ô ÀÌÇØ ¸øÇ߱⠶§¹®¿¡ º°µµÀÇ ¼³¸íÀº ÇÏÁö ¾Ê´Â´Ù. http://csharphelper.com/blog/2014/09/determine-where-a-line-intersects-a-circle-in-c/ Âü°í) ¼Ò½º https://stackoverflow.com/questions/23016676/line-segment-and-circle-intersection https://blog.naver.com/elkiss/140045975518 https://www.geeksforgeeks.org/check-line-touches-intersects-circle/ ÆǺ°½Ä http://ictmath.honam.ac.kr/category/kika/zutohouteisiki/en-to-tyokusenn-no-kankei.html http://ictmath.honam.ac.kr/category/kansuu/2jihouteisiki/hanbetusiki.html ¹æÁ¤½Ä Ç®±â , ¿¹Á¦ °ª https://www.analyzemath.com/CircleEq/circle_line_intersection.html ¿ø°ú Á÷¼±ÀÇ ¹°¸® https://ericleong.me/research/circle-line/ |