geometry

# # Explanation

Let the center point of a circle $C$, radius $R$、2 points which a line $l$ go through $p_1$, $p_2$.
Let cross points of the line and the circle $a$ and $b$.

## # 1. Calculate cross point $p$ of a perpendicular from $C$ to $l$ and $l$

Use projection formula

Vector2 project(Vector2 p1, Vector2 p2, Vector2 c) {
Vector2 v = p2 - p1;
return p1 + v * (v.dot(c - p1) / v.norm());
}


## # 2. Calculate unit vector \vec

Devide a vector with its length.

Vector2 unitVec(Vector2 v) {
return v / v.length();
}


## # 3. Calculate the length of segment $ab$

$p$ devides $ab$ into 2 equal parts.
$\angle{Cpb}$ is a right angle.
Length of $ap$ and $pb$ is, by Pythegorean theorem,

$ap = pb = \sqrt{R^2 - |\vec{Cp}|^2}$

## # 4. Calculate cross points

$a, b = \vec{p} + \vec{e} \cdot \pm ap$

# # Entire Code

double R;
Vector2 C, p1, p2;

Vector2 project(Vector2 p1, Vector2 p2, Vector2 c) {
Vector2 v = p2 - p1;
return p1 + v * (v.dot(c - p1) / v.norm());
}

Vector2 unitVec(Vector2 v) {
return v / v.length();
}

pair<Vector2, Vector2> crossPoints() {
Vector2 p = project(p1, p2, C);
Vector2 e = unitVec(p2 - p1);
double len = sqrt(R*R - (p - C).norm());
Vector2 a = p + e * (-len);
Vector2 b = p + e * len;
if (fabs(a.x) < EPS) a.x = 0.0;
if (fabs(a.y) < EPS) a.y = 0.0;
if (fabs(b.x) < EPS) b.x = 0.0;
if (fabs(b.y) < EPS) b.y = 0.0;
if (a < b) return make_pair(a, b);
return make_pair(b, a);
}


Remote freelancer. A web and mobile application enginner.
Traveling around the world based on East Asia.
I'm looking forward to your job offers from all over the world!

Offer jobs or contact me!
{"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 25, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "url": "https://v1.shunsuke.me/en/algorithm/geometry/line_circle_cross_points/", "fetch_time": 1725914556000000000, "content_mime_type": "text/html", "warc_filename": "crawl-data/CC-MAIN-2024-38/segments/1725700651157.15/warc/CC-MAIN-20240909201932-20240909231932-00556.warc.gz", "warc_record_offset": 560342220, "warc_record_length": 37580, "token_count": 561, "char_count": 1619, "score": 4.3125, "int_score": 4, "crawl": "CC-MAIN-2024-38", "snapshot_type": "latest", "language": "en", "language_score": 0.47658371925354004, "file_path": "/sailhome/yjruan/project/latent-lm/data/processed/finemath-4plus/finemath-4plus/train-00050-of-00064.parquet"}