CGL_2_C Cross Point
Table of contents
# Problem
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_C
Find a cross point of given 2 segments.
# Explanation
Algorithm to find cross point of 2 segments
# Solution
Int N;
Vector2 p0, p1, p2, p3;
Vector2 crossPoint() {
Vector2 baseSeg = p1 - p0;
Vector2 crossSeg = p3 - p2;
double h1 = fabs(baseSeg.cross(p2 - p0)) / (baseSeg.length() * 2);
double h2 = fabs(baseSeg.cross(p3 - p0)) / (baseSeg.length() * 2);
return p2 + crossSeg * h1 / (h1 + h2);
}
void solve() {
Vector2 p = crossPoint();
if (fabs(p.x) < EPS) cout << 0;
else cout << p.x;
cout << ' ';
if (fabs(p.y) < EPS) cout << 0;
else cout << p.y;
cout << endl;
}
void input() {
cin >> N;
while (cin >> p0 >> p1 >> p2 >> p3) {
solve();
}
}
int main() {
cout.precision(15);
input();
}
Shun
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!