#include<stdio.h>

 #include<stdlib.h>

 #include"graphics.h"

 //函数声明

 void MidBrehansem(int x0, int y0, int x1, int y1);  //中点MidBrehansem算法

 void swap(int &x, int &y);  //利用引用传递交换值

 int main()

 {

     int gdriver = DETECT, gmode;

     int x0, y0, x1, y1;

     printf("Please input start point:\n");

     scanf("%d%d", &x0, &y0);

     printf("Please input end point:\n");

     scanf("%d%d", &x1, &y1);

     initgraph(&gdriver, &gmode, "");

     MidBrehansem(x0, y0, x1, y1);

     system("pause");

     closegraph();

     return ;

 }

 //中点MidBrehansem算法

 void MidBrehansem(int x0, int y0, int x1, int y1)

 {

     int  dx, dy,i,di;

     double k;

     if (x0 > x1)

     {

         swap(x0, x1);

         swap(y0, y1);

     }

     int x = x0, y = y0;     //定义到前面会从x1,y1开始画线

     dx = x1 - x0;

     dy = y1 - y0;

     k = 1.0*dy / dx;

     i = (abs(dy)>abs(dx)) ? abs(dy): abs(dx);

     if (k >= )

     {

         di =  * dx + dy;

         while (i--)

         {

             putpixel(x, y, RED);

             if (di >= )

             {

                 di +=  * (dx - dy);

                 x++;

             }

             else

                 di +=  * dx;

             y++;

         }

     }

     else if (k >=  && k < )

     {

         di = dx -  * dy;

         while (i--)

         {

             putpixel(x, y, YELLOW);

             if (di < )

             {

                 di +=  * (dx - dy);

                 y++;

             }

             else

                 di += (-)*dy;

             x++;

         }

     }

     else if (k >= - && k < )

     {

         di = -dx -  * dy;

         while (i--)

         {

             putpixel(x, y, YELLOW);

             if (di >= )

             {

                 di += (-)*(dx + dy);

                 y--;

             }

             else

                 di += (-)*dy;

             x++;

         }

     }

     else

     {

         di = - * (dx + dy);

         while (i--)

         {

             putpixel(x, y, WHITE);

             if (di < )

             {

                 di += (-)*(dx + dy);

                 x++;

             }

             else

                 di += (-)*dx;

             y--;

         }

     }

 }

 //利用引用传递交换值

 void swap(int &x, int &y)

 {

     int t;

     t = x;

     x = y;

     y = t;

 }

中点Brehensam画线算法的更多相关文章

  1. 中点Brehensam画圆算法

    #include<stdio.h> #include<stdlib.h> #include<graphics.h> #include<math.h> v ...

  2. 两种画线算法(DDA&Bersenham)

    DDA(digital differential analyzer) 由直线的斜截式方程引入 对于正斜率的线段,如果斜率<=1,则以单位x间隔(δx=1)取样,并逐个计算每一个y值 Yk+1 = ...

  3. Bresenham画线算法

    [Bresenham画线算法] Bresenham是一种光栅化算法.不仅可以用于画线,也可以用用画圆及其它曲线. 通过lower与upper的差,可以知道哪一个点更接近线段: 参考:<计算机图形 ...

  4. Bezier画线算法

    编译器:VS2013 描述:Bezier画线是利用导数相同拼接曲线,使曲线十分光滑,而不是随意拼接观赏性很差 主函数段 #include "stdafx.h" #include&l ...

  5. 计算机图形学(二)输出图元_3_画线算法_2_DDA算法

    DDA算法        数字微分分析仪(digital differential analyzer, DDA)方法是一种线段扫描转换算法.基于使用等式(3.4)或等式(3.5)计算的&x或& ...

  6. DDA画线算法

    #include<stdio.h> #include"graphics.h" #include<math.h> #include<stdlib.h&g ...

  7. Bresenham直线算法与画圆算法

    在我们内部开发使用的一个工具中,我们需要几乎从 0 开始实现一个高效的二维图像渲染引擎.比较幸运的是,我们只需要画直线.圆以及矩形,其中比较复杂的是画直线和圆.画直线和圆已经有非常多的成熟的算法了,我 ...

  8. 计算机图形学DDA画线法+中点画线法+Bresenham画线法

    #include <cstdio> #include <cstring> #include <conio.h> #include <graphics.h> ...

  9. 画线函数Glib_Line算法的研究

      在这里首先先简单把我对函数的功能的理解阐述一下,方便后面的分析:Glib_Line函数实现的功能是通过参数给定(x1,y1,x2,y2,color),来确定起点(x1,y1)和终点(x2,y2)两 ...

随机推荐

  1. LeetCode OJ:Insert Interval

    Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...

  2. vue-router配置之通用路径

    目的:浏览器地址栏输入了未匹配的地址时,前端控制跳转到首页index页面: 项目入口js:(一般根据项目结构不同,这个js一般取名为:app.js或者main.js) import Router fr ...

  3. 目录遍历漏洞简介 转载于h3c

    参考文章: 攻防:目录遍历漏洞简介

  4. kafka知识点整理总结

    kafka知识点整理总结 只不过是敷衍 2017-11-22 21:39:59 kafka知识点整理总结,以备不时之需. 为什么要使用消息系统: 解耦 并行 异步通信:想向队列中放入多少消息就放多少, ...

  5. buy now按钮的添加

    样例是 www.dealfreeship.com:在D:\xampp\htdocs\aliexpress\app\design\frontend\default\se101\template\cata ...

  6. MyEclipse快捷键及经验总结 绝对的有用 太给力了

    Ctrl+Shift+L    显示所有快捷键  Ctrl+K      参照选中的词(Word)快速定位到下一个  Ctrl+Shift+K    参照选中的词(Word)快速定位到上一个    C ...

  7. Http常见状态码说明

    一些常见的状态码为: 200 - 服务器成功返回网页404 - 请求的网页不存在503 - 服务不可用 详细分解: 1xx(临时响应) 表示临时响应并需要请求者继续执行操作的状态代码.代码 说明100 ...

  8. hdu2067 小兔的棋盘 DP/数学/卡特兰数

    棋盘的一角走到另一角并且不越过对角线,卡特兰数,数据量小,可以当做dp求路径数 #include<stdio.h> ][]; int main() { ; ) { int i,j; lon ...

  9. window.open()与window.showModalDialog

    弹出窗口两种方式:    1.window.showModalDialog:      var feature = "dialogWidth:615px;dialogHeight:505px ...

  10. C#读取Mysql blob字段 (转帖)

    http://blog.csdn.net/config_man/article/details/6123191 开发环境:Windows XP Professional SP3.VS2008.Winf ...