我是否正确实现了这一点?

时间:2010-06-05 23:20:31

标签: c++ c algorithm drawing

我正在尝试实现这里表示的线条粗细:

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 

我已经实现了这个:

 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);
     }


 }

但是现在线条看起来是垂直和错误的;它应该给我四边形渲染哪个是我正在渲染的,但它输出的点是奇怪的。我做错了吗?

由于

1 个答案:

答案 0 :(得分:2)

您正在错误地计算perp。它应该是(y, -x),而不是(x, -y)。我不知道这是否是唯一的错误。这个刚跳出来。

顺便说一句,我强烈建议您定义一个有用的vec2类型,并使用有用的帮助器:

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

这样,您的代码看起来与您的伪代码几乎相同。单独操作xy更容易出错并且难以阅读。为此目的构建一个基本类非常简单,尽管你可能最好找到第三方实现来避免像上面那样的错误。大多数游戏/图形/物理引擎提供了许多有用的类型和功能。