diff options
author | Juan Linietsky <reduzio@gmail.com> | 2018-05-07 17:54:33 -0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-05-07 17:54:33 -0300 |
commit | 69a96ff4a627fabea2bc40ed27b973f202c44825 (patch) | |
tree | c31f2f35240d66f7cb8bb2622f541566c028b987 /core/math/geometry.h | |
parent | cbd849d13fec42302c0a6e96531c54a71c7d43f8 (diff) | |
parent | c1e099b48f7fe4dc114d10c49f2d49c58a91e40f (diff) | |
download | redot-engine-69a96ff4a627fabea2bc40ed27b973f202c44825.tar.gz |
Merge pull request #15943 from poke1024/geometry-line-line
Add Geometry::line_intersects_line_2d()
Diffstat (limited to 'core/math/geometry.h')
-rw-r--r-- | core/math/geometry.h | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/core/math/geometry.h b/core/math/geometry.h index 73a53c53b6..be998aef0b 100644 --- a/core/math/geometry.h +++ b/core/math/geometry.h @@ -529,6 +529,21 @@ public: return p_segment[0] + n * d; // inside } + static bool line_intersects_line_2d(const Vector2 &p_from_a, const Vector2 &p_dir_a, const Vector2 &p_from_b, const Vector2 &p_dir_b, Vector2 &r_result) { + + // see http://paulbourke.net/geometry/pointlineplane/ + + const real_t denom = p_dir_b.y * p_dir_a.x - p_dir_b.x * p_dir_a.y; + if (Math::abs(denom) < CMP_EPSILON) { // parallel? + return false; + } + + const Vector2 v = p_from_a - p_from_b; + const real_t t = (p_dir_b.x * v.y - p_dir_b.y * v.x) / denom; + r_result = p_from_a + t * p_dir_a; + return true; + } + static bool segment_intersects_segment_2d(const Vector2 &p_from_a, const Vector2 &p_to_a, const Vector2 &p_from_b, const Vector2 &p_to_b, Vector2 *r_result) { Vector2 B = p_to_a - p_from_a; |