完善实验一
This commit is contained in:
+61
-61
@@ -1,80 +1,80 @@
|
||||
#include <cmath>
|
||||
#include <iostream>
|
||||
#include <GL/glut.h>
|
||||
#include <cmath> // 引入数学函数库,用于 abs 和 round 等函数。
|
||||
#include <iostream> // 引入输入输出流库,用于打印坐标。
|
||||
#include <GL/glut.h> // 引入 GLUT/OpenGL 头文件,提供窗口和绘图函数。
|
||||
|
||||
// 窗口宽度和高度
|
||||
const int WIDTH = 640;
|
||||
const int HEIGHT = 480;
|
||||
// 窗口宽度和高度。
|
||||
const int WIDTH = 640; // 定义窗口宽度为 640 像素。
|
||||
const int HEIGHT = 480; // 定义窗口高度为 480 像素。
|
||||
|
||||
void drawDDALine(int x1, int y1, int x2, int y2) {
|
||||
float x = x1;
|
||||
float y = y1;
|
||||
void drawDDALine(int x1, int y1, int x2, int y2) { // 定义 DDA 直线绘制函数,参数为线段两个端点。
|
||||
float x = x1; // 用浮点变量保存当前点的 x 坐标。
|
||||
float y = y1; // 用浮点变量保存当前点的 y 坐标。
|
||||
|
||||
// 计算差值
|
||||
int dx = x2 - x1;
|
||||
int dy = y2 - y1;
|
||||
// 计算差值。
|
||||
int dx = x2 - x1; // 计算终点与起点的 x 方向差值。
|
||||
int dy = y2 - y1; // 计算终点与起点的 y 方向差值。
|
||||
|
||||
// 确定步数,取 dx 和 dy 中绝对值较大的那个
|
||||
int steps = std::abs(dx) > std::abs(dy) ? std::abs(dx) : std::abs(dy); //三元表达式
|
||||
// 确定步数,取 dx 和 dy 中绝对值较大的那个。
|
||||
int steps = std::abs(dx) > std::abs(dy) ? std::abs(dx) : std::abs(dy); // 三元表达式用于选择较大的绝对差值作为步数。
|
||||
|
||||
// 计算每一步的增量
|
||||
float xIncrement = (float)dx / steps;
|
||||
float yIncrement = (float)dy / steps;
|
||||
// 计算每一步的增量。
|
||||
float xIncrement = (float)dx / steps; // 计算每一步 x 坐标增加的值。
|
||||
float yIncrement = (float)dy / steps; // 计算每一步 y 坐标增加的值。
|
||||
|
||||
// 开始绘制点
|
||||
glBegin(GL_POINTS);
|
||||
glVertex2i((int)round(x), (int)round(y)); // 绘制起点
|
||||
// 开始绘制点。
|
||||
glBegin(GL_POINTS); // 开始按点方式提交顶点。
|
||||
glVertex2i((int)round(x), (int)round(y)); // 绘制起点,并将浮点坐标四舍五入为整数坐标。
|
||||
|
||||
for (int k = 0; k < steps; k++) {
|
||||
x += xIncrement;
|
||||
y += yIncrement;
|
||||
// 将浮点坐标四舍五入取整转换为整数像素坐标
|
||||
std::cout << (int)round(x) << ", " << (int)round(y)<<"\n";
|
||||
glVertex2i((int)round(x), (int)round(y));
|
||||
}
|
||||
glEnd();
|
||||
for (int k = 0; k < steps; k++) { // 循环 steps 次,逐步生成直线上的像素点。
|
||||
x += xIncrement; // 更新当前点的 x 坐标。
|
||||
y += yIncrement; // 更新当前点的 y 坐标。
|
||||
// 将浮点坐标四舍五入取整转换为整数像素坐标。
|
||||
std::cout << (int)round(x) << ", " << (int)round(y)<<"\n"; // 在控制台输出当前绘制点的整数坐标。
|
||||
glVertex2i((int)round(x), (int)round(y)); // 将当前点作为 OpenGL 点顶点提交。
|
||||
}
|
||||
glEnd(); // 结束点顶点提交。
|
||||
}
|
||||
|
||||
// 显示回调函数
|
||||
void display() {
|
||||
drawDDALine(0, 0, 50, 20);
|
||||
glFlush();
|
||||
}
|
||||
// 显示回调函数。
|
||||
void display() { // 定义窗口重绘时调用的显示函数。
|
||||
drawDDALine(0, 0, 50, 20); // 调用 DDA 算法绘制从(0,0)到(50,20)的直线。
|
||||
glFlush(); // 强制执行所有尚未执行的 OpenGL 绘图命令。
|
||||
}
|
||||
|
||||
// 初始化 OpenGL 设置
|
||||
// 初始化 OpenGL 设置。
|
||||
void init() {
|
||||
// 设置背景颜色为白色
|
||||
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
|
||||
// 设置背景颜色为白色。
|
||||
glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // 设置清屏颜色为白色且完全不透明。
|
||||
// 空行:分隔背景设置和投影设置。
|
||||
// 设置投影矩阵为 2D 正交投影。
|
||||
glMatrixMode(GL_PROJECTION); // 切换到投影矩阵模式。
|
||||
glLoadIdentity(); // 将当前投影矩阵重置为单位矩阵。
|
||||
// 定义可视区域,左下角(0,0),右上角(WIDTH, HEIGHT)。
|
||||
gluOrtho2D(0.0, WIDTH, 0.0, HEIGHT); // 设置二维正交投影坐标范围。
|
||||
}
|
||||
|
||||
// 设置投影矩阵为 2D 正交投影
|
||||
glMatrixMode(GL_PROJECTION);
|
||||
glLoadIdentity();
|
||||
// 定义可视区域,左下角(0,0),右上角(WIDTH, HEIGHT)
|
||||
gluOrtho2D(0.0, WIDTH, 0.0, HEIGHT);
|
||||
}
|
||||
int main(int argc, char** argv) { // 程序入口函数,接收命令行参数。
|
||||
// 初始化 GLUT。
|
||||
glutInit(&argc, argv); // 初始化 GLUT 库。
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
// 初始化 GLUT
|
||||
glutInit(&argc, argv);
|
||||
// 设置显示模式:单缓冲、RGB 颜色模式。
|
||||
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); // 设置单缓冲和 RGB 颜色模式。
|
||||
|
||||
// 设置显示模式:单缓冲、RGB 颜色模式
|
||||
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
|
||||
// 设置窗口大小和位置。
|
||||
glutInitWindowSize(WIDTH, HEIGHT); // 设置窗口大小为 WIDTH x HEIGHT。
|
||||
glutInitWindowPosition(100, 100); // 设置窗口初始显示位置。
|
||||
|
||||
// 设置窗口大小和位置
|
||||
glutInitWindowSize(WIDTH, HEIGHT);
|
||||
glutInitWindowPosition(100, 100);
|
||||
// 创建窗口。
|
||||
glutCreateWindow("DDA算法"); // 创建标题为“DDA算法”的窗口。
|
||||
|
||||
// 创建窗口
|
||||
glutCreateWindow("DDA算法");
|
||||
// 注册回调函数。
|
||||
glutDisplayFunc(display); // 注册显示回调函数。
|
||||
|
||||
// 注册回调函数
|
||||
glutDisplayFunc(display);
|
||||
// 初始化设置。
|
||||
init(); // 执行 OpenGL 初始化设置。
|
||||
|
||||
// 初始化设置
|
||||
init();
|
||||
// 进入主循环。
|
||||
glutMainLoop(); // 进入 GLUT 事件循环,等待并处理窗口事件。
|
||||
|
||||
// 进入主循环
|
||||
glutMainLoop();
|
||||
|
||||
return 0;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user