How can I represent a vector equation of a line segment in C++? -
i working computer graphics.
i represent line 2 end points, and, line2d class have method returns vector2d object.
suppose, have following classes:
struct point2d { int x; int y; }; then, can represent line segment using 2 points:
class linesegment2d { private: point2d start; point2d end; public: ... ... }; according definition, vector composed of magnitude , direction.
class vector2d { private: point2d p; public: double magnitude(void); point component(void); vector2d normal(); vector2d & add(vector & rhs); vector2d & subtract(vector & rhs); vector2d & multiply(int scalar); int dotproduct(vector2d rhs); vector2d & crossproduct(vector2d rhs); }; one object of point2d sufficient represent vector. example, magnitude of vector = sqrt(p.x*p.x + p.y*p.y);. and, p.x , p.y collectively represent direction.
on other hand, know vector equation of line passing through (x0,y0,z0) is, r =r0 + tv where, r vector subject line. r0 position vector points direction of point (x0, y0, z0). since, r0 position vector, obviously, origin of r0 (0,0,0). t real numbered value, where, −∞<t<∞ –. v vector parallel our subject straight line.
vector equation of line segment between points p(1, 3, 2) , q(-4, 3, 0):
according above formula, vector equation of line pq either
r =<1,3,2> + tv or,
r =<-4,3,0> + tv the vector connects 2 points p , q is,
pq = <(-4-1), (3-3), (0-2)> = <-5, 0, -2> and, vector parallel our subject line sure.
so, can write,
r =<1, 3, 2> + t <-5, 0, -2> =<1, 3, 2>+<-5t, 0, -2t> = <(1-5t), (3+0), (2-2t)> =<1-5t, 3, 2-2t> according vector equation of line segment, think, vector class should following:
class linevector2d { private: vector2d v; double t; public: .......... }; is correct representation?
if so, how can calculate/set/find value of t?
i think there's confusion because of following
according definition, vector composed of magnitude , direction.
there more 1 way of representing vector. think in question mean vector can represented magnitude (scalar) , unit vector indicating direction. vector can ordered triplet (for 3 dimensions) indicate magnitude (sqrt(x^2 + y^2 + z^2)) , direction origin.
i think answer question is, don't need calculate t. correct me if i'm mistaken, think you're interpreting t magnitude? can calculate v sqrt(x^2 + y^2 + z^2), v can hold both magnitude , direction ordered triplet itself.
edit:
template <typename t> struct point2d { t x; t y; point2d operator + (const point2d<t>& rhs) const { return point2d<t>{x + rhs.x, y + rhs.y}; } point2d operator - (const point2d<t>& rhs) const { return point2d<t>{x - rhs.x, y - rhs.y}; } // ... point2d operator * (const t value) const { return point2d<t>{x*value, y*value}; } point2d operator / (const t value) const { return point2d<t>{x/value, y/value}; } // ... }; template <typename t> class vector2d { private: point2d<t> p; public: vector2d(const point2d<t>& point) : p{point} {} /*double magnitude(); point2d<t> component(); vector2d normal(); int dotproduct(vector2d rhs); vector2d& crossproduct(vector2d rhs);*/ vector2d operator + (const vector2d<t>& rhs) const { return p + rhs.p; } vector2d operator - (const vector2d<t>& rhs) const { return p - rhs.p; } // ... vector2d operator * (const t value) const { return p*value; } vector2d operator / (const t value) const { return p/value; } // ... }; template <typename t> class linevector2d { private: point2d<t> p; vector2d<t> v; public: linevector2d() = default; linevector2d(const point2d<t>& point, const vector2d<t>& direction) : p{point}, v{direction} {} /// returns point on line time/value `t` point2d<t> valueat(t t) { return p + v*t; } };
Comments
Post a Comment