问题描述
我必须在屏幕上渲染一系列相互连接的 2D 线和圆弧(恒定半径),形成一条连续路径(切线可能仍然存在不连续性,所以它只是 C0) .这是 CNC 机械 GUI 中的典型任务。
路径的每个元素可以概括为:
struct Gline
{
int x,y; // [pix] Destination point
double bulge; // normalized sagitta (0:straight-line,1:half-circle)
void get_center_and_radius(int& xc,int& yc,int& r) const; // Makes sense just for arcs (bulge not zero)
};
其中bulge (tan(arc_angle_span/4)
) 代表线的“曲线”,如果为零则线是直的,如果线是半圆,则ecc ...
根据这些数据(实际上也需要起点,但在这种情况下这并不重要)您可以计算出圆弧的中心和半径。
我当前的解决方案使用 windows GDI 函数 Lineto
和 Arc
:
const double toll = 1E-6;
// HDC hdc; // Device context
int x_from = x_start,// The starting point
y_from = y_start;
::Moveto(hdc,x_from,y_from);
std::vector<Gline> gpolyline;
for( const Gline& gline : gpolyline )
{
if( std::fabs(gline.bulge) < toll )
{// Straight line
::Moveto(hdc,y_from);
::Lineto(hdc,gline.x,gline.y);
}
else
{// Arc
int xc=0,yc=0; // [pix] Center coordinates
int r = 0; // [pix] Radius
gline.get_center_and_radius(xc,yc,r);
if( gline.bulge > toll )
{// Positive arc
::Arc(hdc,xc-r,yc-r,xc+r,yc+r,y_from,gline.y);
}
else
{// Negative arc
::Arc(hdc,gline.y,y_from);
}
}
x_from = gline.x;
y_from = gline.y;
}
这有点管用,但这种方法的问题在于直线和圆弧并没有真正连接,没有约束表明直线/圆弧的目标点与下一个圆弧的起点重合。
当视口放大太多时,这会变得很明显,例如 get_center_and_radius
函数中的数字错误变得非常明显。
我一直在寻找类似 polyLine
/polyDraw
的直线和圆弧函数,我想知道是否有一种聪明的方法可以使 polyDraw
适应近似圆弧,但我不确定它是否可以完成或值得。
如果我的解决方案可以在 Windows GDI 领域得到改进,其他人可能如何解决这个问题,或者我是否只需要使用外部图形库,我正在寻求建议。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)