0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

2点の座標の距離・一次関数方程式の導出・4点の座標から交点を導出(Pythonでライブラリを使わずに)

Posted at

きっかけ

競技プログラミングの勉強中に、2点の座標から距離と一次関数方程式を導いたり、4点の座標から一次関数を2つ導出して交点座標(対角線の交点)を導出する方法を調べていたところ、ライブラリを使う方法は見つかるのですが、素朴に計算していくものがなかったので作成しました。

※注意!

ある程度の確認は行いましたが、誤りがある可能性もあります。あくまで使用は自己責任でお願いいたします。

# 2点の距離を返す
def makedistance(x1, y1, x2, y2):
    d = ((x2 - x1) ** 2 + (y2 - y1) ** 2)**0.5
    return d

# 2点の傾きと切片を返す
def makeliner(x1, y1, x2, y2):
    if x2-x1 == 0:
        return 'line'
    else:
        return (y2-y1)/(x2-x1),(x2*y1-x1*y2)/(x2-x1)

# 点A・点Cがなす直線と、点B・点Dがなす直線の交点を返す
def makeintersection(ax,ay,bx,by,cx,cy,dx,dy):
    # ACの直線の傾きと切片
    ac_steep, ac_intercept = (cy-ay)/(cx-ax),(cx*ay-ax*cy)/(cx-ax)
    # BDの直線の傾きと切片
    bd_steep, bd_intercept = (dy-by)/(dx-bx),(dx*by-bx*dy)/(dx-bx)
    # 傾きが同じなら平行
    div = ac_steep - bd_steep
    if div == 0:
        return 'parallel'
    else:
        return (bd_intercept-ac_intercept)/div, (ac_steep*bd_intercept-bd_steep*ac_intercept)/div

0
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?