void clip(int x1,int y1,int x2,int y2,int xmin,int ymin,int xmax1,int ymax1) { float u1=0.0,u2=1.0,dx=x2-x1,dy=y2-y1; if(x1==x2&&y1==y2) return; if(cliptest(-dx,x1-xmin,&u1,&u2)) if(cliptest(dx,xmax1-x1,&u1,&u2)) if(cliptest(-dy,y1-ymin,&u1,&u2)) if(cliptest(dy,ymax1-y1,&u1,&u2)) { if(u2<1.0) { x2=x1+u2*dx; y2=y1+u2*dy; } if(u1>0.0) { x1+=u1*dx; y1+=u1*dy; } array_line_entry2(round(x1),round(y1),round(x2),round(y2)); set_color(cur_color); setdrawmode(COPY_MODE); bresline(round(x1),round(y1),round(x2),round(y2),1); } }
void liang(double x0,double y0,double x1,double y1) { //arguments are endpoints of the line ie..p1(x0,y0) and p2(x1,y1) double dx=x1-x0,dy=y1-y0,te=0.0,tl=1.0;//te and tl represent min(entry) and max(exit) if(cliptest(-dx,x0-xmin,&te,&tl)) //inside test wrt left edge if(cliptest(dx,xmax-x0,&te,&tl)) //inside test wrt Right edge if(cliptest(-dy,y0-ymin,&te,&tl)) //inside test wrt Bottom edge if(cliptest(dy,ymax-y0,&te,&tl)) //inside test wrt Top edge { if(tl<1.0) //means value of tl has been updated after calling cliptest { //new leaving point values ,i.e..p1 x1=x0+tl*dx; y1=y0+tl*dy; } if(te>0.0)//te was updated { //new leaving point values ,i.e..p2 x0=x0+te*dx; y0=y0+te*dy; } //Window to viewport mappings //sx and sy is used to scale the line. it zooms the the clipping window double sx=(xvmax-xvmin)/(xmax-xmin); double sy=(yvmax-yvmin)/(ymax-ymin); double vx0=xvmin+(x0-xmin)*sx; double vy0=yvmin+(y0-ymin)*sy; double vx1=xvmin+(x1-xmin)*sx; double vy1=yvmin+(y1-ymin)*sy; // black colored viewport glColor3f(0.0,0.0,0.0); glBegin(GL_LINE_LOOP); glVertex2f(xvmin,yvmin); glVertex2f(xvmax,yvmin); glVertex2f(xvmax,yvmax); glVertex2f(xvmin,yvmax); glEnd(); //blue clipped line glColor3f(0.0,0.0,1.0); glBegin(GL_LINES); glVertex2d(vx0,vy0); glVertex2d(vx1,vy1); glEnd(); } }
void lbc(double x0, double y0, double x1, double y1) { double dx = x1 - x0, dy = y1 - y0, te = 0.0, tl = 1.0; if(cliptest(-dx, x0 - xmin, &te, &tl)) if(cliptest(dx, xmax - x0, &te, &tl)) if(cliptest(-dy, y0 - ymin, &te, &tl)) if(cliptest(dy, ymax - y0, &te, &tl)) { if(tl<1.0) { x1 = x0+tl*dx; y1 = y0+tl*dy; } if(te>0.0) { x0 = x0+te*dx; y0 = y0+te*dy; } double sx = (xvmax - xvmin) / (xmax - xmin); double sy = (yvmax - yvmin) / (ymax - ymin); double vx0 = xvmin + (x0 - xmin) * sx; double vy0 = yvmin + (y0 - ymin) * sy; double vx1 = xvmin + (x1- xmin) * sx; double vy1 = yvmin + (y1- ymin) * sy; glColor3f(1.0, 0.0, 0.0); glBegin(GL_LINE_LOOP); glVertex2f(xvmin,yvmin); glVertex2f(xvmax,yvmin); glVertex2f(xvmax,yvmax); glVertex2f(xvmin,yvmax); glEnd(); glColor3f(0.0,0.0,1.0); glBegin(GL_LINES); glVertex2d(vx0,vy0); glVertex2d(vx1,vy1); glEnd(); } }
void LiangBarskyLineClipAndDraw (double x0, double y0,double x1, double y1) { double dx=x1-x0, dy=y1-y0, te=0.0, tl=1.0; if(cliptest(-dx,x0-xmin,&te,&tl)) // inside test wrt left edge if(cliptest(dx,xmax-x0,&te,&tl)) // inside test wrt right edge if(cliptest(-dy,y0-ymin,&te,&tl)) // inside test wrt bottom edge if(cliptest(dy,ymax-y0,&te,&tl)) // inside test wrt top edge { if( tl < 1.0 ) { x1 = x0 + tl*dx; y1 = y0 + tl*dy; } if( te > 0.0 ) { x0 = x0 + te*dx; y0 = y0 + te*dy; } // Window to viewport mappings double sx=(xvmax-xvmin)/(xmax-xmin); // Scale parameters double sy=(yvmax-yvmin)/(ymax-ymin); double vx0=xvmin+(x0-xmin)*sx; double vy0=yvmin+(y0-ymin)*sy; double vx1=xvmin+(x1-xmin)*sx; double vy1=yvmin+(y1-ymin)*sy; //draw a red colored viewport glColor3f(1.0, 0.0, 0.0); glBegin(GL_LINE_LOOP); glVertex2f(xvmin, yvmin); glVertex2f(xvmax, yvmin); glVertex2f(xvmax, yvmax); glVertex2f(xvmin, yvmax); glEnd(); glColor3f(0.0,0.0,1.0); // draw blue colored clipped line glBegin(GL_LINES); glVertex2d (vx0, vy0); glVertex2d (vx1, vy1); glEnd(); } }// end of line clipping
void Liang(double x0,double y0, double x1, double y1) { double dz=x1-x0,da=y1-y0,te=0.0,t1=1.0; glColor3f(1.0,0.0,0.0); glBegin(GL_LINE_LOOP); glVertex2f(xvmin,yvmin); glVertex2f(xvmax,yvmin); glVertex2f(xvmax,yvmax); glVertex2f(xvmin,yvmax); glEnd(); if(cliptest(-dz,x0-xmin,&te,&t1)) if(cliptest(dz,xmax-x0,&te,&t1)) if(cliptest(-da,y0-ymin,&te,&t1)) if(cliptest(da,ymax-y0,&te,&t1)) { if(t1<1.0) { x1=x0+t1*dz; y1=y0+t1*da; } if(te>0.0) { x0=x0+te*dz; y0=y0+te*da; } double sx=(xvmax-xvmin)/(xmax-xmin); double sy=(yvmax-yvmin)/(ymax-ymin); double vx0=xvmin+(x0-xmin)*sx; double vy0=yvmin+(y0-ymin)*sy; double vx1=xvmin+(x1-xmin)*sx; double vy1=yvmin+(y1-ymin)*sy; glColor3f(0.0,0.0,1.0); glBegin(GL_LINES); glVertex2d(vx0,vy0); glVertex2d(vx1,vy1); glEnd(); } }