straight class
Author: Bernhard Wymann
Keywords: torcs, berniw, bernhard wymann, robot, steer, straight

# 4.2 The Straight Class ### Overview Like you will see later it is also nice to have a straight class. It's purpose is to compute intersection points of straights and distances between points and straights. ### Implementation Put the following code also into linalg.h. Like you can see the straights are described with two 2D-vectors. One holds a point on the straight and the other the direction. Because we normalize the direction in the constructor we can simplify the computation of the distance to a point.
    class Straight {
        public:
            /* constructors */
            Straight() {}
            Straight(float x, float y, float dx, float dy)
                {  p.x = x; p.y = y; d.x = dx; d.y = dy; d.normalize(); }
            Straight(const v2d &anchor, const v2d &dir)
                { p = anchor; d = dir; d.normalize(); }

            /* methods */
            v2d intersect(const Straight &s) const;
            float dist(const v2d &p) const;

            /* data */
            v2d p;          /* point on the straight */
            v2d d;          /* direction of the straight */
    };
    /* intersection point of *this and s */
    inline v2d Straight::intersect(const Straight &s) const
    {
        float t = -(d.x*(s.p.y-p.y)+d.y*(p.x-s.p.x))/(d.x*s.d.y-d.y*s.d.x);
        return s.p + s.d*t;
    }
    /* distance of point s from straight *this */
    inline float Straight::dist(const v2d &s) const
    {
        v2d d1 = s - p;
        v2d d3 = d1 - d*d1*d;
        return d3.len();
    }
    #endif // _LINALG_H_
### Summary You know how to deal with straights. You implemented the above stuff in linalg.h.