How can I represent a vector equation of a line segment in C++?

后端 未结 6 1937
青春惊慌失措
青春惊慌失措 2021-01-19 17:59

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

6条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-19 18:24

    If you want to convert a line segment to a vector, you have to be aware that there is no "universal semantic" for the conversion, it is up to you to define what the conversion means. That said, I assume you want a vector with the same (Euclidean) norm as the length of the line segment and pointing in the same direction, i.e. something like this:

    class LineSegment2d
    {
       ....
       Vector2d getVector() const {
          return Vector2d(end) - Vector2d(start);
       }
    };
    

    In other words, offset the line segment to the origin point of the coordinate system. The end point can then be converted to a vector.

    EDIT: After understanding a bit more about why you want this, you might be looking for another representation

    class LineSegment2d
    {
       ....
       Vector2d getVector() const {
          return Vector2d(end);
       }
    };
    

    This will get you one vector for each line segment: the end point. If your polygons consist of connected line segments, this will give you all the vertices in the polygon.

提交回复
热议问题