扫描转换

#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相关推荐

最新文章

  1. Python 数据类型:列表
  2. 单选项选择时,即时更新至数据库
  3. 学习速率 learning rate
  4. 《技术的本质》2月24日part1
  5. 如何使用Data Lake Analytics创建分区表
  6. arch模型的思路_ARCH模型
  7. karaf内嵌文件服务器,关于OSGI(Karaf) Classloader的几点说明
  8. python 爬关键词,Python爬虫实战:爬取微博热搜关键词
  9. 【英语学习】【WOTD】tenacious 释义/词源/示例
  10. 兆芯笔试题(2015)找反复数最多的字母的数量以及位置
  11. VS2012下载和安装
  12. 像程序员一样思考——解决问题
  13. 迅雷 Thunder 11.0.2.52 抢先体验版
  14. (二十五)各向同性张量(函数)
  15. cad引出线段lisp_利用lisp给CAD直线取整?
  16. oracle智能便携投影机,智能投影 篇四:天猫精灵投影仪上手体验,小巧便携+内置电池,李佳琦带货推荐...
  17. OLA音频变速算法的仿真与剖析
  18. 施工部署主要不包括_施工部署包括哪些?
  19. Volley 登录注册
  20. 一遍两遍三四遍,知识含义清晰现

热门文章

  1. mysql emojo 其他方案_MySQL数据库存储昵称带有emoji处理方案
  2. oppor9splus科学计算机,oppor9splus参数
  3. iOS的rtmp推流Demo
  4. AirServer手机投屏软件的功能优势特点介绍
  5. 洛谷 P4269 / loj 2041 [SHOI2015] 聚变反应炉 题解【贪心】【DP】
  6. 学习笔记:隋唐人的行
  7. ADS6445开发笔记(1)---- 芯片介绍
  8. python编程实例:华氏度和摄氏度转化器
  9. 弘辽科技:宝贝自然排名提升,轻松上淘宝首页!
  10. 微课程在线教育付费直播平台系统