# 线画图元生成算法
**实验目的**
掌握 DDA 算法和 Bresenham 直线算法
**实验内容**
自定义直线段起始点和终点坐标;
采用不同的彩色显示两种算法生成的直线结果;
为突出显示效果,采用网格表示像素。
**实验结果**
**实验环境**
OS:windows10
IDE:VS2019
使用库:openGL + glfw3
DDA 算法
鉴于 openGL 不能自动生成网格,我自己写了一段代码来生成网格。定义直接在屏幕上输出的坐标为实坐标,在网格上输出的坐标为虚拟坐标。
定义网格的粒度为 PIXLE = 10;
![](https://www.writebug.com/myres/static/uploads/2021/11/16/1a192a8ce7db5957a2bc98b65b9213f3.writebug)
定义:TO_VIRTUAL_PIXEL 转到虚拟像素
TO_REAL_PIXEL 转到实际像素
![](https://www.writebug.com/myres/static/uploads/2021/11/16/1a192a8ce7db5957a2bc98b65b9213f3.writebug)
结果如图
![](https://www.writebug.com/myres/static/uploads/2021/11/16/73843b0478330359a895b63c15f3b6b5.writebug)
- Bresenham 算法
- Bersenham 的一般算法针对 x 正向增大且斜率 0<k<1.关键在于拓展此算法使其对于任意的线段都有效。现在仅对八分之一坐标有效
- 当 k > 1 时,注意到仅将 x 和 y 的位置对调即可,区域拓展为一个象限
- 当 x 向负向增长的时候,把原式里的 x + 1 改为 x – 1 即可。区域拓展为两个现象
- 对传入的参数进行控制,使得起点的 y 值总是小于终点的 y 值(如果不满足,就交换参数传入的顺序),区域拓展为四个象限
![](https://www.writebug.com/myres/static/uploads/2021/11/16/a57ceab51a01ee180f9ac8e8b84e83a5.writebug)
![](https://www.writebug.com/myres/static/uploads/2021/11/16/4420bae8cfd97674053c0e6f56fea147.writebug)
实验分析和总结
本次实验重点学习了直线生成算法。此算法在原理上相对简单,关键在于对算法的优化.DDA 算法把原先的乘法计算转变为浮点数加法。Bersenham 算法又在此基础上把浮点数加法改为整数加法,进一步提高了效率。
源代码
```c++
# include<iostream>
# include<math.h>
# include<GLFW/glfw3.h>
# include<gl/GL.h>
# define ROUND(x) ((int)(x + 0.5))
# define PIXEL 10
# define TO_VIRTUAL_PIXEL(real) (real/PIXEL)
# define TO_REAL_PIXEL(x) (x*PIXEL)
//#define DDA
# define BERSENHAM
void myDDA_template(int x0, int y0, int x1, int y1);
void Bersenham_template(int x0, int y0, int x1, int y1);
void Bersenham_template_1(int dx, int dy, int x, int y, int degree);
void draw_pixel(int x, int y) {
printf("Draw_Pixel(%d , %d)\n", x, y);
int real_x = TO_REAL_PIXEL(x);
int real_y = TO_REAL_PIXEL(y);
glColor3f(1.0, 0.0, 0.0);
glPointSize(1);
glBegin(GL_POINTS);
for (int i = 0; i < PIXEL; i++) {
for (int j = 0; j < PIXEL; j++) {
glVertex2i(real_x + i, real_y + j);
}
}
glEnd();
}
void draw_grid(int width, int heigh) {
glColor3f(0.0, 1.0, 0.0);
glPointSize(1);
glBegin(GL_POINTS);
for (int x = 0; x < width; x = x + PIXEL) {
for (int y = 0; y < heigh; y++) {
glVertex2i(x, y);
}
}
for (int y = 0; y < heigh; y = y + PIXEL) {
for (int x = 0; x < width; x++) {
glVertex2i(x, y);
}
}
glEnd();
}
void myDDA(int x0, int y0, int x1, int y1) {
myDDA_template(TO_VIRTUAL_PIXEL(x0), TO_VIRTUAL_PIXEL(y0), TO_VIRTUAL_PIXEL(x1), TO_VIRTUAL_PIXEL(y1));
}
void myDDA_template(int x0, int y0, int x1, int y1) {
//这里都使用虚拟坐标进行计算
//printf("%d %d %d %d \n", x0, y0, x1, y1);
draw_pixel(x0, y0);
float x = x0, y = y0;
float dx = x1 - x0;
float dy = y1 - y0;
int steps = (int)(abs(dx) > abs(dy)) ? abs(dx) : abs(dy);
float xinc = dx / steps;
float yinc = dy / steps;
for (int i = 0; i < steps; i++) {
+= xinc;
+= yinc;
//printf("draw pixel (%d,%d) \n", ROUND(x), ROUND(y));
draw_pixel(ROUND(x), ROUND(y));
}
}
void Bersenham(int x0, int y0, int x1, int y1) {
if (y1 > y0) {
Bersenham_template(TO_VIRTUAL_PIXEL(x0), TO_VIRTUAL_PIXEL(y0), TO_VIRTUAL_PIXEL(x1), TO_VIRTUAL_PIXEL(y1));
}
else {
Bersenham_template(TO_VIRTUAL_PIXEL(x1), TO_VIRTUAL_PIXEL(y1), TO_VIRTUAL_PIXEL(x0), TO_VIRTUAL_PIXEL(y0));
}
}
void Bersenham_template(int x0, int y0, int x1, int y1) {
printf("Bersenham args (%d , %d , %d , %d)\n", x0, y0, x1, y1);
draw_pixel(x0, y0);
int x = x0;
int y = y0;
int dx = abs(x1 - x0);
int dy = abs(y1 - y0);
int p0;
if(dy > dx) {
Bersenham_template_1(dy, dx, x0, y0, (x1 > x0));
}
else {
Bersenham_template_1(dx, dy, x0, y0, (x1 > x0));
}
}
// for 0<k <1
// degree 分为正向和反向
void Bersenham_template_1(int dx, int dy, int x, int y, int degree) {
int p0;
= 2 * dy - dx;
if (degree) {
for (int i = 0; i < dx; i++) {
if (p0 < 0) {
draw_pixel(x + 1 + i, y);
= p0 + 2 * dy;
}
else {
y++;
draw_pixel(x + 1 + i, y);
= p0 + 2 * dy - 2 * dx;
}
}
}
else {
for (int i = 0; i < dx; i++) {
if (p0 < 0) {
draw_pixel(x - 1 - i, y);
= p0 + 2 * dy;
}
else {
y++;
draw_pixel(x - 1 - i, y);
= p0 + 2 * dy - 2 * dx;
}
}
}
}
void Init(int width, int height) {
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, width, 0.0, height, 0.0, 1.0);
}
int main() {
int width, height;
width = 1440;
height = 960;
GLFWwindow* window;
if (!glfwInit()) {
return -1;
}
window = glfwCreateWindow(width, height, "Computer Graphics", NULL, NULL);
if (!window) {
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glfwSwapInterval(1);
Init(width, height);
int printed = 1;
while (!glfwWindowShouldClose(window)) {
//glClear(GL_COLOR_BUFFER_BIT);
if (printed) {
//Draw here
# ifdef DDA
myDDA(100, 100, 600, 300);
myDDA((float)100, (float)100, (float)200, (float)0);
myDDA((float)100, (float)100, (float)105, (float)200);
myDDA((float)100, (float)100, (float)70, (float)200);
myDDA((float)100, (float)100, (float)0, (float)100);
myDDA((float)100, (float)100, (float)200, (float)100);
# endif
# ifdef BERSENHAM
//Bersenham(0, 0, 500, 100);
Bersenham(0, 0, 100, 500);
Bersenham(0, 0, 0, 1000);
Bersenham(1000, 500, 0, 0);
Bersenham(1000, 200, 200, 800);
# endif
}
draw_grid(width, height);
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}
```
没有合适的资源?快使用搜索试试~ 我知道了~
基于C语言实现线画图元生成算法【100012533】
共661个文件
html:193个
c:105个
js:67个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 168 浏览量
2023-05-31
14:13:12
上传
评论
收藏 18.94MB ZIP 举报
温馨提示
本项目需要掌握 DDA 算法和 Bresenham 直线算法,了解自定义直线段起始点和终点坐标;采用不同的彩色显示两种算法生成的直线结果;为突出显示效果,采用网格表示像素。
资源推荐
资源详情
资源评论
收起资源包目录
基于C语言实现线画图元生成算法【100012533】 (661个子文件)
libglfw3.a 304KB
libglfw3.a 304KB
libglfw3dll.a 83KB
libglfw3dll.a 83KB
README.android 57B
AUTHORS 1KB
configurations.autopkg 638B
configurations.autopkg 638B
README.blackberry 1KB
fg_font_data.c 383KB
fg_geometry.c 79KB
fg_main_mswin.c 62KB
fg_stroke_mono_roman.c 47KB
fg_stroke_roman.c 47KB
fg_main_x11.c 40KB
fg_main_blackberry.c 37KB
fg_menu.c 36KB
shapes.c 35KB
fg_window_mswin.c 28KB
fg_joystick.c 27KB
fg_teapot.c 24KB
fg_init.c 24KB
CallbackMaker.c 22KB
fg_main_android.c 21KB
fg_joystick_x11.c 21KB
fg_structure.c 20KB
fg_window_x11.c 19KB
fg_gamemode_x11.c 19KB
smooth_opengl3.c 16KB
android_native_app_glue.c 16KB
fg_window.c 15KB
fg_input_devices_wl.c 15KB
fg_main.c 15KB
fg_font.c 13KB
fg_state_mswin.c 13KB
fg_callbacks.c 12KB
fg_state.c 11KB
lorenz.c 11KB
fg_window_wl.c 11KB
test-shapes-gles1.c 11KB
one.c 10KB
fg_window_blackberry.c 10KB
fractals_random.c 10KB
fg_spaceball_x11.c 10KB
fg_ext.c 10KB
fg_xinput_x11.c 9KB
fg_window_x11_glx.c 9KB
fg_init_x11.c 9KB
fractals.c 9KB
fg_joystick_mswin.c 8KB
fg_state_x11.c 8KB
fg_cursor_x11.c 7KB
fg_window_egl.c 7KB
fg_state_x11_glx.c 7KB
fg_input_devices.c 7KB
fg_gamemode_mswin.c 7KB
fg_runtime_android.c 6KB
fg_cursor_mswin.c 6KB
fg_spaceball_mswin.c 6KB
fg_init_mswin.c 6KB
fg_window_android.c 6KB
fg_misc.c 6KB
glmatrix.c 6KB
fg_gamemode.c 5KB
fg_gamemode_wl.c 5KB
fg_state_blackberry.c 5KB
fg_cursor_wl.c 5KB
subwin.c 5KB
fg_state_egl.c 5KB
xparsegeometry_repl.c 5KB
multi-touch.c 5KB
fg_init_wl.c 5KB
timer.c 4KB
fg_state_wl.c 4KB
fg_main_wl.c 4KB
fg_init_blackberry.c 4KB
fg_menu_mswin.c 4KB
fg_glutfont_definitions_x11.c 4KB
fg_input_devices_mswin.c 4KB
spaceball.c 3KB
fg_display.c 3KB
fg_input_devices_x11.c 3KB
timer.c 3KB
fg_gl2.c 3KB
fg_state_android.c 3KB
fg_init_egl.c 3KB
fg_cursor.c 3KB
fg_input_devices_android.c 2KB
fg_ext_x11.c 2KB
fg_overlay.c 2KB
gles_stubs.c 2KB
fg_ext_mswin.c 2KB
fg_spaceball.c 2KB
fg_joystick_android.c 2KB
fg_videoresize.c 2KB
fg_ext_android.c 2KB
fg_ext_wl.c 2KB
fg_menu_x11.c 2KB
fg_gamemode_android.c 2KB
fg_structure_x11.c 2KB
共 661 条
- 1
- 2
- 3
- 4
- 5
- 6
- 7
资源评论
神仙别闹
- 粉丝: 2687
- 资源: 7642
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 88-520告白(520气球).zip
- HTML+CSS+JS精品网页模板H126.rar
- n.cpp
- jdk-8u411-windows-x64下载安装可用
- vgg模型-图像分类算法对水果识别-不含数据集图片-含逐行注释和说明文档.zip
- KMP算法(Knuth-Morris-Pratt算法
- vgg模型-python语言pytorch框架训练识别化妆品分类-不含数据集图片-含逐行注释和说明文档.zip
- KMP算法(Knuth-Morris-Pratt算法
- shufflenet模型-基于人工智能的卷积网络训练识别狗的表情-不含数据集图片-含逐行注释和说明文档.zip
- EPIC Fantasy Town 1.0
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功