OpenGL利用DDA算法绘制图形,并增加鼠标键盘交互

复制代码
#include <GL/glut.h>
#include <math.h>

// 定义角度常数
const float PI = 3.14159265358979323846f;
const int RADIUS = 100; // 六边形外接圆的半径

// 当前颜色
float currentColor[3] = { 0.0f, 0.0f, 0.0f }; // 初始颜色为黑色

// 当前旋转角度
float rotationAngle = 0.0f;

// 计算六个顶点的坐标
void calculateHexagonVertices(float centerX, float centerY, float radius, float vertices[6][2]) {
	for (int i = 0; i < 6; i++) {
		float angle = (i * 60) * PI / 180;  // 每个顶点之间的角度为60°
		vertices[i][0] = centerX + radius * cos(angle);
		vertices[i][1] = centerY + radius * sin(angle);
	}
}

// 使用DDA算法绘制一条线
void ddaLine(float x1, float y1, float x2, float y2) {
	float dx = x2 - x1;
	float dy = y2 - y1;
	float steps = (fabs(dx) > fabs(dy)) ? fabs(dx) : fabs(dy);  // 计算步数
	float xIncrement = dx / steps;
	float yIncrement = dy / steps;

	float x = x1;
	float y = y1;

	glBegin(GL_POINTS);
	for (int i = 0; i <= steps; i++) {
		glVertex2i(round(x), round(y));  // 绘制点
		x += xIncrement;
		y += yIncrement;
	}
	glEnd();
}

// 绘制六芒星
void drawHexagram(float centerX, float centerY, float radius) {
	// 计算六个顶点
	float vertices[6][2];
	calculateHexagonVertices(centerX, centerY, radius, vertices);

	// 保存当前矩阵
	glPushMatrix();

	// 平移坐标系,将六芒星的中心移动到原点
	glTranslatef(centerX, centerY, 0.0f);

	// 旋转六芒星
	glRotatef(rotationAngle, 0.0f, 0.0f, 1.0f);

	// 再次平移坐标系,将六芒星恢复到原来位置
	glTranslatef(-centerX, -centerY, 0.0f);

	// 绘制六边形的外框
	for (int i = 0; i < 6; i++) {
		ddaLine(vertices[i][0], vertices[i][1], vertices[(i + 1) % 6][0], vertices[(i + 1) % 6][1]);
	}

	// 绘制六芒星的内部对角线
	ddaLine(vertices[0][0], vertices[0][1], vertices[3][0], vertices[3][1]);
	ddaLine(vertices[1][0], vertices[1][1], vertices[4][0], vertices[4][1]);
	ddaLine(vertices[2][0], vertices[2][1], vertices[5][0], vertices[5][1]);

	// 恢复矩阵
	glPopMatrix();
}

// OpenGL初始化设置
void init() {
	glClearColor(1.0, 1.0, 1.0, 1.0); // 背景色为白色
	glColor3f(currentColor[0], currentColor[1], currentColor[2]); // 使用初始颜色(黑色)
	glPointSize(1.0); // 点的大小
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluOrtho2D(0.0, 500.0, 0.0, 500.0); // 设置坐标系
}

// 渲染函数
void display() {
	glClear(GL_COLOR_BUFFER_BIT);

	// 绘制六芒星
	drawHexagram(250, 250, RADIUS);

	glFlush();
}

// 键盘事件处理函数
void keyboard(unsigned char key, int x, int y) {
	if (key == 'r' || key == 'R') {
		// 红色
		currentColor[0] = 1.0f;
		currentColor[1] = 0.0f;
		currentColor[2] = 0.0f;
	}
	else if (key == 'g' || key == 'G') {
		// 绿色
		currentColor[0] = 0.0f;
		currentColor[1] = 1.0f;
		currentColor[2] = 0.0f;
	}
	else if (key == 'b' || key == 'B') {
		// 黄色
		currentColor[0] = 1.0f;
		currentColor[1] = 1.0f;
		currentColor[2] = 0.0f;
	}

	// 更新颜色
	glColor3f(currentColor[0], currentColor[1], currentColor[2]);

	// 重新绘制
	glutPostRedisplay();
}

// 鼠标事件处理函数
void mouse(int button, int state, int x, int y) {
	if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
		// 左键点击时增加旋转角度
		rotationAngle += 15.0f; // 每次点击增加15度

		// 如果角度超过360度,重置为0
		if (rotationAngle >= 360.0f) {
			rotationAngle -= 360.0f;
		}

		// 重新绘制
		glutPostRedisplay();
	}
}

// 主函数
int main(int argc, char **argv) {
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
	glutInitWindowSize(500, 500);
	glutCreateWindow("DDA绘制六芒星");
	init();

	// 注册事件处理函数
	glutKeyboardFunc(keyboard);
	glutMouseFunc(mouse);  // 注册鼠标事件处理函数

	glutDisplayFunc(display);
	glutMainLoop();
	return 0;
}

利用DDA算法绘制六边形,增加键盘交互实现改变图形颜色,增加鼠标交互实现按下鼠标左键图形旋转的功能。

相关推荐
电鱼智能的电小鱼2 小时前
基于电鱼 AI 工控机的智慧工地视频智能分析方案——边缘端AI检测,实现无人值守下的实时安全预警
网络·人工智能·嵌入式硬件·算法·安全·音视频
孫治AllenSun2 小时前
【算法】图相关算法和递归
windows·python·算法
格图素书3 小时前
数学建模算法案例精讲500篇-【数学建模】DBSCAN聚类算法
算法·数据挖掘·聚类
DashVector4 小时前
向量检索服务 DashVector产品计费
数据库·数据仓库·人工智能·算法·向量检索
AI纪元故事会4 小时前
【计算机视觉目标检测算法对比:R-CNN、YOLO与SSD全面解析】
人工智能·算法·目标检测·计算机视觉
夏鹏今天学习了吗4 小时前
【LeetCode热题100(59/100)】分割回文串
算法·leetcode·深度优先
卡提西亚4 小时前
C++笔记-10-循环语句
c++·笔记·算法
还是码字踏实4 小时前
基础数据结构之数组的双指针技巧之对撞指针(两端向中间):三数之和(LeetCode 15 中等题)
数据结构·算法·leetcode·双指针·对撞指针
HahaGiver6666 小时前
Unity与Android原生交互开发入门篇 - 打开Unity游戏的设置
android·unity·交互
Coovally AI模型快速验证7 小时前
当视觉语言模型接收到相互矛盾的信息时,它会相信哪个信号?
人工智能·深度学习·算法·机器学习·目标跟踪·语言模型