#include <stdio.h>
#include <math.h>
// 構造体の定義
typedef struct {
int x;
int y;
} Point;
// 距離計算関数
double distance(Point p1, Point p2) {
}
// 点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+mWouaVsApkb3VibGUgZGlzdGFuY2UoUG9pbnQgcDEsIFBvaW50IHAyKSB7CiAgICByZXR1cm4gc3FydChwb3coYWJzKHAxLnggLSBwMi54KSwgMikgKyBwb3coYWJzKHAxLnkgLSBwMi55KSwgMikpOwp9CgovLyDngrlB44Go57ea5YiGQkPjga7mnIDnn63ot53pm6LjgpLoqIjnrpfjgZnjgovplqLmlbAKZG91YmxlI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gICByZXR1cm4gZGlzdGFuY2UoQSwgcHJvamVjdGlvbik7CiAgICB9Cn0KCmludCBtYWluKCkgewogICAgUG9pbnQgQSwgQiwgQzsKCiAgICAvLyDjg6bjg7zjgrbjg7zjgYvjgonjga7lhaXlipsKICAgIHByaW50Zigi54K5QeOBruW6p+aomeOCkuWFpeWKm+OBl+OBpuOBj+OBoOOBleOBhCAoeCB5KTogIik7CiAgICBzY2FuZigiJWQgJWQiLCAmQS54LCAmQS55KTsKICAgIHByaW50Zigi54K5QuOBruW6p+aomeOCkuWFpeWKm+OBl+OBpuOBj+OBoOOBleOBhCAoeCB5KTogIik7CiAgICBzY2FuZigiJWQgJWQiLCAmQi54LCAmQi55KTsKICAgIHByaW50Zigi54K5Q+OBruW6p+aomeOCkuWFpeWKm+OBl+OBpuOBj+OBoOOBleOBhCAoeCB5KTogIik7CiAgICBzY2FuZigiJWQgJWQiLCAmQy54LCAmQy55KTsKCiAgICAvLyDmnIDnn63ot53pm6Ljga7oqIjnrpcKICAgIGRvdWJsZSBkaXN0YW5jZSA9IHNob3J0ZXN0RGlzdGFuY2UoQSwgQiwgQyk7CiAgICBwcmludGYoIueCuUHjgajnt5rliIZCQ+OBruacgOefrei3nembouOBrzogJS4yZlxuIiwgZGlzdGFuY2UpOwoKICAgIHJldHVybiAwOwp9Cg==