Qt中使用微分法绘制圆弧

//微分法绘制圆弧函数
#define PI 3.141592653589793
#define scaleFactor 0.05
//参数分别为圆弧的起始点、终止点、偏移点和顺逆时针方向
QList<QPointF> CreateCurvePoints(QPointF start, QPointF end, QPointF IJ, bool bPositive)
{
	QList<QPointF> R;

	double CX = IJ.x() + start.x();
	double CY = IJ.y() + start.y();
	double LastX = start.x();
	double LastY = start.y();
	double curX = end.x();
	double curY = end.y();
	double dI = IJ.x();
	double dJ = IJ.y();

	double Radius = sqrt(dI * dI + dJ * dJ);
	double HS = atan2(LastY - CY, LastX - CX);
	double HE = atan2(curY - CY, curX - CX);

	if (true == bPositive)
	{
		while (HS <= HE) HS += PI * 2;
	}
	else
	{
		while (HS >= HE) HS -= PI * 2;
	}

	int segs = (int)(scaleFactor * fmax(2.0, Radius) * abs(HS - HE) / (PI * 2));

	if (segs < 10) segs = 10;

	double HEdeg = HE * 360.0 / (PI * 2.0);

	double HSdeg = HS * 360.0 / (PI * 2.0);
	for (int i = 0; i <= segs; i++)
	{
		double P = ((double)i / (double)segs) * (HE - HS) + HS;
		double nx = cos(P) * Radius + CX;
		double ny = sin(P) * Radius + CY;
		R.push_back(QPointF(nx, ny));
	}

	return R;
}

//调用上述函数,获取圆弧的点集
QList<QPointF> arcPoint = CreateCurvePoints(arcStartPoint, endPoint, deviation, positive);
QList<QPointF> region;
for_each(arcPoint.begin(), arcPoint.end(), [&](QPointF& ePoint)
{	
	//1.遍历点集
	//2.将每个点的坐标乘以相应的DPI转化为像素值
	//3.将转化为像素值的点存储到区域中,后面通过绘制区域路径来绘制圆弧
	region.push_back(ePoint);
});

//创建路径,区域的第一个点作为路径的起始点
QPainterPath path(region[0]);

for (int i = 0; i < region.size(); i++)
{
//微分法的原理是将圆弧拆分成若干个小段,绘制一个个微分后的极小的直线来完成圆弧的绘制
	path.lineTo(region[i]);
}
QBrush brush;
brush.setCorlor(Qt:white);
QPainter painter(this);//painter的参数为绘制对象
painter.setPen(Qt::NoPen);
painter.setBrush(brush);
painter.drawPath(path);
painter.setRenderHint(QPainter::SmoothPixmapTransform);

你可能感兴趣的:(Qt,qt,开发语言,c++)