Did I implement this correctly?

Posted by user146780 on Stack Overflow See other posts from Stack Overflow or by user146780
Published on 2010-06-05T23:20:30Z Indexed on 2010/06/05 23:22 UTC
Read the original article Hit count: 210

Filed under:
|
|
|

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'v implemented this like so:

 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.x * 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 outputing are strange. Have I done this wrong?

Thanks

© Stack Overflow or respective owner

Related posts about c++

Related posts about c