45
52

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 2015-03-26

線分abと、線分cdが交錯しているかどうかの判定。

var judgeIentersected = function(ax, ay, bx, by, cx, cy, dx, dy) {
  var ta = (cx - dx) * (ay - cy) + (cy - dy) * (cx - ax);
  var tb = (cx - dx) * (by - cy) + (cy - dy) * (cx - bx);
  var tc = (ax - bx) * (cy - ay) + (ay - by) * (ax - cx);
  var td = (ax - bx) * (dy - ay) + (ay - by) * (ax - dx);

  return tc * td < 0 && ta * tb < 0;
  // return tc * td <= 0 && ta * tb <= 0; // 端点を含む場合
};

trueが返る場合は2つの線分が交差している。
falseが返る場合は2つの線分が交差していない。

以下デモ。
http://codepen.io/ykob/details/vEzRPX/


以下のページの内容を参考にしました。というか式はそのまま。
http://www5d.biglobe.ne.jp/~tomoya03/shtml/algorithm/Intersection.htm

45
52
1

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
45
52

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?