QT画贝塞尔曲线

QT绘制贝塞尔曲线


程序演示
刚开始学习计算机图形学,觉得很有趣,我的水平不高,代码可供参考。

部分代码

typedef struct
{
	float X;
	float Y;
} PointF;

PointF bezier_interpolation_func(float t, PointF *points)
{
	//PointF *tmp_points = (PointF *)malloc(point_count * sizeof(PointF));
	//内存溢出?
	PointF tmp_points[300];
	for (int i = 1; i < point_count; ++i)
	{
		for (int j = 0; j < point_count - i; ++j)
		{
			if (i == 1)
			{
				tmp_points[j].X = (float)(points[j].X * (1 - t) + points[j + 1].X * t);
				tmp_points[j].Y = (float)(points[j].Y * (1 - t) + points[j + 1].Y * t);
				continue;
			}
			tmp_points[j].X = (float)(tmp_points[j].X * (1 - t) + tmp_points[j + 1].X * t);
			tmp_points[j].Y = (float)(tmp_points[j].Y * (1 - t) + tmp_points[j + 1].Y * t);
		}
	}
	return tmp_points[0];
}
void Widget::mouseMoveEvent(QMouseEvent *event)
{
	QPoint p1;
	int x, y;
	int i;
	bool if_in;
	p1 = event->pos();
	x = p1.x();
	y = p1.y();
	if (right_move == true)
	{
		now = point_count - 1;
		point[now].setX(x);
		point[now].setY(y);
		if (point_count > 2)
		{
			bezier_point.clear();
			bezier(point);
		}
		update();
	}
	else
	{
		if (now == 0)
		{
			for (i = 0; i < point_count; i++)
			{
				if (x > point[i].x() - 40 && x < point[i].x() + 40 && y > point[i].y() - 40 && y < point[i].y() + 40)
				{
					now = i;
					break;
				}
			}
		}
		if (con > 0 || x > point[now].x() - 10 && x < point[now].x() + 10 && y > point[now].y() - 10 && y < point[now].y() + 10)
		{
			if_in = true;
		}
		else
		{
			if_in = false;
		}
		if (if_in == true)
		{
			con = 5;
			point[now].setX(x);
			point[now].setY(y);
		}
		if (point_count > 2)
		{
			bezier_point.clear();
			bezier(point);
		}
		update();
	}
}
void Widget::mouseReleaseEvent(QMouseEvent *event)
{
	con = 0;
	now = 0;
	right_move = false;
}

void Widget::mousePressEvent(QMouseEvent *event)
{
	if (event->button() == Qt::RightButton)
	{
		right_move = true;
		Pt = event->pos();
		point.push_back(Pt);
		point_count++;
		if (point_count > 2)
		{
			bezier_point.clear();
			bezier(point);
		}
		update();
	}
	else if (event->button() == Qt::LeftButton)
	{
		update();
	}
	else if (event->button() == Qt::MidButton)
	{
		if (point_count > 0)
		{
			--point_count;
			point.pop_back();
			bezier_point.clear();
			bezier(point);
			update();
		}
	}
}

下载

下载: https://download.csdn.net/download/ryugu_rena/10735781

积分不够或者有建议: [email protected]

猜你喜欢

转载自blog.csdn.net/Ryugu_Rena/article/details/83241810
今日推荐