views:

142

answers:

1

I'm trying to implement line thickness as denoted here:

start = line start = vector(x1, y1)
end = line end = vector(x2, y2)
dir = line direction = end - start = vector(x2-x1, y2-y1)
ndir = normalized direction = dir*1.0/length(dir)
perp = perpendicular to direction = vector(dir.x, -dir.y)
nperp = normalized perpendicular = perp*1.0/length(perp)

perpoffset = nperp*w*0.5
diroffset = ndir*w*0.5


p0, p1, p2, p3 = polygon points:
p0 = start + perpoffset - diroffset
p1 = start - perpoffset - diroffset
p2 = end + perpoffset + diroffset
p3 = end - perpoffset + diroffset 

I have implemented this like this:

 void OGLENGINEFUNCTIONS::GenerateLinePoly(const std::vector<std::vector<GLdouble>> &input, std::vector<GLfloat> &output, int width)
 {
     output.clear();
     float temp;
     float dirlen;
     float perplen;
     POINTFLOAT start;
     POINTFLOAT end;
     POINTFLOAT dir;
     POINTFLOAT ndir;
     POINTFLOAT perp;
     POINTFLOAT nperp;

     POINTFLOAT perpoffset;
     POINTFLOAT diroffset;

     POINTFLOAT p0, p1, p2, p3;

     for(int i = 0; i < input.size() - 1; ++i)
     {
         start.x = input[i][0];
         start.y = input[i][1];

         end.x = input[i + 1][0];
         end.y = input[i + 1][1];

         dir.x = end.x - start.x;
         dir.y = end.y - start.y;

         dirlen = sqrt((dir.x * dir.x) + (dir.y * dir.y));

         ndir.x = dir.x * (1.0 / dirlen);
         ndir.y = dir.y * (1.0 / dirlen);

         perp.x = dir.x;
         perp.y = -dir.y;

         perplen = sqrt((perp.x * perp.x) + (perp.y * perp.y));

         nperp.x = perp.x * (1.0 / perplen);
         nperp.y = perp.y * (1.0 / perplen);

         perpoffset.x = nperp.x * width * 0.5;
         perpoffset.y = nperp.y * width * 0.5;

         diroffset.x = ndir.x * width * 0.5;
         diroffset.y = ndir.y * width * 0.5;

            // p0 = start + perpoffset - diroffset
             //p1 = start - perpoffset - diroffset
             //p2 = end + perpoffset + diroffset
            // p3 = end - perpoffset + diroffset 

         p0.x = start.x + perpoffset.x - diroffset.x;
         p0.y = start.y + perpoffset.y - diroffset.y;

         p1.x = start.x - perpoffset.x - diroffset.x;
         p1.y = start.y - perpoffset.y - diroffset.y;

         p2.x = end.x + perpoffset.x + diroffset.x;
         p2.y = end.y + perpoffset.y + diroffset.y;

         p3.x = end.x - perpoffset.x + diroffset.x;
         p3.y = end.y - perpoffset.y + diroffset.y;


         output.push_back(p0.x);
         output.push_back(p0.y);
         output.push_back(p1.x);
         output.push_back(p1.y);
         output.push_back(p2.x);
         output.push_back(p2.y);
         output.push_back(p3.x);
         output.push_back(p3.y);
     }


 }

but right now the lines look perpendicular and wrong; it should be giving me quads to render which is what I'm rendering, but the points it is outputting are strange. Have I done this wrong?

Thanks

+3  A: 

You are calculating perp incorrectly. It should be (y, -x), not (x, -y). I don't know if that's the only bug. This one just jumped out at me.

As an aside, I strongly recommend that you define a useful vec2 type, with useful helpers like:

vec2 perp(vec2 v) { return vec2(v.y, -v.x); }

That way, your code will look almost the same as your pseudocode. Manipulating x and y individually is much more error-prone and harder to read. It is quite simple to build a basic class for this purpose, though you might be better off finding a third-party implementation to avoid mistakes like the above one. Most game/graphics/physics engines provide a bunch of useful types and functions.

Marcelo Cantos
Thanks :-).....
Milo