DDABresenham
2024-05-13 13:15:47
扫描转换
#include <bits/stdc++.h>
#include <GL/glut.h>
using namespace std;const int MAXN = 1000000; //10w条线段
const int WIDTH = 400;
const int HEIGHT = 300;
uniform_real_distribution<double> u(0, 1);
default_random_engine e;vector<int> v;
void myinit(void){glClearColor(0, 0, 0, 0); //置背景色glMatrixMode(GL_PROJECTION);//设矩阵模式gluOrtho2D(0.0, WIDTH, 0.0, HEIGHT); //正交投影窗口
}int main(int argc, char* argv[]){uniform_int_distribution<int> u1(0, 400);uniform_int_distribution<int> u2(0, 300);int n = MAXN * 2;for(int i = 0; i < n; i++){v.push_back(u1(e));v.push_back(u2(e));}//int id = 0;//for(int i = 0; i < n; i++) {// cout<<v[id++]<<" "<<v[id++]<<endl;//}glutInit(&argc, argv);glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);glutInitWindowSize(800,600);glutInitWindowPosition(0,0);glutCreateWindow("line algorithm");//glutDisplayFunc(Bresenham);glutDisplayFunc(DDA);myinit();glutMainLoop();
}
DDA
void DDA(){int st = clock();int id = 0;glClear(GL_COLOR_BUFFER_BIT); //清除glPointSize(1.0f);for(int i = 0; i < MAXN; i++){glColor3f(u(e),u(e),u(e)); //绘制颜色double x0 = v[id++], y0 = v[id++];double x1 = v[id++], y1 = v[id++];double dx = x1 - x0, dy = y1 - y0;double step = max(fabs(dx), fabs(dy));double ax = dx / step;double ay = dy / step;glBegin(GL_POINTS);for(int j = 0; j < step; j++){glVertex2i(floor(x0 + 0.5), floor(y0 + 0.5));x0 += ax;y0 += ay;}glEnd();}glutSwapBuffers();int ed = clock();cout<<"DDA: "<<ed - st<<"\n";
}
Bresenham
void Bresenham(void){int st = clock();int id = 0;glClear(GL_COLOR_BUFFER_BIT);glPointSize(1.0f);for(int i = 0; i < MAXN; i++){glColor3f(u(e),u(e),u(e)); //绘制颜色int x0 = v[id++], y0 = v[id++], x1 = v[id++], y1 = v[id++];if(x0 > x1 ){swap(x0, x1);swap(y0, y1);}int x = x1 - x0, y = y1 - y0;int step = max(abs(x), abs(y));int x2 = x * 2, y2 = y * 2;glBegin(GL_POINTS);if(y > 0){//m is the slope of the line//cout<<"m > 0 ";if(abs(x) > abs(y)){//cout<<"m < 1\n";int p0 = y2 - x;for(; x0 <= x1; x0++){glVertex2i(x0, y0);if(p0 < 0) {p0 = p0 + y2;}else{p0 = p0 + y2 - x2;y0++;}}}else {//cout<<"m > 1\n";swap(x0, y0);swap(x1, y1);swap(x2, y2);swap(x, y);int p0 = y2 - x;for(; x0 <= x1; x0++){glVertex2i(y0, x0);if(p0 < 0) {p0 = p0 + y2;}else{p0 = p0 + y2 - x2;y0++;}}}}else{//cout<<"m < 0 ";if(abs(x) > abs(y)){//cout<<"m > -1\n";int p0 = y2 + x;for(; x0 <= x1; x0++){glVertex2i(x0, y0);if(p0 > 0) {p0 = p0 + y2;}else{p0 = p0 + y2 + x2;y0--;}}}else {//cout<<"m < -1\n";swap(x0, y0);swap(x1, y1);swap(x2, y2);swap(x, y);int p0 = y2 + x;for(; x0 <= x1; x0++){glVertex2i(y0, x0);if(p0 > 0) {p0 = p0 + y2;}else{p0 = p0 + y2 + x2;y0--;}}}}glEnd();}glutSwapBuffers();int ed = clock();cout<<"Bresenham: "<<ed - st<<"\n";
}
DDABresenham相关推荐
最新文章
- Python 数据类型:列表
- 单选项选择时,即时更新至数据库
- 学习速率 learning rate
- 《技术的本质》2月24日part1
- 如何使用Data Lake Analytics创建分区表
- arch模型的思路_ARCH模型
- karaf内嵌文件服务器,关于OSGI(Karaf) Classloader的几点说明
- python 爬关键词,Python爬虫实战:爬取微博热搜关键词
- 【英语学习】【WOTD】tenacious 释义/词源/示例
- 兆芯笔试题(2015)找反复数最多的字母的数量以及位置
- VS2012下载和安装
- 像程序员一样思考——解决问题
- 迅雷 Thunder 11.0.2.52 抢先体验版
- (二十五)各向同性张量(函数)
- cad引出线段lisp_利用lisp给CAD直线取整?
- oracle智能便携投影机,智能投影 篇四:天猫精灵投影仪上手体验,小巧便携+内置电池,李佳琦带货推荐...
- OLA音频变速算法的仿真与剖析
- 施工部署主要不包括_施工部署包括哪些?
- Volley 登录注册
- 一遍两遍三四遍,知识含义清晰现