Lesson 1: Bresenham’s Line Drawing Algorithm
2016-03-16 16:55
393 查看
1.
void line(int x0, int y0, int x1, int y1, TGAImage &image, TGAColor color) { for (float t=0.; t<1.; t+=.01) { int x = x0*(1.-t) + x1*t; int y = y0*(1.-t) + y1*t; image.set(x, y, color); }}2.void line(int x0, int y0, int x1, int y1, TGAImage &image, TGAColor color) {for (int x=x0; x<=x1; x++) {float t = (x-x0)/(float)(x1-x0);int y = y0*(1.-t) + y1*t;image.set(x, y, color);}}
3.
void line(int x0, int y0, int x1, int y1, TGAImage &image, TGAColor color) { bool steep = false; if (std::abs(x0-x1)<std::abs(y0-y1)) { // if the line is steep, we transpose the image std::swap(x0, y0); std::swap(x1, y1); steep = true; } if (x0>x1) { // make it left−to−right std::swap(x0, x1); std::swap(y0, y1); } for (int x=x0; x<=x1; x++) { float t = (x-x0)/(float)(x1-x0); int y = y0*(1.-t) + y1*t; if (steep) { image.set(y, x, color); // if transposed, de−transpose } else { image.set(x, y, color); } } }
4.
void line(int x0, int y0, int x1, int y1, TGAImage &image, TGAColor color) { bool steep = false; if (std::abs(x0-x1)<std::abs(y0-y1)) { std::swap(x0, y0); std::swap(x1, y1); steep = true; } if (x0>x1) { std::swap(x0, x1); std::swap(y0, y1); } int dx = x1-x0; int dy = y1-y0; float derror = std::abs(dy/float(dx)); float error = 0; int y = y0; for (int x=x0; x<=x1; x++) { if (steep) { image.set(y, x, color); } else { image.set(x, y, color); } error += derror; if (error>.5) { y += (y1>y0?1:-1); error -= 1.; } } }
5.
void line(int x0, int y0, int x1, int y1, TGAImage &image, TGAColor color) { bool steep = false; if (std::abs(x0-x1)<std::abs(y0-y1)) { std::swap(x0, y0); std::swap(x1, y1); steep = true; } if (x0>x1) { std::swap(x0, x1); std::swap(y0, y1); } int dx = x1-x0; int dy = y1-y0; int derror2 = std::abs(dy)*2; int error2 = 0; int y = y0; for (int x=x0; x<=x1; x++) { if (steep) { image.set(y, x, color); } else { image.set(x, y, color); } error2 += derror2; if (error2 > dx) { y += (y1>y0?1:-1); error2 -= dx*2; } } }
相关文章推荐
- Go语言核心之美 2.2-浮点数
- 让DJANGO里的get_success_url定义的reverse_lazy带参数跳转
- 蓝桥杯 ALGO-51(素数,打表)
- 将DJANGO管理界面的filter_horizontal移到前面来复用
- go语言资料集合
- Django--缓存
- django基本命令
- 读《Introducing Go》O'Reilly 第 5 章
- Django中的cookie与session
- la 4945 free goodies,不会!
- Django View基础
- Django~Excel,PDF
- Go语言核心之美 2.1-整数
- hdu 4775 Infinite Go(暴力)
- Golang 中的格式化输入输出
- Django--中间件
- [Canvas绘图] 第35节 藏图阁(15) AlphaGo与李世石的围棋大战之第五局
- django-debug-tool配置
- 机器学习系列(8)_读《Nature》论文,看AlphaGo养成