Browse Source

直线与平面交点

master
blobt 4 years ago
parent
commit
ed18a6e699
  1. BIN
      .vs/3dMath/v15/.suo
  2. 21
      GMath.cpp
  3. 6
      GMath.h
  4. 11
      main.cpp

BIN
.vs/3dMath/v15/.suo

21
GMath.cpp

@ -1132,3 +1132,24 @@ GVector3 GPlane::GetNormal() const
{
return n;
}
float dist(const GPlane & pi, const GPoint3 & p)
{
float D;
D = (p*pi.n + pi.d) / norm(pi.n);
return D;
}
bool intersect_line_plane(GPoint3 & p, const GLine & l, const GPlane & pi)
{
//如果直线的方向和平面的法线垂直,则直线和平面平行,没有交点
if (EQ_ZERO((l.v*pi.n), PRECISION)) {
return false;
}
float t = -(l.p*pi.n + pi.d) / (l.v * pi.n);
p = l(t);
return true;
}

6
GMath.h

@ -226,6 +226,8 @@ public:
bool IsOnLine(const GPoint3& q);
friend float dist(const GPoint3& q, const GLine& l);
friend bool intersect_line_plane(GPoint3& p, const GLine& l, const GPlane& pi);
};
class GPlane {
@ -241,4 +243,8 @@ public:
GPlane& operator =(const GPlane& rhs);
GVector3 GetNormal() const;
friend float dist(const GPlane& pi, const GPoint3& p);
friend bool intersect_line_plane(GPoint3& p, const GLine& l, const GPlane& pi);
};

11
main.cpp

@ -2,11 +2,14 @@
int main() {
GPlane PI1(GVector3(0.0f, 0.0f, 1.0f), GPoint3(0.0f, 0.0f, 0.0f));
GPlane PI2(GPoint3(1.0,0.0f,0.0f), GPoint3(0.0, 1.0f, 0.0f), GPoint3(0.0, 0.0f, 1.0f));
GPlane PI(1.0f, 1.0f, 1.0f, -1.0);
GLine l(GPoint3(0.0f, 0.0f, 0.0f), GVector3(1.0f, 1.0f, 1.0f));
GPlane PI = PI1;
cout << PI.GetNormal() << endl;
GPoint3 p;
bool ret = intersect_line_plane(p, l, PI);
cout << p << endl;
return 0;
}
Loading…
Cancel
Save