在OpenGL中沿坡度绘制图像

在OpenGL中沿坡度绘制图像,opengl,graphics,Opengl,Graphics,我正在写一个程序,可以用填充圆在两点之间画一条线。圆圈: -不应该互相重叠 -尽量靠近 -每个圆的中心应该在这条线上 我已经编写了一个函数来生成这些圆,但是我在计算每个圆的位置时遇到了问题,所以它们是正确排列的 void addCircles(scrPt endPt1, scrPt endPt2) { float xLength, yLength, length, cSquare, slope; int numberOfCircles; // Get the x distance

我正在写一个程序,可以用填充圆在两点之间画一条线。圆圈: -不应该互相重叠 -尽量靠近 -每个圆的中心应该在这条线上

我已经编写了一个函数来生成这些圆,但是我在计算每个圆的位置时遇到了问题,所以它们是正确排列的

void addCircles(scrPt endPt1, scrPt endPt2)
{
  float xLength, yLength, length, cSquare, slope;
  int numberOfCircles;

  // Get the x distance between the two points
  xLength = abs(endPt1.x - endPt2.x);

  // Get the y distance between the two points
  yLength = abs(endPt1.y - endPt2.y);

  // Get the length between the points
  cSquare = pow(xLength, 2) + pow(yLength, 2);
  length = sqrt(cSquare);

  // calculate the slope
  slope = (endPt2.y - endPt1.y) / (endPt2.x - endPt1.x);

  // Find how many circles fit inside the length
  numberOfCircles = round(length / (radius * 2) - 1);

  // set the position of each circle
  for (int i = 0; i < numberOfCircles; i++)
  {
     scrPt circPt;
     circPt.x = endPt1.x + ((radius * 2) * i);
     circPt.y = endPt1.y + (((radius * 2) * i) * slope);

     changeColor();
     drawCircle (circPt.x, circPt.y);
  }

非常感谢您的帮助

我建议您将线路的方向计算为:

计算从一个中心点到下一个中心点的距离,
numberOfSegments
是截面数,而不是圆数:

int numberOfSegments = (int)trunc( length / (radius * 2) );
float distCpt = numberOfSegments == 0 ? 0.0f : length / (float)numberOfSegments;
圆的中心点是通过将向量添加到直线的起点来计算的。直线方向上的矢量pints及其长度由两个圆之间的距离乘以圆的“指数”得出:

circPt.y = endPt1.y + (((radius * 2) * i) * slope);
for (int i = 0; i <= numberOfSegments; i++)
{
   float cpt_x = endPt1.x + xDir * distCpt * (float)i;
   float cpt_y = endPt1.y + yDir * distCpt * (float)i;     

   changeColor();
   drawCircle(cpt_x , cpt_y);
}   
for(int i=0;i
for (int i = 0; i <= numberOfSegments; i++)
{
   float cpt_x = endPt1.x + xDir * distCpt * (float)i;
   float cpt_y = endPt1.y + yDir * distCpt * (float)i;     

   changeColor();
   drawCircle(cpt_x , cpt_y);
}   
for (int i = 0; i < numberOfSegments; i++)