- Bresenham algprothm
- 0.5를 더해서 반올림 하는것을 탈피해서 비트 연산을 이용한다.
#include <stdio.h>
#include <windows.h>
#define MAX_X 36
#define MAX_Y 20
#define SetPixel(x,y) array[y][x]=1
int array[20][36];
void display(void)
{
int i,j;
system("cls");
for( i=0; i<MAX_Y; i++ )
{
for( j=0; j < MAX_X; j++ )
{
if( array[i][j] )
printf("□");
else
printf("■");
}
printf("\n");
}
}
void line_to( int x1, int y1, int x2, int y2 )
{
int dx = x2 - x1;
int dy = y2 - y1;
double a = (double)dy/dx;
double b = y1 - a*x1;
int stepx=1, stepy=1;
if ( dx < 0 )
stepx = -1;
if ( dy < 0 )
stepy = -1;
if( abs(dx) > abs(dy) )
{
while( x1 != x2 )
{
SetPixel( x1, (int)(a*x1+ b + 0.5) );
x1 += stepx;
}
SetPixel( x1, (int)(a*x1+ b + 0.5) );
}
else
{
while( y1 != y2 )
{
SetPixel( (int)( (y1-b)/a + 0.5) , y1 );
y1 += stepy;
}
SetPixel( (int)( (y1-b)/a + 0.5) , y1);
}
}
void line_to_1( int x1, int y1, int x2, int y2 )
{
int dx = x2 - x1;
int dy = y2 - y1;
double a = (double)dy/dx;
double b = y1 - a*x1;
int stepx=1, stepy=1;
double fraction = 0.5 + a;
if ( dx < 0 )
stepx = -1;
if ( dy < 0 )
stepy = -1;
if( abs(dx) > abs(dy) )
{
while( x1 != x2 )
{
SetPixel( x1, y1 );
if( fraction >= 1 )
{
y1 += 1;
fraction -= 1;
}
fraction += a;
x1 += 1;
}
}
else
{
while( y1 != y2 )
{
SetPixel( (int)( (y1-b)/a + 0.5) , y1 );
y1 += stepy;
}
SetPixel( (int)( (y1-b)/a + 0.5) , y1);
}
}
void line_to_2( int x1, int y1, int x2, int y2 )
{
int dx = x2 - x1;
int dy = y2 - y1;
double a = (double)dy/dx;
double b = y1 - a*x1;
int stepx=1, stepy=1;
int fraction = dx+2*dy;
if ( dx < 0 )
stepx = -1;
if ( dy < 0 )
stepy = -1;
if( abs(dx) > abs(dy) )
{
while( x1 != x2 )
{
SetPixel( x1, y1 );
if( fraction >= 1 )
{
y1 += 1;
fraction -= 2*dx;
}
fraction += 2*dy;
x1 += 1;
}
}
else
{
while( y1 != y2 )
{
SetPixel( (int)( (y1-b)/a + 0.5) , y1 );
y1 += stepy;
}
SetPixel( (int)( (y1-b)/a + 0.5) , y1);
}
}
void line_to_3( int x1, int y1, int x2, int y2 )
{
int dx = x2 - x1;
int dy = y2 - y1;
double a = (double)dy/dx;
double b = y1 - a*x1;
int stepx=1, stepy=1;
int fraction = (dy<<1) - dx;
if ( dx < 0 )
stepx = -1;
if ( dy < 0 )
stepy = -1;
dx <<= 1;
dy <<= 1;
if( abs(dx) > abs(dy) )
{
while( x1 != x2 )
{
SetPixel( x1, y1 );
if( fraction >= 0 )
{
y1 += 1;
fraction -= dx;
}
fraction += dy;
x1 += 1;
}
}
else
{
while( y1 != y2 )
{
SetPixel( (int)( (y1-b)/a + 0.5) , y1 );
y1 += stepy;
}
SetPixel( (int)( (y1-b)/a + 0.5) , y1);
}
}
void line_to_4( int x1, int y1, int x2, int y2 )
{
int dx = x2 - x1;
int dy = y2 - y1;
int stepx=1, stepy=1;
int fraction;
if ( dx < 0 )
{
dx = -dx;
stepx = -1;
}
if ( dy < 0 )
{
dy = -dy;
stepy = -1;
}
dx <<= 1;
dy <<= 1;
if( dx >= dy )
{
fraction = dy - (dx>>1);
while( x1 != x2 )
{
SetPixel( x1, y1 );
if( fraction >= 0 )
{
y1 += stepy;
fraction -= dx;
}
fraction += dy;
x1 += stepx;
}
}
else
{
fraction = dx - (dy>>1);
while( y1 != y2 )
{
SetPixel( x1, y1 );
if( fraction >= 0 )
{
x1 += stepx;
fraction -= dy;
}
fraction += dx;
y1 += stepy;
}
}
SetPixel( x1, y1 );
}
int main()
{
line_to_4(8, 15 , 0, 0 );
display();
}
Trackback Address :: 이 글에는 트랙백을 보낼 수 없습니다