p 9 Left boundary Right boundary Top boundary



p 9 Left boundary Right boundary Top boundary ywmax p 2 p 10 p 4 p 1 p 3 윈도우 내부 p 8 p 6 p 5 Bottom boundary ywmin xwmin p 7 선 자르기 xwmax 3
















uin 1 u=0 uin 2 ustart = uin 2 uend = 1 ustart < uend: 그릴 부분 ustart = uin 2 부터 uend = 1 까지 u=1 uout 2 선 자르기 19

uin 1 u=0 uout 1 uin 2 ustart = uin 2 uend = uout 1 u=1 ustart > uend: 그릴 부분 없음 uout 2 선 자르기 20



line_clip()함수 소스 코드(line. c 파일 내) static int line_clip(float x 1, float y 1, float x 2, float y 2, float *cx 1, float *cy 1, float *cx 2, float *cy 2) { float dx, dy, p[4], q[4], Uin[2], Uout[2], u, Ustart, Uend; int k, in, out; float XWmin, XWmax, YWmin, YWmax; get_window_boundary(&XWmin, &XWmax, &YWmin, &YWmax); dx=x 2 -x 1; p[0] = -dx; p[1] = dx; p[2] = -dy; p[3] = dy; dy=y 2 -y 1; q[0] = x 1 - XWmin; q[1] = XWmax - x 1; q[2] = y 1 - YWmin; q[3] = YWmax - y 1; 계속 선 자르기 23
![계속 /*--- vertical line ---*/ if(dx==0. 0) { if(q[0] < 0. 0) return 0; 계속 /*--- vertical line ---*/ if(dx==0. 0) { if(q[0] < 0. 0) return 0;](http://slidetodoc.com/presentation_image/416e886cc26784423d2cb066c99cf6de/image-24.jpg)
계속 /*--- vertical line ---*/ if(dx==0. 0) { if(q[0] < 0. 0) return 0; /* outside of left boundary */ if(q[1] < 0. 0) return 0; /* outside of right boundary */ if(y 1 > y 2) {float tmp = y 1; y 1 = y 2; y 2 = tmp; } if(y 2 < YWmin) return 0; /* outside of bottom boundary */ if(y 1 > YWmax) return 0; /* outside of top boundary */ *cx 1 = *cx 2 = x 1; *cy 1 = (y 1 > YWmin) ? y 1 : YWmin; *cy 2 = (y 2 < YWmax) ? y 2 : YWmax; return 1; } 계속 선 자르기 24
![계속 /*--- horizontal line ---*/ if(dy==0. 0) { if(q[2] < 0. 0) return 0; 계속 /*--- horizontal line ---*/ if(dy==0. 0) { if(q[2] < 0. 0) return 0;](http://slidetodoc.com/presentation_image/416e886cc26784423d2cb066c99cf6de/image-25.jpg)
계속 /*--- horizontal line ---*/ if(dy==0. 0) { if(q[2] < 0. 0) return 0; /* outside of bottom boundary */ if(q[3] < 0. 0) return 0; /* outside of top boundary */ if(x 1 > x 2) {float tmp = x 1; x 1 = x 2; x 2 = tmp; } if(x 2 < XWmin) return 0; /* outside of left boundary */ if(x 1 > XWmax) return 0; /* outside of right boundary */ *cy 1 = *cy 2 = y 1; *cx 1 = (x 1 > XWmin) ? x 1 : XWmin; *cx 2 = (x 2 < XWmax) ? x 2 : XWmax; return 1; } 계속 선 자르기 25
![계속 for(in=out=k=0; k<4; k++) { u = q[k] / p[k]; if(p[k] < 0. 0) 계속 for(in=out=k=0; k<4; k++) { u = q[k] / p[k]; if(p[k] < 0. 0)](http://slidetodoc.com/presentation_image/416e886cc26784423d2cb066c99cf6de/image-26.jpg)
계속 for(in=out=k=0; k<4; k++) { u = q[k] / p[k]; if(p[k] < 0. 0) Uin[in++] = u; else Uout[out++] = u; }/*for*/ Ustart = MAX(Uin[0], Uin[1]), 0. 0); Uend = MIN(Uout[0], Uout[1]), 1. 0); if(Ustart > Uend) return 0; /* line passing outside of winodw */ *cx 1 = x 1 + Ustart *cy 1 = y 1 + Ustart *cx 2 = x 1 + Uend *cy 2 = y 1 + Uend * dx; * dy; return 1; } 선 자르기 26

draw_line()함수의 수정 Draw_line()함수에서는 라인을 그리기 전에 아래 코드와 같이 line_clip()함수를 호출하여 선분을 clip 한 후 그 결과 윈도우 내부에 나타날 부분이 있으면 (line_clip()함수의 return값이 0이면 그릴 부분이 없음) 그 부분만을 그린다. void draw_line(float x 1, float y 1, float x 2, float y 2, float r, float g, float b) { int px 1, py 1, px 2, py 2; /* pixel positions */ if(!line_clip(x 1, y 1, x 2, y 2, &x 1, &y 1, &x 2, &y 2)) return; get_pixel_position(x 1, y 1, &px 1, &py 1); get_pixel_position(x 2, y 2, &px 2, &py 2); line_bres(px 1, py 1, px 2, py 2, r, g, b); } 선 자르기 27
- Slides: 27