title: How can I represent a vector equation of a line segment in C++?
body: <p>I am working with computer graphics.</p>
  
  <p>I would like to represent a line with two end points, and, then I would like my <code>Line2d</code> class to have a method that returns a <code>Vector2d</code> object.</p>
  
  <p>Suppose, I have the following classes:</p>
  
  <pre><code>struct Point2d
  {
      int x;
      int y;
  };
  </code></pre>
  
  <p>Then, I can easily represent a line segment using two points:</p>
  
  <pre><code>class LineSegment2d
  {
  private:
      Point2d start;
      Point2d end;
  public:
      ...
      ...
  };
  </code></pre>
  
  <p>According to the definition, a vector is composed of a magnitude and a direction.</p>
  
  <pre><code>class Vector2d
  {
  private:
      Point2d p;
  public:
      double Magnitude(void);
      Point Component(void);
      Vector2d Normal();
      Vector2d &amp; Add(Vector &amp; rhs);
      Vector2d &amp; Subtract(Vector &amp; rhs);
      Vector2d &amp; Multiply(int scalar);
      int DotProduct(Vector2d rhs);
      Vector2d &amp; CrossProduct(Vector2d rhs);
  };
  </code></pre>
  
  <p>One object of <code>Point2d</code> is sufficient to represent a vector. For example, magnitude of a vector = <code>sqrt(p.x*p.x + p.y*p.y);</code>. And, <code>p.x</code> and <code>p.y</code> collectively represent the direction.</p>
  
  <hr>
  
  <p>On the other hand, we know that the vector equation of a line passing through <code>(x0,y0,z0)</code> is, <code>r =r0 + tv</code>
  Where, 
  <code>r</code> is the vector for the subject line.
  <code>r0</code>  is a position vector that points to the direction of the point <code>(x0, y0, z0)</code>. Since, r0  is a position vector, obviously, the origin of r0  would be <code>(0,0,0)</code>.
  <code>t</code> is any real numbered value, where, <code>−∞&lt;t&lt;∞ –</code>.
  <code>v</code> is a vector which is parallel to our subject straight line.</p>
  
  <p>Vector equation of a line segment between the points <code>P(1, 3, 2)</code> and <code>Q(-4, 3, 0)</code>:</p>
  
  <p>According to the above formula, the vector equation of the line <code>PQ</code> could be either </p>
  
  <pre><code>r =&lt;1,3,2&gt; + tv 
  </code></pre>
  
  <p>or,     </p>
  
  <pre><code>r =&lt;-4,3,0&gt; + tv
  </code></pre>
  
  <p>The vector that connects the two points <code>P</code> and <code>Q</code> is, </p>
  
  <pre><code>PQ  = &lt;(-4-1), (3-3), (0-2)&gt;
      = &lt;-5, 0, -2&gt;
  </code></pre>
  
  <p>And, this vector is parallel to our subject line for sure.</p>
  
  <p>So, we can write,</p>
  
  <pre><code>r   =&lt;1, 3, 2&gt; + t &lt;-5, 0, -2&gt;
      =&lt;1, 3, 2&gt;+&lt;-5t, 0, -2t&gt;
      = &lt;(1-5t), (3+0), (2-2t)&gt;
      =&lt;1-5t, 3, 2-2t&gt;
  </code></pre>
  
  <p>According to the vector equation of a line segment, I think, my Vector class should look like the following:</p>
  
  <pre><code>class LineVector2d
  {
  private:
      Vector2d v;
      double t;
  public:
      ..........
  };
  </code></pre>
  
  <p><strong>Is this the correct representation?</strong></p>
  
  <p>If that so, how can I calculate/set/find the value of <code>t</code>?</p>