LoginSignup
0
0

アーカイブ:直線の交点座標

Posted at
function prog(x1, y1, x2, y2, x3, y3, x4, y4)
    """
    (x1,y1)と(x2,y2)を通る直線と,(x3,y3)と(x4,y4)を通る直線の交点の座標を求める
    戻り値は交点の x, y 座標。交点がない場合は (NaN, NaN) を返す。
    """
    if x1 == x2 && x3 == x4
        x = y = NaN
    elseif x1 == x2
        x = x1
        y = (y4 - y3)/(x4 - x3)*(x1 - x3) + y3
    elseif x3 == x4
        x = x3
        y = (y2 - y1)/(x2 - x1)*(x3 - x1) + y1
    else
        a1 = (y2 - y1)/(x2 - x1)
        a3 = (y4 - y3)/(x4 - x3)
        if (a1 == a3)
            x = y = NaN
        else
            x = (a1*x1 - y1 - a3*x3 + y3)/(a1 - a3)
            y = (y2 - y1)/(x2 - x1)*(x - x1) + y1
        end
    end
    return (x, y)
end;
prog(4, 8, 8, 0, 2, 2, 12, 7) |> println
prog(4, 1, 4, 5, -1, -1, 6, 3) |> println
prog(-1, -1, 6, 3, 4, 1, 4, 5) |> println
prog(-1, -1, 6, 3, 4, 1, 4+1e-3, 5) |> println
prog(1, 2, 5, 2, 0, 3, 8, 3) |> println
prog(1, 1, 5, 5, -1, 0, 6, 7) |> println
prog(-5, 2, -5, 8, 2, 3, 2, 7) |> println
(6.0, 4.0)
(4, 1.8571428571428568)
(4, 1.8571428571428568)
(4.000214316330904, 1.8572653236176593)
(NaN, NaN)
(NaN, NaN)
(NaN, NaN)
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