void lineDDA(int x1, int y1, int x2, int y2)
{
int dx=x2-x1, dy=y2-y1, step, k;
float xinc, yinc, x=(float)x1, y=(float)y1;
if(abs(dx) > abs(dy)) step = abs(dx);
else step = abs(dy);
xinc = dx/(float)step;
yinc = dy/(float)step;
Iarray[x1][y1] = 0; // start pixel marking(setpixel function)
for(k=0;k<step;k++){
x += xinc;
y += yinc;
Iarray[(int)(x+0.5)][(int)(y+0.5)]=0;// marking(setpixel function)
}
}
1) 초기화
2)장축구하기
3) 시작위치에 marking
4) 다음 pixel위치 구하기
5) 4번을 장축 길이만큼 반복
1) 기울기가 1이하인 경우
void lineBres(int x1, int y1, int x2, int y2)
{
int dx=abs(x2-x1), dy=abs(y2-y1);
int p, twoDy, twoDyDx;
int x,y,xEnd,IorD;
if(dx > dy){
p=2*dy-dx; twoDy=2*dy; twoDyDx=2*(dy-dx);
if(x1>x2)
{
x=x2;y=y2;xEnd=x1;
if(y1-y2>0)IorD=1;
else IorD=-1;
}
else
{
x=x1;y=y1;xEnd=x2;
if(y2-y1>0)IorD=1;
else IorD=-1;
}
Iarray[x][y][0]=r; Iarray[x][y][1]=g; Iarray[x][y][2]=b;// start point marking with a user-defined color(r,g,b)
while(x<xEnd){
x++;
if(p<0) p+=twoDy;
else{ y+=IorD; p+=twoDyDx;}
Iarray[x][y][0]=r; Iarray[x][y][1]=g; Iarray[x][y][2]=b;//marking
}// end of while
}// end of if
else{
p = 2 * dx - dy;
twoDy = 2 * dx; twoDyDx = 2 * (dx - dy); //변화는값 계산해놓음
if (x1 > x2)
{
x = x2; y = y2; xEnd = x1; //아래에서 그리기
if (y1 - y2 > 0)IorD = 1; //기울기가 양수면 1 음수면 -1
else IorD = -1;
}
else
{
x = x1; y = y1; xEnd = x2; //아래에서 그리기
if (y2 - y1 > 0)IorD = 1; //기울기가 양수면 1 음수면 -1
else IorD = -1;
}
//마지막 배열 index : RGB
Iarray[x][y][0] = r; Iarray[x][y][1] = g; Iarray[x][y][2] = b;// start point marking with a user-defined color(r,g,b)
while (y < yEnd) {
y++; //x는 1씩증가 y는 유지 or 증가
if (p < 0) p += twoDy; //y : 유지
else { x += IorD; p += twoDyDx; }//y:변화
Iarray[x][y][0] = r; Iarray[x][y][1] = g; Iarray[x][y][2] = b;//marking
}
}
}