#include <stdio.h>
// 구조체로 점을 정의
typedef struct {
double x;
double y;
} Point;
// 구조체로 직선을 정의 (Ax + By + C = 0)
typedef struct {
double A;
double B;
double C;
} Line;
// 두 점으로부터 직선 방정식 Ax + By + C = 0을 생성
Line getLine(Point p1, Point p2) {
Line line;
line.A = p2.y - p1.y; // y2 - y1
line.B = p1.x - p2.x; // x1 - x2
line.C = p2.x * p1.y - p1.x * p2.y; // x2*y1 - x1*y2
return line;
}
// 두 직선의 교점을 계산
int getIntersection(Line l1, Line l2, Point *intersection) {
double D = l1.A * l2.B - l2.A * l1.B;
if (D == 0) {
return 0; // 평행하거나 일치함
}
intersection->x = (l1.B * l2.C - l2.B * l1.C) / D;
intersection->y = (l2.A * l1.C - l1.A * l2.C) / D;
return 1;
}
int main() {
Point p1, p2, p3, p4, intersection;
printf("첫 번째 직선을 정의할 두 점 입력 (x1 y1 x2 y2): ");
scanf("%lf %lf %lf %lf", &p1.x, &p1.y, &p2.x, &p2.y);
printf("두 번째 직선을 정의할 두 점 입력 (x3 y3 x4 y4): ");
scanf("%lf %lf %lf %lf", &p3.x, &p3.y, &p4.x, &p4.y);
Line l1 = getLine(p1, p2);
Line l2 = getLine(p3, p4);
if (getIntersection(l1, l2, &intersection)) {
printf("두 직선의 교점은: (%.2f, %.2f)\n", intersection.x, intersection.y);
} else {
printf("두 직선은 평행하거나 일치합니다 (교점 없음).\n");
}
return 0;
}