2
2

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 5 years have passed since last update.

二点間の距離を求める

Last updated at Posted at 2017-03-11

dx = x1 - x2;
dy = y1 - y2;
とする。この二点間の距離を求めたい。

普通はこうだ。

d = sqrt(dx * dx + dy * dy)

そのための専用の関数もある。

d = hypot(dx, dy)

数学的な詳細はわからないが、オーバーフローが発生しないように、工夫されているらしい。性能では多少劣る。

自作の画像処理アプリを作ろうとして、hypotについて調べていたところ、別の方法をみつけた。「Moler-Morrison Algorithm」という。コードは掲載しないが、気になる人は各自検索してほしい。四則演算からなる数行の式を3回程度繰り返すだけで、double型で必要充分な精度に収束する。

数学は私の専門ではないので、説明とか証明はできない。精度と速度はこれから検証してみる。ウェブで検索してみると、これに関する日本語の説明が少ない。このアルゴリズムは有名なものなのだろうか?

2
2
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
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?