直线DDA,直线和圆的Bresenham算法

 

// DDA.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<Windows.h>
#include<graphics.h>
#include<conio.h>
#include<math.h>
void dda_line(int xa, int ya, int xb, int yb, int c);
int main(int argc, _TCHAR* argv[])
{
    int gd=DETECT,gm; /*图形屏幕初始化*/
    initgraph(&gd,&gm,"");
    dda_line(100,100,200,200,255);
    getch();
    closegraph();
    return 0;
}


void dda_line(int xa, int ya, int xb, int yb, int c)
{
float delta_x, delta_y, x, y;
int dx, dy, steps, k;
dx=xb-xa;
dy=yb-ya;
if(abs(dx)>abs(dy))steps=abs(dx);
else steps=abs(dy);
delta_x=(float)dx/(float)steps;
delta_y=(float)dy/(float)steps;
x=xa;
y=ya;
putpixel(x, y, c);
for(k=1;k<=steps;k++)
{
x+=delta_x;
y+=delta_y;
putpixel(x, y, c);
}
}

#include "stdafx.h"
#include<Windows.h>
#include<graphics.h>
#include<conio.h>
#include<math.h>
void lineBres(int x0, int y0, int xEnd, int yEnd,int c);
int main(int argc, _TCHAR* argv[])
{
    int gd=DETECT,gm; /*图形屏幕初始化*/
    initgraph(&gd,&gm,"");
    lineBres(0,0,100,100,255);
    getch();
    closegraph();
    return 0;
}


void lineBres(int x0, int y0, int xEnd, int yEnd,int c)
{
        int dx = (int)fabs(double((xEnd-x0)));
        int dy = (int)fabs(double(yEnd-y0));
        int p =2* dy-dx;
        int twoDy = 2*dy, twoDyMinusDx = 2* (dy - dx);
        int x,y;

        if (x0>xEnd)
        {
                x=xEnd;
                y=yEnd;
                xEnd=x0;
        }
        else{
                x=x0;
                y=y0;
        }
        putpixel (x,y,c);

        while (x<xEnd)
        {
                x++;
                if(p<0)
                        p+=twoDy;
                else{
                        y++;
                        p+=twoDyMinusDx;
                }
               putpixel (x,y,c);
       }
}

 

#include "stdafx.h"
#include<Windows.h>
#include<graphics.h>
#include<conio.h>
#include<math.h>
void CircleBres(int xc, int yc, int radious, int c);
void plot_circle_points(int xc, int yc, int x, int y, int c);
int main(int argc, _TCHAR* argv[])
{
    int gd=DETECT,gm; /*图形屏幕初始化*/
    initgraph(&gd,&gm,"");
    CircleBres(100,100,20,255);
    getch();
    closegraph();
    return 0;
}
void CircleBres(int xc, int yc, int radious, int c)
{
     int x,y,p;
     x=0;
     y=radious;
     p=3-2*radious;
     while(x<y){
         plot_circle_points(xc,yc,x,y,c);
         if(p<0) p=p+4*x+6;
         else{
         p=p+4*(x-y)+10;
         y-=1;
         } 
     x+=1;
     }
     if(x==y){
         plot_circle_points(xc,yc,x,y,c);
     }
}
void plot_circle_points(int xc, int yc, int x, int y, int c)
{
    putpixel(xc+x, yc+y, c);
    putpixel(xc-x, yc+y, c);
    putpixel(xc+x, yc-y, c);
    putpixel(xc-x, yc-y, c);
    putpixel(xc+y, yc+x, c);
    putpixel(xc-y, yc+x, c);
    putpixel(xc+y, yc-x, c);
    putpixel(xc-y, yc-x, c);
}

猜你喜欢

转载自www.cnblogs.com/asahiLikka/p/11580714.html