#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. Jboss7 部署EJB3 简明教程

    什么是EJB? EJB 是 Java 企业Bean, 是JavaEE服务端 企业组件模型,它的设计目标与核心应用是部署分布式应用程序.话不多说,直接看如何在本机部署EJB3.   部署环境: 操作系统 ...

  2. python的if语法

    在赋值表达式中 a=a+3 if a else 0 这种简洁的语法,似乎必须要跟上else,不能写成 a=a+3 if a ,否则会报错

  3. Zend Studio导致PHP插入数据库中文乱码【坑了个爹】

    用PHP往数据库里面插入数据,在执行INSERT语句前已经执行过 SET NAMES UTF8命令,MySql数据库的编码也确定是UTF8,然而插入中文的结果还是乱码. 找来找去,最后发现原来是用的I ...

  4. DiskGenius注册算法简析

    初次接触DiskGenius已经成为遥远的记忆,那个时候还只有DOS版本.后来到Windows版,用它来处理过几个找回丢失分区的案例,方便实用.到现在它的功能越来越强大,成为喜好启动技术和桌面支持人员 ...

  5. 2019.1.10 L223

    Heavy rains that brought additional pollution downstream last year contributed to the first decline ...

  6. avalonJS-源码阅读(2)

    上一篇文章讲述的avalon刷页面所用到的几个函数. 这篇则是主要讲avalon 对刷DOM刷出来的avalon自定义属性如何处理的. 目录[-] avalon页面处理(2)数据结构解析avalon标 ...

  7. C# 对象不能从 DBNull 转换为其他类型。

    原因是被查询的数据库表的查询项有空(什么都没填),补填0后OK.

  8. caffe测试mnist问题-error while loading shared libraries

    问题描述 build/examples/mnist/convert_mnist_data.bin: error while loading shared libraries: libcudart.so ...

  9. HDU 1872:稳定排序

    稳定排序 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  10. 【转】Python判断字符串是否为字母或者数字

    str_1 = " str_2 = "Abc" str_3 = "123Abc" #用isdigit函数判断是否数字 print(str_1.isdi ...