#include <stdio.h>
#include <math.h>
// 構造体の定義
typedef struct {
int x;
int y;
} Point;
// 距離計算関数
double distance(Point p1, Point p2) {
return sqrt((p1.
x - p2.
x) * (p1.
x - p2.
x) + (p1.
y - p2.
y) * (p1.
y - p2.
y)); }
// 点Aと線分BCの最短距離を計算する関数
double shortestDistance(Point A, Point B, Point C) {
// ベクトルの定義
double ABx = B.x - A.x;
double ABy = B.y - A.y;
double BCx = C.x - B.x;
double BCy = C.y - B.y;
// 線分BCの長さ
double BC_length = distance(B, C);
if (BC_length == 0) return distance(A, B); // BとCが同じ点の場合
// 線分BCに対する点Aの投影位置を計算
double t = (ABx * BCx + ABy * BCy) / (BC_length * BC_length);
if (t < 0) {
// プロジェクションが線分BCの外側(点Bに近い)
return distance(A, B);
} else if (t > 1) {
// プロジェクションが線分BCの外側(点Cに近い)
return distance(A, C);
} else {
// プロジェクションが線分BCの内側
Point projection = { B.x + t * BCx, B.y + t * BCy };
return distance(A, projection);
}
}
int main() {
Point A, B, C;
// ユーザーからの入力
printf("点Aの座標を入力してください (x y): "); scanf("%d %d", &A.
x, &A.
y); printf("点Bの座標を入力してください (x y): "); scanf("%d %d", &B.
x, &B.
y); printf("点Cの座標を入力してください (x y): "); scanf("%d %d", &C.
x, &C.
y);
// 最短距離の計算
double distance = shortestDistance(A, B, C);
printf("点Aと線分BCの最短距離は: %.2f\n", distance
);
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxtYXRoLmg+CgovLyDmp4vpgKDkvZPjga7lrprnvqkKdHlwZWRlZiBzdHJ1Y3QgewogICAgaW50IHg7CiAgICBpbnQgeTsKfSBQb2ludDsKCi8vIOi3nembouioiOeul+mWouaVsApkb3VibGUgZGlzdGFuY2UoUG9pbnQgcDEsIFBvaW50IHAyKSB7CiAgICByZXR1cm4gc3FydCgocDEueCAtIHAyLngpICogKHAxLnggLSBwMi54KSArIChwMS55IC0gcDIueSkgKiAocDEueSAtIHAyLnkpKTsKfQoKLy8g54K5QeOBqOe3muWIhkJD44Gu5pyA55+t6Led6Zui44KS6KiI566X44GZ44KL6Zai5pWwCmRvdWJsZSBzaG9ydGVzdERpc3RhbmNlKFBvaW50IEEsIFBvaW50IEIsIFBvaW50IEMpIHsKICAgIC8vIOODmeOCr+ODiOODq+OBruWumue+qQogICAgZG91YmxlIEFCeCA9IEIueCAtIEEueDsKICAgIGRvdWJsZSBBQnkgPSBCLnkgLSBBLnk7CiAgICBkb3VibGUgQkN4ID0gQy54IC0gQi54OwogICAgZG91YmxlIEJDeSA9IEMueSAtIEIueTsKCiAgICAvLyDnt5rliIZCQ+OBrumVt+OBlQogICAgZG91YmxlIEJDX2xlbmd0aCA9IGRpc3RhbmNlKEIsIEMpOwogICAgaWYgKEJDX2xlbmd0aCA9PSAwKSByZXR1cm4gZGlzdGFuY2UoQSwgQik7IC8vIELjgahD44GM5ZCM44GY54K544Gu5aC05ZCICgogICAgLy8g57ea5YiGQkPjgavlr77jgZnjgovngrlB44Gu5oqV5b2x5L2N572u44KS6KiI566XCiAgICBkb3VibGUgdCA9IChBQnggKiBCQ3ggKyBBQnkgKiBCQ3kpIC8gKEJDX2xlbmd0aCAqIEJDX2xlbmd0aCk7CgogICAgaWYgKHQgPCAwKSB7CiAgICAgICAgLy8g44OX44Ot44K444Kn44Kv44K344On44Oz44GM57ea5YiGQkPjga7lpJblgbTvvIjngrlC44Gr6L+R44GE77yJCiAgICAgICAgcmV0dXJuIGRpc3RhbmNlKEEsIEIpOwogICAgfSBlbHNlIGlmICh0ID4gMSkgewogICAgICAgIC8vIOODl+ODreOCuOOCp+OCr+OCt+ODp+ODs+OBjOe3muWIhkJD44Gu5aSW5YG077yI54K5Q+OBq+i/keOBhO+8iQogICAgICAgIHJldHVybiBkaXN0YW5jZShBLCBDKTsKICAgIH0gZWxzZSB7CiAgICAgICAgLy8g44OX44Ot44K444Kn44Kv44K344On44Oz44GM57ea5YiGQkPjga7lhoXlgbQKICAgICAgICBQb2ludCBwcm9qZWN0aW9uID0geyBCLnggKyB0ICogQkN4LCBCLnkgKyB0ICogQkN5IH07CiAgICAgICAgcmV0dXJuIGRpc3RhbmNlKEEsIHByb2plY3Rpb24pOwogICAgfQp9CgppbnQgbWFpbigpIHsKICAgIFBvaW50IEEsIEIsIEM7CgogICAgLy8g44Om44O844K244O844GL44KJ44Gu5YWl5YqbCiAgICBwcmludGYoIueCuUHjga7luqfmqJnjgpLlhaXlipvjgZfjgabjgY/jgaDjgZXjgYQgKHggeSk6ICIpOwogICAgc2NhbmYoIiVkICVkIiwgJkEueCwgJkEueSk7CiAgICBwcmludGYoIueCuULjga7luqfmqJnjgpLlhaXlipvjgZfjgabjgY/jgaDjgZXjgYQgKHggeSk6ICIpOwogICAgc2NhbmYoIiVkICVkIiwgJkIueCwgJkIueSk7CiAgICBwcmludGYoIueCuUPjga7luqfmqJnjgpLlhaXlipvjgZfjgabjgY/jgaDjgZXjgYQgKHggeSk6ICIpOwogICAgc2NhbmYoIiVkICVkIiwgJkMueCwgJkMueSk7CgogICAgLy8g5pyA55+t6Led6Zui44Gu6KiI566XCiAgICBkb3VibGUgZGlzdGFuY2UgPSBzaG9ydGVzdERpc3RhbmNlKEEsIEIsIEMpOwogICAgcHJpbnRmKCLngrlB44Go57ea5YiGQkPjga7mnIDnn63ot53pm6Ljga86ICUuMmZcbiIsIGRpc3RhbmNlKTsKCiAgICByZXR1cm4gMDsKfQo=