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

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


 }

但是现在这些线看起来是垂直的而且是错误的;它应该给我四边形来渲染,这就是我正在渲染的,但它输出的点很奇怪。我做错了吗?

谢谢

最佳答案

您计算的 perp 不正确。它应该是 (y, -x),而不是 (x, -y)。我不知道这是不是唯一的错误。这个突然向我袭来。

顺便说一句,我强烈建议您定义一个有用的 vec2 类型,以及一些有用的助手,例如:

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

这样,您的代码看起来几乎与您的伪代码相同。单独操作 xy 更容易出错,也更难阅读。为此目的构建一个基本类非常简单,但您最好还是找到第三方实现来避免上述错误。大多数游戏/图形/物理引擎都提供了一堆有用的类型和函数。

关于c++ - 我是否正确实现了这一点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2982505/

相关文章:

c - 在链表中的第n个位置插入一个节点;这段代码有什么问题?

algorithm - MATLAB中的相邻灰度相关性矩阵(NGLDM)

C++通过引用传递?

c++ - OpenGL 相机类

java - 使用界面在后台运行 matlab

c - 需要像 read() 这样的函数将整数数据读入缓冲区并获得与 read() 相同的缓冲区值

arrays - 来自返回数组最大值的函数的 C 段错误

C++ 链接错误与 linux 上的多个定义

arrays - 查找数组中的三元组 i、j、k 的数量,使得索引为 i 到 j-1 的元素的异或等于索引为 j 到 k 的元素的异或

python - 遗传算法-无序可变长度染色体-交叉策略?