如何用C ++表示线段的向量方程? [英] How can I represent a vector equation of a line segment in C++?

查看:127
本文介绍了如何用C ++表示线段的向量方程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用计算机图形。

I am working with computer graphics.

我想表示一个有两个端点的行,然后我想要 Line2d 类具有返回 Vector2d 对象的方法。

I would like to represent a line with two end points, and, then I would like my Line2d class to have a method that returns a Vector2d object.

有以下类:

struct Point2d
{
    int x;
    int y;
};


class LineSegment2d
{
private:
    Point2d start;
    Point2d end;
public:
    ...
    ...
};

根据定义,向量由大小和方向组成。

According to the definition, a vector is composed of a magnitude and a 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);
};

Point2d 的一个对象足以表示矢量。例如,向量的大小= sqrt(p.x * p.x + p.y * p.y); px py 共同代表了方向。

One object of Point2d is sufficient to represent a vector. For example, magnitude of a vector = sqrt(p.x*p.x + p.y*p.y);. And, p.x and p.y collectively represent the direction.

另一方面,我们知道通过(x0,y0,z0)的行的向量方程是: r = r0 + tv
其中,
r 是主题行的向量。
r0 是指向点的方向的位置向量(x0,y0,z0) 。因为,r0是位置向量,显然,r0的原点将是(0,0,0)
t 是任何实数值,其中-∞
v 是与我们的主题直线平行的向量。

On the other hand, we know that the vector equation of a line passing through (x0,y0,z0) is, r =r0 + tv Where, r is the vector for the subject line. r0 is a position vector that points to the direction of the point (x0, y0, z0). Since, r0 is a position vector, obviously, the origin of r0 would be (0,0,0). t is any real numbered value, where, −∞<t<∞ –. v is a vector which is parallel to our subject straight line.

段之间 P(1,3,2) Q(-4,3,0)

根据上面的公式,线 PQ 的向量方程可以是

According to the above formula, the vector equation of the line PQ could be either

r =<1,3,2> + tv 

或,

r =<-4,3,0> + tv

连接两个点的向量 P Q 是,

The vector that connects the two points P and Q is,

PQ  = <(-4-1), (3-3), (0-2)>
    = <-5, 0, -2>

而且,这个向量与我们的主题行是平行的。

And, this vector is parallel to our subject line for sure.

所以,我们可以写

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>

根据线段的向量方程,我认为我的Vector类应该如下:

According to the vector equation of a line segment, I think, my Vector class should look like the following:

class LineVector2d
{
private:
    Vector2d v;
    double t;
public:
    ..........
};

这是正确的表示吗?

如果是这样,我如何计算/设置/找到 t 的值?

If that so, how can I calculate/set/find the value of t?

推荐答案

我认为有一些混乱,因为下面的

I think there's some confusion because of the following


根据定义,是由大小和方向组成的。

According to the definition, a vector is composed of a magnitude and a direction.

有多种表示向量的方法。我认为在你的问题你的意思是一个向量可以表示一个幅度(标量)和一个单位向量指示一个方向。向量可以仅是指示大小( sqrt(x ^ 2 + y ^ 2 + z ^ 2))的有序三元组原因。

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.

我认为你的问题的答案是,你不需要计算 t 。纠正我,如果我错了,但我认为你解释 t 作为幅度?你可以计算从 v sqrt(x ^ 2 + y ^ 2 + z ^ 2) code> v 可以作为有序三元组自身保存幅度和方向。

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.

>

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 the point on the line for the time/value `t`
    Point2d<T> valueAt(T t)
    {
        return p + v*t;
    }
};

这篇关于如何用C ++表示线段的向量方程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆