Acasa Tehnologie Desenare linie folosind algoritmul Bresenham

Desenare linie folosind algoritmul Bresenham

by Dragos Schiopu

>

#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <iostream.h>
#include <math.h>
#include <dos.h>
//#include <mouse.h>

void Bresenham(const int x_1,const int y_1,const int x_2,const int y_2)
{
int color=getcolor( );

int x1=x_1;
int y1=y_1;

int x2=x_2;
int y2=y_2;

if(x_1>x_2)
{
x1=x_2;
y1=y_2;

x2=x_1;
y2=y_1;
}

int dx=abs(x2-x1);
int dy=abs(y2-y1);
int inc_dec=((y2>=y1)?1:-1);

if(dx>dy)
{
int two_dy=(2*dy);
int two_dy_dx=(2*(dy-dx));
int p=((2*dy)-dx);

int x=x1;
int y=y1;

putpixel(x,y,color);

while(x<x2)
{
x++;

if(p<0)
p+=two_dy;

else
{
y+=inc_dec;
p+=two_dy_dx;
}

putpixel(x,y,color);
}
}

else
{
int two_dx=(2*dx);
int two_dx_dy=(2*(dx-dy));
int p=((2*dx)-dy);

int x=x1;
int y=y1;

putpixel(x,y,color);

while(y!=y2)
{
y+=inc_dec;

if(p<0)
p+=two_dx;

else
{
x++;
p+=two_dx_dy;
}

putpixel(x,y,color);
}
}
}

int main(void)
{
int gdriver = DETECT, gmode, errorcode;
int xmax, ymax,y,x;

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

errorcode = graphresult();
if (errorcode != grOk)
{
printf("Graphics error: %sn", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}

xmax = getmaxx(); ymax = getmaxy();
//MouseInit();
//MouseShow();
Bresenham(100,100,200,0);
Bresenham(100,100,200,20);
Bresenham(100,100,200,40);
Bresenham(100,100,200,60);
Bresenham(100,100,200,80);
Bresenham(100,100,200,100);
Bresenham(100,100,200,120);
Bresenham(100,100,200,140);
Bresenham(100,100,200,160);
Bresenham(100,100,200,180);
Bresenham(100,100,200,200);

Bresenham(100,100,0,0);
Bresenham(100,100,0,20);
Bresenham(100,100,0,40);
Bresenham(100,100,0,60);
Bresenham(100,100,0,80);
Bresenham(100,100,0,100);
Bresenham(100,100,0,120);
Bresenham(100,100,0,140);
Bresenham(100,100,0,160);
Bresenham(100,100,0,180);
Bresenham(100,100,0,200);

Bresenham(100,100,100,0);
Bresenham(100,100,100,200);

//line(0,0,600,800);

//getch();
//closegraph();
//clrscr();
//getch();
//cout<<xmax<<"n";
//cout<<ymax;
getch();
return 0;
}

s-ar putea sa-ti placa