0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Heron's Shortest Path Problem (Shortest Path Problem) 「算数の鏡写しの水くみ、反射 の問題は何年生で学習しますか。」

0
Posted at

・Gemini先生への履歴は後半です。wikpedia のリンクあります。
・>...小学6年生で本格的に学習します。...
・>...直感的には人間の生活(水くみ)が先で、それを厳密な幾何学にしたのがヘロン、そしてそれを再びわかりやすい教育用ストーリーに戻したのが「水くみ問題」、とも言えますね。

Gemini先生へ。

(回答省略)

sympyで

・水くみ問題(最短経路問題)の計算と対称移動の描画

# 水くみ問題(最短経路問題)の計算と対称移動の描画

import matplotlib.pyplot as plt
import sympy as sp

# --------------------------------------------------
# 1. SymPy によるシンボリック計算
# --------------------------------------------------

# 川のライン(x軸: y = 0)および地点A, Bの設定
# 例: A(1, 4), B(7, 2)
x_A, y_A = 1, 4
x_B, y_B = 7, 2

A = sp.Point(x_A, y_A)
B = sp.Point(x_B, y_B)

# 川を表す直線 L (y = 0)
L = sp.Line((0, 0), (1, 0))

# B の川(x軸)に関する線対称な点 B'
# x座標は変わらず、y座標の符号が反転する: B'(7, -2)
B_prime = B.reflect(L)

# A と B' を通る直線を定義
line_AB_prime = sp.Line(A, B_prime)

# 直線 AB' と川 L (x軸) の交点 P を求める
intersection = line_AB_prime.intersection(L)
P = intersection[0]  # 交点 Point2D(5, 0)

# 最短距離 (A-P 間の距離 + P-B 間の距離 = A-B' 間の直線距離)
min_distance = A.distance(B_prime)

print(f"出発地点 A: {A.evalf()}")
print(f"目的地点 B: {B.evalf()}")
print(f"対称点 B': {B_prime.evalf()}")
print(f"最適な水くみ地点 P: {P.evalf()}")
print(f"最短移動距離: {min_distance} (約 {min_distance.evalf():.4f})")

# --------------------------------------------------
# 2. Matplotlib による視覚化
# --------------------------------------------------

fig, ax = plt.subplots(figsize=(8, 6))

# 点のプロット
points = {"A": A, "B": B, "B'": B_prime, "P": P}
coords = {name: (float(pt.x), float(pt.y)) for name, pt in points.items()}

# 川 (x軸) の描画
ax.axhline(0, color="blue", linewidth=2, label="River (y = 0)")

# 折れ線 (A -> P -> B) : 実際の移動経路
ax.plot(
    [coords["A"][0], coords["P"][0], coords["B"][0]],
    [coords["A"][1], coords["P"][1], coords["B"][1]],
    "r-o",
    linewidth=2,
    label="Path (A -> P -> B)",
)

# 補助線 (A -> B') : 線対称による直線化
ax.plot(
    [coords["A"][0], coords["B'"][0]],
    [coords["A"][1], coords["B'"][1]],
    "g--",
    alpha=0.7,
    label="Straight line (A -> B')",
)

# 補助線 (P -> B')
ax.plot(
    [coords["P"][0], coords["B'"][0]],
    [coords["P"][1], coords["B'"][1]],
    "r:",
    alpha=0.7,
)

# 各点のマーカーとラベル表示
for name, (x, y) in coords.items():
    ax.scatter(x, y, color="black", zorder=5)
    ax.annotate(
        f" {name}({x:.0f}, {y:.0f})",
        (x, y),
        textcoords="offset points",
        xytext=(5, 5),
        ha="left",
    )

# グラフの整形
ax.set_title("Shortest Path Problem (Water Fetching Problem)")
ax.set_xlabel("x")
ax.set_ylabel("y")
ax.axvline(0, color="gray", linestyle="--", alpha=0.5)
ax.grid(True, linestyle=":", alpha=0.6)
ax.legend(loc="upper right")
ax.set_aspect("equal", adjustable="datalim")

plt.show()

計算結果

出発地点 A: Point2D(1.0, 4.0)
目的地点 B: Point2D(7.0, 2.0)
対称点 B': Point2D(7.0, -2.0)
最適な水くみ地点 P: Point2D(5.0, 0)
最短移動距離: 6*sqrt(2) (約 8.4853)

image.png

Gemini先生への履歴

1. 算数の鏡写しの水くみ、反射 の問題は何年生で学習しますか。
2. 水くみ問題(最短経路問題)の具体的な解き方と線対称を使った考え方を教えてください。
3. 水くみ問題(最短経路問題)をsympyのソースコードで教えて下さい。
4. 水くみ問題(最短経路問題)を英語に翻訳して下さい。
5. どうしてHeronなのですか。
6. 私は 人間が川で水をくむイメージ の方が先だと思っていました。
7. 上記のwikipediaのurlを教えて下さい。
8. 上記のsympyのソースコードをPNG図だけ出力して下さい。
9. 最初のsympyのソースコードを関数を使ったグラフのソースコードに変換して下さい。何次関数になりますか。
10. 最初のsympyのソースコードを微分を使ったグラフのソースコードに変換して下さい。
11. ありがとうございました。
12. 上記の私の質問すべてから、Markdownのテキスト形式で、質問リストを出力して。追番を追加して。本質問も含めて。コピーできる形式で。空白行不要です。コピーできる様式で

いつもと違うおすすめです。

wikipedia で

sympy doc

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?