#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. flask(十)使用alembic,进行数据库结构管理,升级,加表,加项

    1.安装扩展,在虚拟环境中安装 alembic,不懂可以去看pycharm的系列文章. 2.初始化, 使用 Alembic 前需要通过 alembic init 命令创建一个 alembic 项目,该 ...

  2. 团队作业之现场UML设计

    项目UML设计(团队) 团队信息 团队名:第三视角 各成员学号及姓名 姓名 学号 博客链接 张扬(组长) 031602345 http://www.cnblogs.com/sxZhangYang/p/ ...

  3. 超级好用的C++万能头文件

    #include<bits/stdc++.h>包含了目前c++所包含的所有头文件 对比: #include <iostream> #include <cstdio> ...

  4. vmware Esxi 回收Thin模式磁盘空间

    本文来自:https://blog.csdn.net/happyfreeangel/article/details/85075179 第一部分 回收Thin模式磁盘空间背景:在使用Thin模式的磁盘, ...

  5. Swift中WebView的应用

    WebView控件是做网络应用开发中使用最多的控件,直接在WebView内部指定一个网页地址就可以访问网页了,同时也可以实现UIWebViewDelegate协议实现相应的方法去控制内容的加载和处理. ...

  6. postman--安装及Interceptor插件

        1. 官网安装(看网速-我下载的时候一直下载失败) 打开官网,https://www.getpostman.com 选择ios或者win 2. 非官网安装 https://pan.baidu. ...

  7. 解决python3读写中文txt时UnicodeDecodeError : 'ascii' codec can't decode byte 0xc4 in position 5595: ordinal not in range(128) on line 0的问题

    今天使用python3读写含有中文的txt时突然报了如下错误,系统是MAC OS,iDE是pycharm: UnicodeDecodeError : 'ascii' codec can't decod ...

  8. Ajax请求数据的两种方式

    ajax 请求数据的两种方法,有需要的朋友可以参考下. 实现ajax 异步访问网络的方法有两个.第一个是原始的方法,第二个是利用jquery包的 原始的方法不用引入jquery包,只需在html中编写 ...

  9. spark中saveAsTextFile如何最终生成一个文件

    原文地址: http://www.cnblogs.com/029zz010buct/p/4685173.html 一般而言,saveAsTextFile会按照执行task的多少生成多少个文件,比如pa ...

  10. How to return NULL string

    Q: std::string get_file_contents(const char *filename) { std::ifstream in(filename, std::ios::in | s ...