I am working with computer graphics.
I would like to represent a line with two end points, and, then I would like my Line2d
class to have a method that
I think there's some confusion because of the following
According to the definition, a vector is composed of a magnitude and a direction.
There is more than one way of representing a vector. I think in your question you mean that a vector can be represented by a magnitude (scalar) and a unit vector indicating a direction. A vector can just be an ordered triplet (for three dimensions) that indicate the magnitude (sqrt(x^2 + y^2 + z^2)
) and the direction from the origin.
I think the answer to your question is, you don't need to calculate t
. Correct me if I'm mistaken, but I think you're interpreting t
as the magnitude? You can calculate that from v
with sqrt(x^2 + y^2 + z^2)
, but v
can hold both a magnitude and direction as an ordered triplet by itself.
Edit:
template
struct Point2d
{
T x;
T y;
Point2d operator + (const Point2d& rhs) const
{
return Point2d{x + rhs.x, y + rhs.y};
}
Point2d operator - (const Point2d& rhs) const
{
return Point2d{x - rhs.x, y - rhs.y};
}
// ...
Point2d operator * (const T value) const
{
return Point2d{x*value, y*value};
}
Point2d operator / (const T value) const
{
return Point2d{x/value, y/value};
}
// ...
};
template
class Vector2d
{
private:
Point2d p;
public:
Vector2d(const Point2d& point) : p{point} {}
/*double Magnitude();
Point2d Component();
Vector2d Normal();
int DotProduct(Vector2d rhs);
Vector2d& CrossProduct(Vector2d rhs);*/
Vector2d operator + (const Vector2d& rhs) const
{
return p + rhs.p;
}
Vector2d operator - (const Vector2d& 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
class LineVector2d
{
private:
Point2d p;
Vector2d v;
public:
LineVector2d() = default;
LineVector2d(const Point2d& point, const Vector2d& direction) : p{point}, v{direction} {}
/// Returns the point on the line for the time/value `t`
Point2d valueAt(T t)
{
return p + v*t;
}
};