Go to the documentation of this file.00001
00005 #include <blort/Recognizer3D/Vector2.hh>
00006
00007 namespace P
00008 {
00009
00014 Vector2 LineIntersection(const Vector2 &p1, const Vector2 &d1,
00015 const Vector2 &p2, const Vector2 &d2) throw(Except)
00016 {
00017 double d = Cross(d2, d1);
00018 if(d == 0.)
00019 throw Except(__HERE__, "lines do not intersect");
00020 double l = Cross(d2, p2 - p1)/d;
00021 return Vector2(p1.x + l*d1.x, p1.y + l*d1.y);
00022 }
00023
00031 Vector2 LineIntersection(const Vector2 &p1, const Vector2 &d1,
00032 const Vector2 &p2, const Vector2 &d2, double *l1, double *l2) throw(Except)
00033 {
00034 double d = Cross(d2, d1);
00035 if(d == 0.)
00036 throw Except(__HERE__, "lines do not intersect");
00037 Vector2 p12 = p2 - p1;
00038 *l1 = Cross(d2, p12)/d;
00039 *l2 = Cross(d1, p12)/d;
00040 return Vector2(p1.x + *l1*d1.x, p1.y + *l1*d1.y);
00041 }
00042
00046 bool LinesIntersecting(const Vector2 &a1, const Vector2 &a2,
00047 const Vector2 &b1, const Vector2 &b2)
00048 {
00049 double l1 = 0., l2 = 0.;
00050 LineIntersection(a1, a2 - a1, b1, b2 - b1, &l1, &l2);
00051 return (l1 >= 0. && l1 <= 1.) && (l2 >= 0. && l2 <= 1.);
00052 }
00053
00057 bool LinesIntersecting(const Vector2 &a1, const Vector2 &a2,
00058 const Vector2 &b1, const Vector2 &b2, Vector2 &isct)
00059 {
00060 double l1 = 0., l2 = 0.;
00061 isct=LineIntersection(a1, a2 - a1, b1, b2 - b1, &l1, &l2);
00062 return (l1 >= 0. && l1 <= 1.) && (l2 >= 0. && l2 <= 1.);
00063 }
00064
00065 }
00066