├── .gitignore ├── polygon.cpp ├── 4_connect_boundary_fill.cpp ├── flood.cpp ├── dda.cpp ├── 2d_reflection.cpp ├── 2d_scaling.cpp ├── 2d_translation.cpp ├── midpoint_circle.cpp ├── 3d_translation.cpp ├── 8_connect_boundary_fill.cpp ├── 2d_rotation.cpp ├── bezier_curve.cpp ├── bresenham_line.cpp ├── .vscode └── settings.json ├── sutherland_hodgeman_polygon_clip.cpp ├── av_ellipse.cpp ├── scan_line_polygon.cpp ├── scan_line_fill.cpp ├── main24.cpp ├── midpoint_ellipse.cpp ├── av_cohen_sutherland_line_clipping.cpp ├── 2d_shearing.cpp ├── point_clipping.cpp ├── 3d_trans_scale_rotation.cpp ├── av_2d_switch.cpp ├── main22.cpp ├── cohen.cpp ├── sutherland_hodgeman.cpp └── hermite.cpp /.gitignore: -------------------------------------------------------------------------------- 1 | *.exe 2 | github* 3 | nunkon* 4 | test* -------------------------------------------------------------------------------- /polygon.cpp: -------------------------------------------------------------------------------- 1 | // polygon drawing 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | int main() 9 | { 10 | int gdriver = DETECT, gmode, errorcode; 11 | int maxx, maxy; 12 | int poly[] = {110, 240, 130, 440, 150, 260, 170, 280, 190, 260, 110, 240}; 13 | initgraph(&gdriver, &gmode, ""); 14 | drawpoly(6, poly); 15 | getch(); 16 | closegraph(); 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /4_connect_boundary_fill.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | void boundaryFill4(int x, int y, int fill_color,int boundary_color) 3 | { 4 | if(getpixel(x, y) != boundary_color && 5 | getpixel(x, y) != fill_color) 6 | { 7 | putpixel(x, y, fill_color); 8 | boundaryFill4(x + 1, y, fill_color, boundary_color); 9 | boundaryFill4(x, y + 1, fill_color, boundary_color); 10 | boundaryFill4(x - 1, y, fill_color, boundary_color); 11 | boundaryFill4(x, y - 1, fill_color, boundary_color); 12 | } 13 | } 14 | int main() 15 | { 16 | int gd = DETECT, gm; 17 | initgraph(&gd, &gm, ""); 18 | int x = 250, y = 200, radius = 50; 19 | // user input 20 | circle(x, y, radius); 21 | boundaryFill4(x, y, 6, 15); 22 | delay(10000); 23 | getch(); 24 | closegraph(); 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /flood.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | void floodFill(int x, int y, int oldcolor, int newcolor) 5 | { 6 | if (getpixel(x, y) == oldcolor) 7 | { 8 | putpixel(x, y, newcolor); 9 | floodFill(x + 1, y, oldcolor, newcolor); 10 | floodFill(x, y + 1, oldcolor, newcolor); 11 | floodFill(x - 1, y, oldcolor, newcolor); 12 | floodFill(x, y - 1, oldcolor, newcolor); 13 | } 14 | } 15 | //getpixel(x,y) gives the color of specified pixel 16 | 17 | int main() 18 | { 19 | int gm, gd = DETECT, radius; 20 | int x, y; 21 | cout << "Enter x and y positions for circle\n"; 22 | cin >> x >> y; 23 | cout << "Enter radius of circle\n"; 24 | cin >> radius; 25 | initgraph(&gd, &gm, "c:\\turboc3\\bgi"); 26 | circle(x, y, radius); 27 | floodFill(x, y, 0, 15); 28 | getch(); 29 | closegraph(); 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /dda.cpp: -------------------------------------------------------------------------------- 1 | // C++ program to implement DDA algorithm 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | int main(){ 8 | int xa,xb,ya,yb,steps,gd=DETECT,gm; 9 | cout<<"\n enter the 1st point: "; 10 | cin>>xa>>ya; 11 | cout<<"\n enter the 2nd point: "; 12 | cin>>xb>>yb; 13 | initgraph(&gd,&gm,(char*)""); 14 | int dx=xb-xa, dy=yb-ya; 15 | if(abs(dx)>abs(dy)){ 16 | steps = dx; 17 | }else{ 18 | steps = dy; 19 | } 20 | float Xinc = (float)dx/(float)steps; 21 | float Yinc = (float)dy/(float)steps; 22 | float X = (float)xa, Y = (float)ya; 23 | for(int i=0;i<=steps;i++){ 24 | cout<<"("< 3 | #include 4 | using namespace std; 5 | int main() 6 | { 7 | int gm, gd = DETECT, x1, y1, x2, y2, x3, y3, x4, y4; 8 | initgraph(&gd, &gm, (char*)""); 9 | line(getmaxx() / 2, 0, getmaxx() / 2, getmaxy()); 10 | line(0, getmaxy() / 2, getmaxx(), getmaxy() / 2); 11 | cout<<"Enter four coordinates(x, y) : "; 12 | cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3 >> x4 >> y4; 13 | cout << "Original object - GREEN" << endl; 14 | setcolor(2); 15 | line(x1,y1,x2,y2); 16 | line(x1,y1,x3,y3); 17 | line(x3,y3,x4,y4); 18 | line(x2,y2,x4,y4); 19 | cout<<"Object after reflection through x-axis - YELLOW"; 20 | setcolor(14); 21 | line(x1, getmaxy() -y1,x2, getmaxy() -y2); 22 | line(x1, getmaxy() -y1,x3, getmaxy() -y3); 23 | line(x3, getmaxy() -y3,x4, getmaxy() -y4); 24 | line(x2, getmaxy() -y2,x4, getmaxy() -y4); 25 | getch(); 26 | closegraph(); 27 | return 0; 28 | } -------------------------------------------------------------------------------- /2d_scaling.cpp: -------------------------------------------------------------------------------- 1 | // 2d scaling 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | int x1, y1, x2, y2; 9 | 10 | void scaling() 11 | { 12 | int xn1, xn2, yn1, yn2; 13 | float sx, sy; 14 | printf("Enter the scaling factor"); 15 | scanf("%f%f", &sx, &sy); 16 | cleardevice(); 17 | outtextxy(300, 200, "SCALING"); 18 | xn1 = x1 * sx; 19 | yn1 = y1 * sy; 20 | xn2 = x2 * sx; 21 | yn2 = y2 * sy; 22 | line(x1, y1, x2, y2); 23 | line(xn1, yn1, xn2, yn2); 24 | getch(); 25 | } 26 | 27 | void get() 28 | { 29 | printf("\n Enter the coordinates x1,y1,x2,y2"); 30 | scanf("%d%d%d%d", &x1, &y1, &x2, &y2); 31 | outtextxy(200, 100, "ORIGINAL OBJECT"); 32 | line(x1, y1, x2, y2); 33 | getch(); 34 | } 35 | 36 | int main() 37 | { 38 | int ch, gd = DETECT, gm; 39 | initgraph(&gd, &gm, "c:\\tc\\bgi"); 40 | get(); 41 | cleardevice(); 42 | scaling(); 43 | return 0; 44 | } -------------------------------------------------------------------------------- /2d_translation.cpp: -------------------------------------------------------------------------------- 1 | // 2d translation 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | int x1, y1, x2, y2; 9 | 10 | void translation() 11 | { 12 | int tx, ty, xn1, xn2, yn1, yn2; 13 | printf("\n Enter the translation\n"); 14 | scanf("%d%d", &tx, &ty); 15 | cleardevice(); 16 | outtextxy(400, 100, "TRANSLATION"); 17 | xn1 = x1 + tx; 18 | yn1 = y1 + ty; 19 | xn2 = x2 + tx; 20 | yn2 = y2 + ty; 21 | line(x1, y1, x2, y2); 22 | line(xn1, yn1, xn2, yn2); 23 | getch(); 24 | } 25 | 26 | void get() 27 | { 28 | printf("\n Enter the coordinates x1,y1,x2,y2 : "); 29 | scanf("%d%d%d%d", &x1, &y1, &x2, &y2); 30 | outtextxy(200, 100, "ORIGINAL OBJECT"); 31 | line(x1, y1, x2, y2); 32 | getch(); 33 | } 34 | 35 | int main() 36 | { 37 | int ch, gd = DETECT, gm; 38 | initgraph(&gd, &gm, "c:\\tc\\bgi"); 39 | get(); 40 | cleardevice(); 41 | translation(); 42 | return 0; 43 | } -------------------------------------------------------------------------------- /midpoint_circle.cpp: -------------------------------------------------------------------------------- 1 | // C++ program to implement Midpoint circle drawing algorithm 2 | #include 3 | #include 4 | using namespace std; 5 | int main(){ 6 | int gd = DETECT, gm, xc, yc, r; 7 | initgraph(&gd, &gm, (char*)""); 8 | cout << "Enter the coordinates of the center: "; 9 | cin >> xc >> yc; 10 | cout << "Enter the radius: "; 11 | cin >> r; 12 | int x = 0, y = r, p = 1 - r; 13 | while (x < y){ 14 | putpixel(xc + x, yc + y, WHITE); 15 | putpixel(xc - x, yc + y, WHITE); 16 | putpixel(xc + x, yc - y, WHITE); 17 | putpixel(xc - x, yc - y, WHITE); 18 | putpixel(xc + y, yc + x, WHITE); 19 | putpixel(xc - y, yc + x, WHITE); 20 | putpixel(xc + y, yc - x, WHITE); 21 | putpixel(xc - y, yc - x, WHITE); 22 | if (p < 0){ 23 | p += 2 * x + 3; 24 | } 25 | else{ 26 | p += 2 * (x - y) + 5; 27 | y--; 28 | } 29 | x++; 30 | } 31 | getch(); 32 | closegraph(); 33 | return 0; 34 | } -------------------------------------------------------------------------------- /3d_translation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | int x1, x2, y1, y2, mx, my, depth; 8 | 9 | void draw() { 10 | bar3d(x1, y1, x2, y2, depth, 1); 11 | } 12 | 13 | void trans() { 14 | int a1, a2, b1, b2, dep, x, y; 15 | printf("\n Enter the Ttransition Co ordinates:"); 16 | scanf("%d%d", &x, &y); 17 | a1 = x1 + x; 18 | a2 = x2 + x; 19 | b1 = y1 + y; 20 | b2 = y2 + y; 21 | dep = (a2 - a1) / 4; 22 | bar3d(a1, b1, a2, b2, dep, 1); 23 | setcolor(5); 24 | draw(); 25 | 26 | } 27 | 28 | 29 | int main() { 30 | int gd = DETECT, gm, c; 31 | initgraph(&gd, &gm, (char*)""); 32 | printf("\n\t\t3D Transmission\n\n"); 33 | printf("\nEnter 1st top value(x1,y1):"); 34 | scanf("%d%d", &x1, &y1); 35 | printf("Enter right bottom value(x2,y2):"); 36 | scanf("%d%d", &x2, &y2); 37 | depth = (x2 - x1) / 4; 38 | mx = (x1 + x2) / 2; 39 | my = (y1 + y2) / 2; 40 | draw(); 41 | getch(); 42 | cleardevice(); 43 | trans(); 44 | getch(); 45 | return 0; 46 | } 47 | 48 | -------------------------------------------------------------------------------- /8_connect_boundary_fill.cpp: -------------------------------------------------------------------------------- 1 | //8 connect Boundary Filling Algorithm 2 | #include 3 | void boundaryFill8(int x, int y, int fill_color,int boundary_color) 4 | { 5 | if(getpixel(x, y) != boundary_color && 6 | getpixel(x, y) != fill_color) 7 | { 8 | putpixel(x, y, fill_color); 9 | boundaryFill8(x + 1, y, fill_color, boundary_color); 10 | boundaryFill8(x, y + 1, fill_color, boundary_color); 11 | boundaryFill8(x - 1, y, fill_color, boundary_color); 12 | boundaryFill8(x, y - 1, fill_color, boundary_color); 13 | boundaryFill8(x - 1, y - 1, fill_color, boundary_color); 14 | boundaryFill8(x - 1, y + 1, fill_color, boundary_color); 15 | boundaryFill8(x + 1, y - 1, fill_color, boundary_color); 16 | boundaryFill8(x + 1, y + 1, fill_color, boundary_color); 17 | } 18 | } 19 | int main() 20 | { 21 | int gd = DETECT, gm; 22 | initgraph(&gd, &gm, ""); 23 | rectangle(50, 50, 100, 100); 24 | boundaryFill8(55, 55, 4, 15); 25 | delay(10000); 26 | getch(); 27 | closegraph(); 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /2d_rotation.cpp: -------------------------------------------------------------------------------- 1 | // 2d rotation 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | int x1, y1, x2, y2; 9 | 10 | void rotation() 11 | { 12 | int r; 13 | float rx, xn1, xn2, yn1, yn2; 14 | printf("\n enter the angle for rotation"); 15 | scanf("%d", &r); 16 | cleardevice(); 17 | outtextxy(500, 200, "ROTATION"); 18 | rx = (r * 3.14) / 180; 19 | xn1 = x1 * cos(rx) - y1 * sin(rx); 20 | yn1 = y1 * cos(rx) + x1 * sin(rx); 21 | xn2 = x2 * cos(rx) - y2 * sin(rx); 22 | yn2 = y2 * cos(rx) + x2 * sin(rx); 23 | line(x1, y1, x2, y2); 24 | line(xn1, yn1, xn2, yn2); 25 | getch(); 26 | } 27 | 28 | void get() 29 | { 30 | printf("\n Enter the coordinates x1,y1,x2,y2"); 31 | scanf("%d%d%d%d", &x1, &y1, &x2, &y2); 32 | outtextxy(200, 100, "ORIGINAL OBJECT"); 33 | line(x1, y1, x2, y2); 34 | getch(); 35 | } 36 | 37 | int main() 38 | { 39 | int ch, gd = DETECT, gm; 40 | initgraph(&gd, &gm, (char *)""); 41 | get(); 42 | rotation(); 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /bezier_curve.cpp: -------------------------------------------------------------------------------- 1 | // implement a Bézier curve in C++ 2 | 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | int main(){ 8 | int gd = DETECT, gm; 9 | initgraph(&gd, &gm, ""); 10 | // user input x1, y1, x2, y2, x3, y3, x4, y4 11 | int x1 = 100, y1 = 0, x2 = 300, y2 = 300, x3 = 400, y3 = 300, x4 = 400, y4 = 100; 12 | int x, y; 13 | // original form according to the formula 14 | // for(int i=0;i<=1;i+=0.01){ 15 | // x = (1-i)*(1-i)*(1-i)*x1 + 3*i*(1-i)*(1-i)*x2 + 3*i*i*(1-i)*x3 + i*i*i*x4; 16 | // y = (1-i)*(1-i)*(1-i)*y1 + 3*i*(1-i)*(1-i)*y2 + 3*i*i*(1-i)*y3 + i*i*i*y4; 17 | // putpixel(x, y, WHITE); 18 | // } 19 | for (float i = 0; i <= 1; i += 0.0001){ 20 | x = (1 - i ) * (1 - i ) * (1 - i ) * x1 + 3 * (1 - i ) * (1 - i ) * i * x2 + 3 * (1 - i ) * i * i * x3 + i * i * i * x4; 21 | y = (1 - i ) * (1 - i ) * (1 - i ) * y1 + 3 * (1 - i ) * (1 - i ) * i * y2 + 3 * (1 - i ) * i * i * y3 + i * i * i * y4; 22 | putpixel(x, y, WHITE); 23 | } 24 | getch(); 25 | closegraph(); 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /bresenham_line.cpp: -------------------------------------------------------------------------------- 1 | // C++ program to implement Bresenham's Line Algorithm 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | int main() 7 | { 8 | int gd=DETECT, gm, x1, y1, x2, y2, dx, dy, p, x, y, xEnd; 9 | initgraph(&gd, &gm, (char*)""); 10 | cout<<"Enter co-ordinates of first point: "; 11 | cin>> x1 >> y1; 12 | cout<<"Enter co-ordinates of second point: "; 13 | cin>> x2 >> y2; 14 | dx=abs(x2-x1); 15 | dy=abs(y2-y1); 16 | p=2*dy-dx; 17 | // Determine which point to start and which point to end 18 | if(x1 > x2) 19 | { 20 | x = x2; 21 | y = y2; 22 | xEnd = x1; 23 | } 24 | else 25 | { 26 | x = x1; 27 | y = y1; 28 | xEnd = x2; 29 | } 30 | cout<<"\n("<=0) 35 | { 36 | y=y+1; 37 | p=p+2*dy-2*dx; 38 | } 39 | else 40 | { 41 | p=p+2*dy; 42 | } 43 | x=x+1; 44 | cout<<"\n("< 2 | // #include 3 | // using namespace std; 4 | #include 5 | #include 6 | #include 7 | int main() 8 | { 9 | int gd,gm,n,*x,i,k=0; 10 | //window coordinates int wx1=220,wy1=140,wx2=420,wy2=140,wx3=420,wy3=340,wx4=220,wy4=340; 11 | int w[]={220,140,420,140,420,340,220,340,220,140};//array for drawing window 12 | detectgraph(&gd,&gm); 13 | initgraph(&gd,&gm,""); //initializing graphics 14 | printf("Window:-"); 15 | setcolor(RED); //red colored window 16 | drawpoly(5,w); //window drawn 17 | printf("Enter the no. of vertices of polygon: "); 18 | scanf("%d",&n); 19 | x = malloc(n*2+1); 20 | printf("Enter the coordinates of points:\n"); 21 | k=0; 22 | for(i=0;i 8 | #include 9 | #include 10 | using namespace std; 11 | #include 12 | int main() 13 | { 14 | int gd = DETECT, gm; 15 | int xc,yc,x,y;float p; 16 | long rx,ry; 17 | initgraph(&gd, &gm,(char*)""); 18 | cout<<"Enter coordinates of focus : "; 19 | cin>>xc>>yc; 20 | cout<<"Enter x,y radius of ellipse: "; 21 | cin>>rx>>ry; 22 | 23 | //Region 1 24 | p=ry*ry-rx*rx*ry+rx*rx/4; 25 | x=0;y=ry; 26 | while(2.0*ry*ry*x <= 2.0*rx*rx*y) 27 | { 28 | if(p < 0) 29 | { 30 | x++; 31 | p = p+2*ry*ry*x+ry*ry; 32 | } 33 | else 34 | { 35 | x++;y--; 36 | p = p+2*ry*ry*x-2*rx*rx*y-ry*ry; 37 | } 38 | putpixel(xc+x,yc+y,RED); 39 | putpixel(xc+x,yc-y,RED); 40 | putpixel(xc-x,yc+y,RED); 41 | putpixel(xc-x,yc-y,RED); 42 | } 43 | 44 | //Region 2 45 | p=ry*ry*(x+0.5)*(x+0.5)+rx*rx*(y-1)*(y-1)-rx*rx*ry*ry; 46 | while(y > 0) 47 | { 48 | if(p <= 0) 49 | { 50 | x++;y--; 51 | p = p+2*ry*ry*x-2*rx*rx*y+rx*rx; 52 | } 53 | else 54 | { 55 | y--; 56 | p = p-2*rx*rx*y+rx*rx; 57 | } 58 | putpixel(xc+x,yc+y,RED); 59 | putpixel(xc+x,yc-y,RED); 60 | putpixel(xc-x,yc+y,RED); 61 | putpixel(xc-x,yc-y,RED); 62 | } 63 | getch(); 64 | closegraph(); 65 | } -------------------------------------------------------------------------------- /scan_line_polygon.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | int main() 9 | { 10 | int gd = DETECT, gm; 11 | initgraph(&gd, &gm,(char*)""); 12 | setcolor(GREEN); 13 | int arr[] ={50,400,50,300,150,350,250,300,250,400,50,400}; 14 | drawpoly(6, arr);8 15 | // setfillstyle(WIDE_DOT_FILL,BLUE); 16 | // fillpoly(6,arr); 17 | setcolor(YELLOW); 18 | // line(10,350,450,350); 19 | int dx,dy,p,xEnd,x,y,x1=10,y1=340,x2=450,y2=340; 20 | dx=abs(x2-x1); 21 | dy=abs(y2-y1); 22 | p=2*dy-dx; 23 | // Determine which point to start and which point to end 24 | if(x1 > x2) 25 | { 26 | x = x2; 27 | y = y2; 28 | xEnd = x1; 29 | } 30 | else 31 | { 32 | x = x1; 33 | y = y1; 34 | xEnd = x2; 35 | } 36 | // cout<<"\n("<=0) 42 | { 43 | y=y+1; 44 | p=p+2*dy-2*dx; 45 | } 46 | else 47 | { 48 | p=p+2*dy; 49 | } 50 | x=x+1; 51 | // cout<<"\n("< 2 | #include 3 | using namespace std; 4 | int main() 5 | { 6 | int n,x[10],y[10],k=0,ymin=1000000,ymax=0,Y,dx,dy,xi[100],gm,gd,temp; 7 | float slope[100]; 8 | printf("Enter the number of vertices:"); 9 | scanf("%d",&n); 10 | printf("enter the coordinates of vertices:"); 11 | for(int i=0;iymax) 15 | ymax=y[i]; 16 | if(y[i]j)) || ((y[i]>j) && (y[i+1]<=j))){ 42 | xi[k]=(int)(x[i]+slope[i]*(j-y[i])); 43 | k++; 44 | } 45 | } 46 | for(int m=0;mxi[i+1]){ 49 | temp=xi[i]; 50 | xi[i]=xi[i+1]; 51 | xi[i+1]=temp; 52 | } 53 | } 54 | for(int i=0;i 2 | using namespace std; 3 | #include 4 | #include 5 | #include 6 | void Window() 7 | { 8 | line (200,200,350,200); 9 | line(350,200,350,350); 10 | line(200,200,200,350); 11 | line(200,350,350,350); 12 | } 13 | void Code(char c[4],float x,float y) 14 | { c[0]=(x<200)?'1':'0'; 15 | c[1]=(x>350)?'1':'0'; 16 | c[2]=(y<200)?'1':'0'; 17 | c[3]=(y>350)?'1':'0'; 18 | } 19 | void Clipping (char c[],char d[],float &x,float &y,float m) 20 | { 21 | int flag=1,i=0; 22 | for (i=0;i<4;i++) 23 | { 24 | if(c[i]!='0' && d[i]!='0') 25 | { 26 | flag=0; 27 | break; 28 | } 29 | if(flag) 30 | { 31 | if(c[0]!='0') 32 | { 33 | y=m*(200-x)+y; 34 | x=200; 35 | } 36 | else if(c[1]!='0') 37 | { 38 | y=m*(350-x)+y; 39 | x=350; 40 | } 41 | else if(c[2]!='0') 42 | { 43 | x=((200-y)/m)+x; 44 | y=200; 45 | } 46 | else if(c[3]!='0') 47 | { 48 | x=((350-y)/m)+x; 49 | y=350; 50 | } 51 | } 52 | if (flag==0) 53 | cout<<"Line lying outside"; 54 | } 55 | } 56 | int main() 57 | { 58 | int gdriver = DETECT, gmode, errorcode; 59 | float x1,y1,x2,y2; 60 | float m; 61 | char c[4],d[4]; 62 | initgraph(&gdriver, &gmode, (char*)""); 63 | cout<<"\n Enter coordinates"; 64 | cin>>x1>>y1>>x2>>y2; 65 | cout<<"\n Before clipping"; 66 | Window(); 67 | line(x1,y1,x2,y2); 68 | getch(); 69 | cleardevice(); 70 | m=float((y2-y1)/(x2-x1)); 71 | Code(c,x1,y1); 72 | Code(d,x2,y2) ; 73 | Clipping(c,d,x1,y1,m); 74 | Clipping(d,c,x2,y2,m); 75 | cout<<"\n After Clipping"; 76 | Window(); 77 | line(x1,y1,x2,y2); 78 | getch(); 79 | closegraph(); 80 | } 81 | -------------------------------------------------------------------------------- /midpoint_ellipse.cpp: -------------------------------------------------------------------------------- 1 | // C++ program to implement midpoint ellipse algorithm 2 | #include 3 | #include 4 | using namespace std; 5 | int main() 6 | { 7 | int gm = DETECT, gd, xc, yc, rx, ry; 8 | int x, y, p; 9 | cout << "Enter Xc, Yc, Rx, Ry : "; 10 | cin >> xc >> yc >> rx >> ry; 11 | x = 0; 12 | y = ry; 13 | initgraph(&gm, &gd, (char *)""); 14 | 15 | p = (ry * ry) - (rx * rx * ry) + ((rx * rx) / 4); 16 | while ((2 * x * ry * ry) < (2 * y * rx * rx)) 17 | { 18 | putpixel(xc + x, yc - y, WHITE); 19 | putpixel(xc - x, yc + y, WHITE); 20 | putpixel(xc + x, yc + y, WHITE); 21 | putpixel(xc - x, yc - y, WHITE); 22 | if (p < 0) 23 | { 24 | x = x + 1; 25 | p = p + (2 * ry * ry * x) + (ry * ry); 26 | } 27 | else 28 | { 29 | x = x + 1; 30 | y = y - 1; 31 | p = p + (2 * ry * ry * x + ry * ry) - (2 * rx * rx * y); 32 | } 33 | } 34 | p = ((float)x + 0.5) * ((float)x + 0.5) * ry * ry + (y - 1) * (y - 1) * rx * rx - rx * rx * ry * ry; 35 | 36 | while (y >= 0) 37 | { 38 | putpixel(xc + x, yc - y, WHITE); 39 | putpixel(xc - x, yc + y, WHITE); 40 | putpixel(xc + x, yc + y, WHITE); 41 | putpixel(xc - x, yc - y, WHITE); 42 | 43 | if (p > 0) 44 | { 45 | y = y - 1; 46 | p = p - (2 * rx * rx * y) + (rx * rx); 47 | } 48 | else 49 | { 50 | y = y - 1; 51 | x = x + 1; 52 | p = p + (2 * ry * ry * x) - (2 * rx * rx * y) - (rx * rx); 53 | } 54 | } 55 | getch(); 56 | closegraph(); 57 | return 0; 58 | } -------------------------------------------------------------------------------- /av_cohen_sutherland_line_clipping.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #include 4 | #include 5 | #include 6 | void Window() 7 | { 8 | line (200,200,350,200); 9 | line(350,200,350,350); 10 | line(200,200,200,350); 11 | line(200,350,350,350); 12 | } 13 | void Code(char c[4],float x,float y) 14 | { c[0]=(x<200)?'1':'0'; 15 | c[1]=(x>350)?'1':'0'; 16 | c[2]=(y<200)?'1':'0'; 17 | c[3]=(y>350)?'1':'0'; 18 | } 19 | void Clipping (char c[],char d[],float &x,float &y,float m) 20 | { 21 | int flag=1,i=0; 22 | for (i=0;i<4;i++) 23 | { 24 | if(c[i]!='0' && d[i]!='0') 25 | { 26 | flag=0; 27 | break; 28 | } 29 | if(flag) 30 | { 31 | if(c[0]!='0') 32 | { 33 | y=m*(200-x)+y; 34 | x=200; 35 | } 36 | else if(c[1]!='0') 37 | { 38 | y=m*(350-x)+y; 39 | x=350; 40 | } 41 | else if(c[2]!='0') 42 | { 43 | x=((200-y)/m)+x; 44 | y=200; 45 | } 46 | else if(c[3]!='0') 47 | { 48 | x=((350-y)/m)+x; 49 | y=350; 50 | } 51 | } 52 | if (flag==0) 53 | cout<<"Line lying outside"; 54 | } 55 | } 56 | int main() 57 | { 58 | int gdriver = DETECT, gmode, errorcode; 59 | float x1,y1,x2,y2; 60 | float m; 61 | char c[4],d[4]; 62 | initgraph(&gdriver, &gmode, (char*)""); 63 | cout<<"\n Enter coordinates"; 64 | cin>>x1>>y1>>x2>>y2; 65 | cout<<"\n Before clipping"; 66 | Window(); 67 | line(x1,y1,x2,y2); 68 | getch(); 69 | cleardevice(); 70 | m=float((y2-y1)/(x2-x1)); 71 | Code(c,x1,y1); 72 | Code(d,x2,y2) ; 73 | Clipping(c,d,x1,y1,m); 74 | Clipping(d,c,x2,y2,m); 75 | cout<<"\n After Clipping"; 76 | Window(); 77 | line(x1,y1,x2,y2); 78 | getch(); 79 | closegraph(); 80 | } -------------------------------------------------------------------------------- /2d_shearing.cpp: -------------------------------------------------------------------------------- 1 | // C++ program to implement 2D shearing 2 | #include 3 | #include 4 | using namespace std; 5 | int main() 6 | { 7 | int gd=DETECT,gm; 8 | int x1,y1,x2,y2,x3,y3,x4, y4, shear_factor, choice; 9 | initgraph(&gd,&gm,(char*)""); 10 | cout << "Enter four coordinates(x, y) : "; 11 | cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3 >> x4 >> y4; 12 | cout<< "Do you want to.." <> choice; 14 | cout<<"Enter the shearing factor: "; 15 | cin >>shear_factor; 16 | 17 | cout<<"Original object - WHITE"< 3 | #include "graphics.h" 4 | using namespace std; 5 | 6 | // Function for point clipping 7 | void pointClip(int XY[][2], int n, int Xmin, int Ymin, int Xmax, int Ymax) 8 | { 9 | // graphics view 10 | // initialize graphics mode 11 | for (int i = 0; i < n; i++) 12 | { 13 | if ((XY[i][0] >= Xmin) && (XY[i][0] <= Xmax)) 14 | { 15 | if ((XY[i][1] >= Ymin) && (XY[i][1] <= Ymax)) 16 | putpixel(XY[i][0], XY[i][1], 15); 17 | } 18 | } 19 | 20 | /**** Arithmetic view ****/ 21 | cout << "Point inside the viewing pane:" << endl; 22 | for (int i = 0; i < n; i++) 23 | { 24 | if ((XY[i][0] >= Xmin) && (XY[i][0] <= Xmax)) 25 | { 26 | if ((XY[i][1] >= Ymin) && (XY[i][1] <= Ymax)) 27 | cout << "[" << XY[i][0] << "," << XY[i][1] << "] "; 28 | } 29 | } 30 | 31 | // print point coordinate outside viewing pane 32 | cout << "\n" 33 | << endl; 34 | cout << "Point outside the viewing pane:" << endl; 35 | for (int i = 0; i < n; i++) 36 | { 37 | if ((XY[i][0] < Xmin) || (XY[i][0] > Xmax)) 38 | cout << "[" << XY[i][0] << "," << XY[i][1] << "] "; 39 | if ((XY[i][1] < Ymin) || (XY[i][1] > Ymax)) 40 | cout << "[" << XY[i][0] << "," << XY[i][1] << "] "; 41 | } 42 | } 43 | 44 | int main() 45 | { 46 | // these are points to be checked if this is in the visible are or not... user input 47 | int XY[7][2] = {{10, 10}, 48 | {25, 105}, 49 | {-10, 10}, 50 | {400, 100}, 51 | {100, 400}, 52 | {400, 400}, 53 | {100, 40}}; 54 | int gd = DETECT, gm; 55 | initgraph(&gd, &gm, ""); 56 | int Xmin = 0; 57 | int Xmax = getmaxx(); 58 | int Ymin = 0; 59 | int Ymax = getmaxy(); 60 | pointClip(XY, 7, Xmin, Ymin, Xmax, Ymax); 61 | getch(); 62 | closegraph(); 63 | return 0; 64 | } 65 | -------------------------------------------------------------------------------- /3d_trans_scale_rotation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int x1, x2, y3, y4, mx, my, depth; 7 | 8 | void draw(){ 9 | bar3d(x1, y3, x2, y4, depth, 1); 10 | } 11 | 12 | void rotate(){ 13 | float t; int a1, b1, a2, b2, dep; 14 | printf("Enter the angle to rotate: "); 15 | scanf("%f", &t); 16 | t = t * (3.14 / 180); 17 | a1 = mx + (x1 - mx) * cos(t) - (y3 - my) * sin(t); 18 | a2 = mx + (x2 - mx) * cos(t) - (y4 - my) * sin(t); 19 | b1 = my + (x1 - mx) * sin(t) - (y3 - my) * cos(t); 20 | b2 = my + (x2 - mx) * sin(t) - (y4 - my) * cos(t); 21 | if (a2 > a1) 22 | dep = (a2 - a1) / 4; 23 | else 24 | dep = (a1 - a2) / 4; 25 | bar3d(a1, b1, a2, b2, dep, 1); 26 | setcolor(5); 27 | draw(); 28 | } 29 | 30 | void trans(){ 31 | int a1, a2, b1, b2, dep, x, y; 32 | printf("Enter the transition coordinates:"); 33 | scanf("%d%d", &x, &y); 34 | a1 = x1 + x; 35 | a2 = x2 + x; 36 | b1 = y3 + y; 37 | b2 = y4 + y; 38 | dep = (a2 - a1) / 4; 39 | bar3d(a1, b1, a2, b2, dep, 1); 40 | setcolor(5); 41 | draw(); 42 | } 43 | 44 | void scale(){ 45 | int x, y, a1, a2, b1, b2, dep; 46 | printf("Enter scalling coordinates: "); 47 | scanf("%d%d", &x, &y); 48 | a1 = mx + (x1 - mx) * x; 49 | a2 = mx + (x2 - mx) * x; 50 | b1 = my + (y3 - my) * y; 51 | b2 = my + (y4 - my) * y; 52 | dep = (a2 - a1) / 4; 53 | bar3d(a1, b1, a2, b2, dep, 1); 54 | setcolor(5); 55 | draw(); 56 | } 57 | 58 | int main(){ 59 | int gd = DETECT, gm, c, choice; 60 | bool continueLoop = true; 61 | initgraph(&gd, &gm, (char *)""); 62 | printf("3D Transformations\n"); 63 | printf("Enter 1st top value(x1,y1): "); 64 | scanf("%d%d", &x1, &y3); 65 | printf("Enter right bottom value(x2,y2): "); 66 | scanf("%d%d", &x2, &y4); 67 | depth = (x2 - x1) / 4; 68 | mx = (x1 + x2) / 2; 69 | my = (y3 + y4) / 2; 70 | do 71 | { 72 | draw(); 73 | cout << "Choose an option.." << endl; 74 | cout << "1. Translation" << endl; 75 | cout << "2. Rotation" << endl; 76 | cout << "3. Scaling" << endl; 77 | cout << "4. Exit" << endl; 78 | cout << "Enter your choice: "; 79 | cin >> choice; 80 | switch (choice) 81 | { 82 | case 1: 83 | trans(); 84 | break; 85 | case 2: 86 | rotate(); 87 | break; 88 | case 3: 89 | scale(); 90 | break; 91 | case 4: 92 | continueLoop = false; 93 | exit (0); 94 | break; 95 | default: 96 | cout << "Invalid choice!!"; 97 | break; 98 | } 99 | getch(); 100 | cleardevice(); 101 | } while (continueLoop); 102 | return 0; 103 | } -------------------------------------------------------------------------------- /av_2d_switch.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | int main() 6 | { 7 | int gd=DETECT,gm,s; 8 | initgraph(&gd,&gm,(char*)""); 9 | cout<<"1.Translation\n2.Rotation\n3.Scaling\n4.Reflection\n5.Shearing "<>s; 12 | switch(s) 13 | { 14 | case 1: 15 | { int x1=200,y1=150,x2=300,y2=250; 16 | int tx=50,ty=50; 17 | cout<<"Rectangle before translation"<>a; 34 | a=(a*3.14)/180; 35 | long xr=x1+((x2-x1)*cos(a)-(y2-y1)*sin(a)); 36 | long yr=y1+((x2-x1)*sin(a)+(y2-y1)*cos(a)); 37 | setcolor(2); 38 | rectangle(x1,y1,xr,yr); 39 | getch(); 40 | break; 41 | } 42 | case 3: 43 | { 44 | int x1=30,y1=30,x2=70,y2=70,y=2,x=2; 45 | cout<<"Before scaling"< 2 | #include 3 | using namespace std; 4 | 5 | struct vertex 6 | { 7 | float x; 8 | float y; 9 | }; 10 | 11 | vertex sp[40], cw[20]; 12 | int n_sp, n_cw; 13 | 14 | void draw_poly(vertex vlist[], int n) 15 | { 16 | for(int i=0; i>n_cw; 100 | cout<<"Enter vertices (x,y) clockwise"<>cw[i].x>>cw[i].y; 103 | draw_poly(cw, n_cw); 104 | cout<<"Enter no. of vertices of subject polygon"<>n_sp; 106 | cout<<"Enter vertices (x,y) clockwise"<>sp[i].x>>sp[i].y; 109 | draw_poly(sp, n_sp); 110 | 111 | char ch; 112 | cout<<"Press a key to clip from an edge of clipping window"<>ch; 116 | edge_clip(cw[i].x, cw[i].y, cw[(i+1)%n_cw].x, cw[(i+1)%n_cw].y); 117 | cleardevice(); 118 | draw_poly(cw, n_cw); 119 | draw_poly(sp, n_sp); 120 | } 121 | 122 | getch(); 123 | closegraph(); 124 | return 0; 125 | } 126 | -------------------------------------------------------------------------------- /cohen.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int getRegionCode(double x, double y,double x_min,double x_max,double y_min,double y_max) 6 | { 7 | int code = 0; 8 | 9 | if (x < x_min) 10 | code = code | 1; 11 | else if (x > x_max) 12 | code = code | 2; 13 | if (y < y_min) 14 | code = code | 4; 15 | else if (y > y_max) 16 | code = code | 8; 17 | 18 | return code; 19 | } 20 | 21 | string CSAlgorithm(int code1, int code2, double x1, double y1, double x2, double y2,double x_min,double x_max,double y_min,double y_max){ 22 | 23 | //bool clip=true; 24 | bool outside=false; 25 | bool inside=false; 26 | 27 | if(code1==0 && code2==0){ 28 | //inside=true; 29 | return "inside"; 30 | } 31 | else 32 | if(code1 & code2){ 33 | return "outside"; 34 | } 35 | else{ 36 | cout <<"\n\tLine is partially inside and needs to be clipped...\n"; 37 | 38 | while(true) 39 | { 40 | if(code1==0 && code2==0){ 41 | break; 42 | } 43 | else if (code1 & code2) 44 | { 45 | break; 46 | } 47 | else{ 48 | double x, y; 49 | int code; 50 | if (code1 != 0) 51 | code = code1; 52 | else 53 | code = code2; 54 | 55 | if (code & 8) 56 | { 57 | x = x1 + (x2 - x1) * (y_max - y1) / (y2 - y1); 58 | y = y_max; 59 | } 60 | else if (code & 4) 61 | { 62 | x = x1 + (x2 - x1) * (y_min - y1) / (y2 - y1); 63 | y = y_min; 64 | } 65 | else if (code & 2) 66 | { 67 | y = y1 + (y2 - y1) * (x_max - x1) / (x2 - x1); 68 | x = x_max; 69 | } 70 | else if (code & 1) 71 | { 72 | y = y1 + (y2 - y1) * (x_min - x1) / (x2 - x1); 73 | x = x_min; 74 | } 75 | 76 | if (code == code1) 77 | { 78 | x1 = x; 79 | y1 = y; 80 | code1 = getRegionCode(x1, y1,x_min,x_max,y_min,y_max); 81 | } 82 | else 83 | { 84 | x2 = x; 85 | y2 = y; 86 | code2 = getRegionCode(x2, y2,x_min,x_max,y_min,y_max); 87 | } 88 | } 89 | 90 | } 91 | cout <<"\tLine clipped from P(" << x1 << "," << y1 << ") to Q("<< x2 << "," << y2 << ")."<< endl; 92 | 93 | } 94 | 95 | return "clipped"; 96 | } 97 | 98 | 99 | int main() 100 | { 101 | cout<<"\n\t *****************Cohen Sutherland Line Clipping Algorithm *****************\n\n"; 102 | 103 | int gd = DETECT, gm; 104 | initgraph(&gd, &gm, (char*)""); 105 | double x_max,y_max,x_min,y_min; 106 | double px,py,qx,qy; 107 | 108 | cout<<"\tEnter the window coordinates : \n"; 109 | cout<<"\tx_min : "; 110 | cin>>x_min; 111 | cout<<"\ty_min : "; 112 | cin>>y_min; 113 | cout<<"\tx_max : "; 114 | cin>>x_max; 115 | cout<<"\tx_max : "; 116 | cin>>y_max; 117 | 118 | cout <<"\n\tWindow Coordinates:"; 119 | cout <<"\n\tx_min= " << x_min << " , y_min= " << y_min << " , x_max= "<< x_max << " , y_max= " << y_max << "\n" << endl; 120 | 121 | cout<<"\tEnter the line coordinates : \n"; 122 | cout<<"\tPx : "; 123 | cin>>px; 124 | cout<<"\tPy : "; 125 | cin>>py; 126 | cout<<"\tQx : "; 127 | cin>>qx; 128 | cout<<"\tQy : "; 129 | cin>>qy; 130 | 131 | cout <<"\n\tLine P(" << px << "," << py << ") to Q("<< qx << "," << qy << ") \n"<< endl; 132 | 133 | int code1=getRegionCode(px,py,x_min,x_max,y_min,y_max); 134 | int code2=getRegionCode(qx,qy,x_min,x_max,y_min,y_max); 135 | 136 | string result=CSAlgorithm(code1,code2,px,py,qx,qy,x_min,x_max,y_min,y_max); 137 | 138 | 139 | if(result=="inside"){ 140 | cout <<"\n\tLine P(" << px << "," << py << ") to Q("<< qx << "," << qy << ") is inside the window coordinates. No need to clip"<< endl; 141 | // draw the output line and the window 142 | line(px,py,qx,qy); 143 | rectangle(x_min,y_min,x_max,y_max); 144 | } 145 | else if(result=="outside"){ 146 | cout <<"\n\tLine P(" << px << "," << py << ") to Q("<< qx << "," << qy << ") is outside the window coordinates. No need to clip"<< endl; 147 | // draw the output line and the window 148 | line(px,py,qx,qy); 149 | rectangle(x_min,y_min,x_max,y_max); 150 | } else{ 151 | cout <<"\n\tLine P(" << px << "," << py << ") to Q("<< qx << "," << qy << ") is partially inside and needs to be clipped"<< endl; 152 | // draw the output line and the window 153 | line(px,py,qx,qy); 154 | rectangle(x_min,y_min,x_max,y_max); 155 | } 156 | // if(result=="outside") 157 | // cout <<"\n\tLine P(" << px << "," << py << ") to Q("<< qx << "," << qy << ") is outside the window coordinates."<< endl; 158 | getch(); 159 | closegraph(); 160 | return 0; 161 | } -------------------------------------------------------------------------------- /sutherland_hodgeman.cpp: -------------------------------------------------------------------------------- 1 | //Coordinates of rectangular clip window : 2 | //xmin,ymin :150 162 3 | //xmax,ymax :200 174 4 | // 5 | // 6 | //Polygon to be clipped : 7 | //Number of sides :3 8 | //Enter the coordinates :100 150 9 | //200 250 10 | //300 200 11 | // UNCLIPPED POLYGON CLIPPED POLYGON 12 | 13 | 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | using namespace std; 20 | 21 | #define round(a) ((int)(a+0.5)) 22 | int k; 23 | float xmin,ymin,xmax,ymax,arr[20],m; 24 | void clipl(float x1,float y1,float x2,float y2) 25 | { 26 | if(x2-x1) 27 | m=(y2-y1)/(x2-x1); 28 | else 29 | m=100000; 30 | if(x1 >= xmin && x2 >= xmin) 31 | { 32 | arr[k]=x2; 33 | arr[k+1]=y2; 34 | k+=2; 35 | } 36 | if(x1 < xmin && x2 >= xmin) 37 | { 38 | arr[k]=xmin; 39 | arr[k+1]=y1+m*(xmin-x1); 40 | arr[k+2]=x2; 41 | arr[k+3]=y2; 42 | k+=4; 43 | } 44 | if(x1 >= xmin && x2 < xmin) 45 | { 46 | arr[k]=xmin; 47 | arr[k+1]=y1+m*(xmin-x1); 48 | k+=2; 49 | } 50 | } 51 | 52 | void clipt(float x1,float y1,float x2,float y2) 53 | { 54 | if(y2-y1) 55 | m=(x2-x1)/(y2-y1); 56 | else 57 | m=100000; 58 | if(y1 <= ymax && y2 <= ymax) 59 | { 60 | arr[k]=x2; 61 | arr[k+1]=y2; 62 | k+=2; 63 | } 64 | if(y1 > ymax && y2 <= ymax) 65 | { 66 | arr[k]=x1+m*(ymax-y1); 67 | arr[k+1]=ymax; 68 | arr[k+2]=x2; 69 | arr[k+3]=y2; 70 | k+=4; 71 | } 72 | if(y1 <= ymax && y2 > ymax) 73 | { 74 | arr[k]=x1+m*(ymax-y1); 75 | arr[k+1]=ymax; 76 | k+=2; 77 | } 78 | } 79 | 80 | void clipr(float x1,float y1,float x2,float y2) 81 | { 82 | if(x2-x1) 83 | m=(y2-y1)/(x2-x1); 84 | else 85 | m=100000; 86 | if(x1 <= xmax && x2 <= xmax) 87 | { 88 | arr[k]=x2; 89 | arr[k+1]=y2; 90 | k+=2; 91 | } 92 | if(x1 > xmax && x2 <= xmax) 93 | { 94 | arr[k]=xmax; 95 | arr[k+1]=y1+m*(xmax-x1); 96 | arr[k+2]=x2; 97 | arr[k+3]=y2; 98 | k+=4; 99 | } 100 | if(x1 <= xmax && x2 > xmax) 101 | { 102 | arr[k]=xmax; 103 | arr[k+1]=y1+m*(xmax-x1); 104 | k+=2; 105 | } 106 | } 107 | 108 | void clipb(float x1,float y1,float x2,float y2) 109 | { 110 | if(y2-y1) 111 | m=(x2-x1)/(y2-y1); 112 | else 113 | m=100000; 114 | if(y1 >= ymin && y2 >= ymin) 115 | { 116 | arr[k]=x2; 117 | arr[k+1]=y2; 118 | k+=2; 119 | } 120 | if(y1 < ymin && y2 >= ymin) 121 | { 122 | arr[k]=x1+m*(ymin-y1); 123 | arr[k+1]=ymin; 124 | arr[k+2]=x2; 125 | arr[k+3]=y2; 126 | k+=4; 127 | } 128 | if(y1 >= ymin && y2 < ymin) 129 | { 130 | arr[k]=x1+m*(ymin-y1); 131 | arr[k+1]=ymin; 132 | k+=2; 133 | } 134 | } 135 | 136 | int main() 137 | { 138 | int gdriver=DETECT,gmode,n,poly[20], i; 139 | float xi,yi,xf,yf,polyy[20]; 140 | system("cls"); 141 | cout<<"Coordinates of rectangular clip window :\nxmin,ymin :"; 142 | cin>>xmin>>ymin; 143 | cout<<"xmax,ymax :"; 144 | cin>>xmax>>ymax; 145 | cout<<"\n\nPolygon to be clipped :\nNumber of sides :"; 146 | cin>>n; 147 | cout<<"Enter the coordinates :"; 148 | for(i=0;i < 2*n;i++) 149 | cin>>polyy[i]; 150 | polyy[i]=polyy[0]; 151 | polyy[i+1]=polyy[1]; 152 | for(i=0;i < 2*n+2;i++) 153 | poly[i]=round(polyy[i]); 154 | initgraph(&gdriver,&gmode,(char*)""); 155 | setcolor(RED); 156 | rectangle(xmin,ymax,xmax,ymin); 157 | cout<<"\t\tUNCLIPPED POLYGON"; 158 | setcolor(WHITE); 159 | fillpoly(n,poly); 160 | getch(); 161 | cleardevice(); 162 | k=0; 163 | for(i=0;i < 2*n;i+=2) 164 | clipl(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]); 165 | n=k/2; 166 | for(i=0;i < k;i++) 167 | polyy[i]=arr[i]; 168 | polyy[i]=polyy[0]; 169 | polyy[i+1]=polyy[1]; 170 | k=0; 171 | for(i=0;i < 2*n;i+=2) 172 | clipt(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]); 173 | n=k/2; 174 | for(i=0;i < k;i++) 175 | polyy[i]=arr[i]; 176 | polyy[i]=polyy[0]; 177 | polyy[i+1]=polyy[1]; 178 | k=0; 179 | for(i=0;i < 2*n;i+=2) 180 | clipr(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]); 181 | n=k/2; 182 | for(i=0;i < k;i++) 183 | polyy[i]=arr[i]; 184 | polyy[i]=polyy[0]; 185 | polyy[i+1]=polyy[1]; 186 | k=0; 187 | for(i=0;i < 2*n;i+=2) 188 | clipb(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]); 189 | for(i=0;i < k;i++) 190 | poly[i]=round(arr[i]); 191 | if(k) 192 | fillpoly(k/2,poly); 193 | setcolor(RED); 194 | rectangle(xmin,ymax,xmax,ymin); 195 | cout<<"\tCLIPPED POLYGON"; 196 | getch(); 197 | closegraph(); 198 | return 0; 199 | } 200 | -------------------------------------------------------------------------------- /hermite.cpp: -------------------------------------------------------------------------------- 1 | //hermite.cpp 2 | 3 | /* 4 | https://people.sc.fsu.edu/~jburkardt/cpp_src/hermite_cubic/hermite_cubic.html 5 | */ 6 | 7 | 8 | 9 | # include 10 | # include 11 | # include 12 | # include 13 | # include 14 | using namespace std; 15 | double hermite_cubic_integral ( double x1, double f1, double d1, double x2, 16 | double f2, double d2 ); 17 | double hermite_cubic_integrate ( double x1, double f1, double d1, double x2, 18 | double f2, double d2, double a, double b ); 19 | double *hermite_cubic_lagrange_integral ( double x1, double x2 ); 20 | double *hermite_cubic_lagrange_integrate ( double x1, double x2, double a, 21 | double b ); 22 | void hermite_cubic_lagrange_value ( double x1, double x2, int n, double x[], 23 | double f[], double d[], double s[], double t[] ); 24 | double hermite_cubic_spline_integral ( int nn, double xn[], double fn[], 25 | double dn[] ); 26 | double *hermite_cubic_spline_integrate ( int nn, double xn[], double fn[], 27 | double dn[], int n, double a[], double b[] ); 28 | double *hermite_cubic_spline_quad_rule ( int nn, double xn[] ); 29 | void hermite_cubic_spline_value ( int nn, double xn[], double fn[], 30 | double dn[], int n, double x[], double f[], double d[], double s[], 31 | double t[] ); 32 | void hermite_cubic_to_power_cubic ( double x1, double f1, double d1, double x2, 33 | double f2, double d2, double *c0, double *c1, double *c2, double *c3 ); 34 | void hermite_cubic_value ( double x1, double f1, double d1, double x2, 35 | double f2, double d2, int n, double x[], double f[], double d[], 36 | double s[], double t[] ); 37 | void power_cubic_to_hermite_cubic ( double c0, double c1, double c2, double c3, 38 | double x1, double x2, double *f1, double *d1, double *f2, double *d2 ); 39 | double r8_uniform_01 ( int *seed ); 40 | void r8vec_bracket3 ( int n, double t[], double tval, int *left ); 41 | double *r8vec_even_new ( int n, double alo, double ahi ); 42 | double *r8vec_uniform_01_new ( int n, int *seed ); 43 | void timestamp ( ); 44 | double hermite_cubic_integral ( double x1, double f1, double d1, double x2, double f2, double d2 ) 45 | { 46 | double h; 47 | double q; 48 | h = x2 - x1; 49 | q = 0.5 * h * ( f1 + f2 + h * ( d1 - d2 ) / 6.0 ); 50 | return q; 51 | } 52 | double hermite_cubic_integrate ( double x1, double f1, double d1, double x2, double f2, double d2, double a, double b ) 53 | { 54 | double dterm; 55 | double fterm; 56 | double h; 57 | double phia1; 58 | double phia2; 59 | double phib1; 60 | double phib2; 61 | double psia1; 62 | double psia2; 63 | double psib1; 64 | double psib2; 65 | double q; 66 | double ta1; 67 | double ta2; 68 | double tb1; 69 | double tb2; 70 | double ua1; 71 | double ua2; 72 | double ub1; 73 | double ub2; 74 | h = x2 - x1; 75 | ta1 = ( a - x1 ) / h; 76 | ta2 = ( x2 - a ) / h; 77 | tb1 = ( b - x1 ) / h; 78 | tb2 = ( x2 - b ) / h; 79 | ua1 = ta1 * ta1 * ta1; 80 | phia1 = ua1 * ( 2.0 - ta1 ); 81 | psia1 = ua1 * ( 3.0 * ta1 - 4.0 ); 82 | ua2 = ta2 * ta2 * ta2; 83 | phia2 = ua2 * ( 2.0 - ta2 ); 84 | psia2 = -ua2 * ( 3.0 * ta2 - 4.0 ); 85 | ub1 = tb1 * tb1 * tb1; 86 | phib1 = ub1 * ( 2.0 - tb1 ); 87 | psib1 = ub1 * ( 3.0 * tb1 - 4.0 ); 88 | ub2 = tb2 * tb2 * tb2; 89 | phib2 = ub2 * ( 2.0 - tb2 ); 90 | psib2 = -ub2 * ( 3.0 * tb2 - 4.0 ); 91 | fterm = f1 * ( phia2 - phib2 ) + f2 * ( phib1 - phia1 ); 92 | dterm = ( d1 * ( psia2 - psib2 ) + d2 * ( psib1 - psia1 ) ) * ( h / 6.0 ); 93 | q = 0.5 * h * ( fterm + dterm ); 94 | return q; 95 | } 96 | double *hermite_cubic_lagrange_integral ( double x1, double x2 ) 97 | { 98 | double h; 99 | double *q; 100 | q = new double[4]; 101 | h = x2 - x1; 102 | q[0] = h / 2.0; 103 | q[1] = h * h / 12.0; 104 | q[2] = h / 2.0; 105 | q[3] = - h * h / 12.0; 106 | return q; 107 | } 108 | double *hermite_cubic_lagrange_integrate ( double x1, double x2, double a, double b ) 109 | { 110 | double h; 111 | double phia1; 112 | double phia2; 113 | double phib1; 114 | double phib2; 115 | double psia1; 116 | double psia2; 117 | double psib1; 118 | double psib2; 119 | double *q; 120 | double ta1; 121 | double ta2; 122 | double tb1; 123 | double tb2; 124 | double ua1; 125 | double ua2; 126 | double ub1; 127 | double ub2; 128 | h = x2 - x1; 129 | ta1 = ( a - x1 ) / h; 130 | ta2 = ( x2 - a ) / h; 131 | tb1 = ( b - x1 ) / h; 132 | tb2 = ( x2 - b ) / h; 133 | ua1 = ta1 * ta1 * ta1; 134 | phia1 = ua1 * ( 2.0 - ta1 ); 135 | psia1 = ua1 * ( 3.0 * ta1 - 4.0 ); 136 | ua2 = ta2 * ta2 * ta2; 137 | phia2 = ua2 * ( 2.0 - ta2 ); 138 | psia2 = -ua2 * ( 3.0 * ta2 - 4.0 ); 139 | ub1 = tb1 * tb1 * tb1; 140 | phib1 = ub1 * ( 2.0 - tb1 ); 141 | psib1 = ub1 * ( 3.0 * tb1 - 4.0 ); 142 | ub2 = tb2 * tb2 * tb2; 143 | phib2 = ub2 * ( 2.0 - tb2 ); 144 | psib2 = -ub2 * ( 3.0 * tb2 - 4.0 ); 145 | q = new double[4]; 146 | q[0] = 0.5 * h * ( phia2 - phib2 ); 147 | q[1] = 0.5 * h * ( psia2 - psib2 ) * ( h / 6.0 ); 148 | q[2] = 0.5 * h * ( phib1 - phia1 ); 149 | q[3] = 0.5 * h * ( psib1 - psia1 ) * ( h / 6.0 ); 150 | return q; 151 | } 152 | void hermite_cubic_lagrange_value ( double x1, double x2, int n, double x[], double f[], double d[], double s[], double t[] ) 153 | { 154 | double dx; 155 | double h; 156 | int j; 157 | h = x2 - x1; 158 | for ( j = 0; j < n; j++ ) 159 | { 160 | dx = x[j] - x1; 161 | f[0+j*4] = 1.0 + ( ( dx * dx ) / ( h * h ) ) * ( - 3.0 + ( dx / h ) * 2.0 ); 162 | d[0+j*4] = ( dx / ( h * h ) ) * ( - 6.0 + ( dx / h ) * 6.0 ); 163 | s[0+j*4] = ( 1.0 / ( h * h ) ) * ( - 6.0 + ( dx / h ) * 12.0 ); 164 | t[0+j*4] = ( 1.0 / ( h * h * h ) ) * 12.0; 165 | f[1+j*4] = dx + ( ( dx * dx ) / h ) * ( - 2.0 + ( dx / h ) ); 166 | d[1+j*4] = 1.0 + ( dx / h ) * ( - 4.0 + ( dx / h ) * 3.0 ); 167 | s[1+j*4] = ( 1.0 / h ) * ( - 4.0 + ( dx / h ) * 6.0 ); 168 | t[1+j*4] = ( 1.0 / ( h * h ) ) * 6.0; 169 | f[2+j*4] = ( ( dx * dx ) / ( h * h ) ) * ( 3.0 - 2.0 * ( dx / h ) ); 170 | d[2+j*4] = ( dx / ( h * h ) ) * ( 6.0 - 6.0 * ( dx / h ) ); 171 | s[2+j*4] = ( 1.0 / ( h * h ) ) * ( 6.0 - 12.0 * ( dx / h ) ); 172 | t[2+j*4] = ( 1.0 / ( h * h * h ) ) * ( - 12.0 ); 173 | f[3+j*4] = ( ( dx * dx ) / h ) * ( - 1.0 + ( dx / h ) ); 174 | d[3+j*4] = ( dx / h ) * ( - 2.0 + ( dx / h ) * 3.0 ); 175 | s[3+j*4] = ( 1.0 / h ) * ( - 2.0 + ( dx / h ) * 6.0 ); 176 | t[3+j*4] = ( 1.0 / h ) * 6.0; 177 | } 178 | return; 179 | } 180 | double hermite_cubic_spline_integral ( int nn, double xn[], double fn[], double dn[] ) 181 | { 182 | int i; 183 | double q; 184 | q = 0.0; 185 | for ( i = 0; i < nn - 1; i++ ) 186 | { 187 | q = q + 188 | 0.5 * ( xn[i+1] - xn[i] ) * ( fn[i] + fn[i+1] 189 | + ( xn[i+1] - xn[i] ) * ( dn[i] - dn[i+1] ) / 6.0 ); 190 | } 191 | return q; 192 | } 193 | double *hermite_cubic_spline_integrate ( int nn, double xn[], double fn[], double dn[], int n, double a[], double b[] ) 194 | { 195 | double aa; 196 | double bb; 197 | int i; 198 | int ii; 199 | int j; 200 | int k; 201 | double *q; 202 | double s; 203 | q = new double[n]; 204 | i = n / 2; 205 | j = n / 2; 206 | for ( ii = 0; ii < n; ii++ ) 207 | { 208 | q[ii] = 0.0; 209 | if ( a[ii] <= b[ii] ) 210 | { 211 | aa = a[ii]; 212 | bb = b[ii]; 213 | s = + 1.0; 214 | } 215 | else 216 | { 217 | aa = b[ii]; 218 | bb = a[ii]; 219 | s = - 1.0; 220 | } 221 | r8vec_bracket3 ( nn, xn, aa, &i ); 222 | r8vec_bracket3 ( nn, xn, bb, &j ); 223 | if ( i == j ) 224 | { 225 | q[ii] = hermite_cubic_integrate ( xn[i], fn[i], dn[i], 226 | xn[i+1], fn[i+1], dn[i+1], aa, bb ); 227 | } 228 | else 229 | { 230 | q[ii] = hermite_cubic_integrate ( xn[i], fn[i], dn[i], 231 | xn[i+1], fn[i+1], dn[i+1], aa, xn[i+1] ); 232 | for ( k = i + 1; k < j; k++ ) 233 | { 234 | q[ii] = q[ii] + hermite_cubic_integral ( xn[k], fn[k], dn[k], 235 | xn[k+1], fn[k+1], dn[k+1] ); 236 | } 237 | q[ii] = q[ii] + hermite_cubic_integrate ( xn[j], fn[j], dn[j], 238 | xn[j+1], fn[j+1], dn[j+1], xn[j], bb ); 239 | } 240 | q[ii] = s * q[ii]; 241 | } 242 | return q; 243 | } 244 | double *hermite_cubic_spline_quad_rule ( int nn, double xn[] ) 245 | { 246 | int j; 247 | double *w; 248 | w = new double[2*nn]; 249 | w[0+0*2] = 0.5 * ( xn[1] - xn[0] ); 250 | for ( j = 1; j < nn - 1; j++ ) 251 | { 252 | w[0+j*2] = 0.5 * ( xn[j+1] - xn[j-1] ); 253 | } 254 | w[0+(nn-1)*2] = 0.5 * ( xn[nn-1] - xn[nn-2] ); 255 | w[1+0*2] = pow ( xn[1] - xn[0], 2 ) / 12.0; 256 | for ( j = 1; j < nn - 1; j++ ) 257 | { 258 | w[1+j*2] = ( xn[j+1] - xn[j-1] ) 259 | * ( xn[j+1] - 2.0 * xn[j] + xn[j-1] ) / 12.0; 260 | } 261 | w[1+(nn-1)*2] = - pow ( xn[nn-2] - xn[nn-1], 2 ) / 12.0; 262 | return w; 263 | } 264 | void hermite_cubic_spline_value ( int nn, double xn[], double fn[], double dn[], int n, double x[], double f[], double d[], double s[], double t[] ) 265 | { 266 | int i; 267 | int left; 268 | left = n / 2; 269 | for ( i = 0; i < n; i++ ) 270 | { 271 | r8vec_bracket3 ( nn, xn, x[i], &left ); 272 | hermite_cubic_value ( xn[left], fn[left], dn[left], xn[left+1], 273 | fn[left+1], dn[left+1], 1, x+i, f+i, d+i, s+i, t+i ); 274 | } 275 | return; 276 | } 277 | void hermite_cubic_to_power_cubic ( double x1, double f1, double d1, double x2, double f2, double d2, double *c0, double *c1, double *c2, double *c3 ) 278 | { 279 | double df; 280 | double h; 281 | h = x2 - x1; 282 | df = ( f2 - f1 ) / h; 283 | *c0 = f1; 284 | *c1 = d1; 285 | *c2 = - ( 2.0 * d1 - 3.0 * df + d2 ) / h; 286 | *c3 = ( d1 - 2.0 * df + d2 ) / h / h; 287 | *c2 = *c2 - x1 * *c3; 288 | *c1 = *c1 - x1 * *c2; 289 | *c0 = *c0 - x1 * *c1; 290 | *c2 = *c2 - x1 * *c3; 291 | *c1 = *c1 - x1 * *c2; 292 | *c2 = *c2 - x1 * *c3; 293 | return; 294 | } 295 | void hermite_cubic_value ( double x1, double f1, double d1, double x2, double f2, double d2, int n, double x[], double f[], double d[], double s[], double t[] ) 296 | { 297 | double c2; 298 | double c3; 299 | double df; 300 | double h; 301 | int i; 302 | h = x2 - x1; 303 | df = ( f2 - f1 ) / h; 304 | c2 = - ( 2.0 * d1 - 3.0 * df + d2 ) / h; 305 | c3 = ( d1 - 2.0 * df + d2 ) / h / h; 306 | for ( i = 0; i < n; i++ ) 307 | { 308 | f[i] = f1 + ( x[i] - x1 ) * ( d1 309 | + ( x[i] - x1 ) * ( c2 310 | + ( x[i] - x1 ) * c3 ) ); 311 | d[i] = d1 + ( x[i] - x1 ) * ( 2.0 * c2 312 | + ( x[i] - x1 ) * 3.0 * c3 ); 313 | s[i] = 2.0 * c2 + ( x[i] - x1 ) * 6.0 * c3; 314 | t[i] = 6.0 * c3; 315 | } 316 | return; 317 | } 318 | void power_cubic_to_hermite_cubic ( double c0, double c1, double c2, double c3, double x1, double x2, double *f1, double *d1, double *f2, double *d2 ) 319 | { 320 | *f1 = c0 + x1 * ( c1 + x1 * ( c2 + x1 * c3 ) ); 321 | *d1 = c1 + x1 * ( 2.0 * c2 + x1 * 3.0 * c3 ); 322 | *f2 = c0 + x2 * ( c1 + x2 * ( c2 + x2 * c3 ) ); 323 | *d2 = c1 + x2 * ( 2.0 * c2 + x2 * 3.0 * c3 ); 324 | return; 325 | } 326 | double r8_uniform_01 ( int *seed ) 327 | { 328 | int i4_huge = 2147483647; 329 | int k; 330 | double r; 331 | if ( *seed == 0 ) 332 | { 333 | std::cerr << "\n"; 334 | std::cerr << "R8_UNIFORM_01 - Fatal error!\n"; 335 | std::cerr << " Input value of SEED = 0.\n"; 336 | std::exit ( 1 ); 337 | } 338 | k = *seed / 127773; 339 | *seed = 16807 * ( *seed - k * 127773 ) - k * 2836; 340 | if ( *seed < 0 ) 341 | { 342 | *seed = *seed + i4_huge; 343 | } 344 | r = ( double ) ( *seed ) * 4.656612875E-10; 345 | return r; 346 | } 347 | void r8vec_bracket3 ( int n, double t[], double tval, int *left ) 348 | { 349 | int high; 350 | int low; 351 | int mid; 352 | if ( n < 2 ) 353 | { 354 | std::cerr << "\n"; 355 | std::cerr << "R8VEC_BRACKET3 - Fatal error!\n"; 356 | std::cerr << " N must be at least 2.\n"; 357 | std::exit ( 1 ); 358 | } 359 | if ( *left < 0 || n - 2 < *left ) 360 | { 361 | *left = ( n - 1 ) / 2; 362 | } 363 | if ( tval < t[*left] ) 364 | { 365 | if ( *left == 0 ) 366 | { 367 | return; 368 | } 369 | else if ( *left == 1 ) 370 | { 371 | *left = 0; 372 | return; 373 | } 374 | else if ( t[*left-1] <= tval ) 375 | { 376 | *left = *left - 1; 377 | return; 378 | } 379 | else if ( tval <= t[1] ) 380 | { 381 | *left = 0; 382 | return; 383 | } 384 | low = 1; 385 | high = *left - 2; 386 | 387 | for ( ; ; ) 388 | { 389 | if ( low == high ) 390 | { 391 | *left = low; 392 | return; 393 | } 394 | 395 | mid = ( low + high + 1 ) / 2; 396 | 397 | if ( t[mid] <= tval ) 398 | { 399 | low = mid; 400 | } 401 | else 402 | { 403 | high = mid - 1; 404 | } 405 | } 406 | } 407 | else if ( t[*left+1] < tval ) 408 | { 409 | if ( *left == n - 2 ) 410 | { 411 | return; 412 | } 413 | else if ( *left == n - 3 ) 414 | { 415 | *left = *left + 1; 416 | return; 417 | } 418 | else if ( tval <= t[*left+2] ) 419 | { 420 | *left = *left + 1; 421 | return; 422 | } 423 | else if ( t[n-2] <= tval ) 424 | { 425 | *left = n - 2; 426 | return; 427 | } 428 | low = *left + 2; 429 | high = n - 3; 430 | 431 | for ( ; ; ) 432 | { 433 | if ( low == high ) 434 | { 435 | *left = low; 436 | return; 437 | } 438 | 439 | mid = ( low + high + 1 ) / 2; 440 | 441 | if ( t[mid] <= tval ) 442 | { 443 | low = mid; 444 | } 445 | else 446 | { 447 | high = mid - 1; 448 | } 449 | } 450 | } 451 | else 452 | { 453 | } 454 | return; 455 | } 456 | double *r8vec_even_new ( int n, double alo, double ahi ) 457 | { 458 | double *a; 459 | int i; 460 | 461 | a = new double[n]; 462 | 463 | if ( n == 1 ) 464 | { 465 | a[0] = 0.5 * ( alo + ahi ); 466 | } 467 | else 468 | { 469 | for ( i = 0; i < n; i++ ) 470 | { 471 | a[i] = ( ( double ) ( n - i - 1 ) * alo 472 | + ( double ) ( i ) * ahi ) 473 | / ( double ) ( n - 1 ); 474 | } 475 | } 476 | 477 | return a; 478 | } 479 | double *r8vec_uniform_01_new ( int n, int *seed ) 480 | { 481 | int i; 482 | int i4_huge = 2147483647; 483 | int k; 484 | double *r; 485 | 486 | if ( *seed == 0 ) 487 | { 488 | cerr << "\n"; 489 | cerr << "R8VEC_UNIFORM_01_NEW - Fatal error!\n"; 490 | cerr << " Input value of SEED = 0.\n"; 491 | exit ( 1 ); 492 | } 493 | r = new double[n]; 494 | for ( i = 0; i < n; i++ ) 495 | { 496 | k = *seed / 127773; 497 | *seed = 16807 * ( *seed - k * 127773 ) - k * 2836; 498 | if ( *seed < 0 ) 499 | { 500 | *seed = *seed + i4_huge; 501 | } 502 | r[i] = ( double ) ( *seed ) * 4.656612875E-10; 503 | } 504 | return r; 505 | } 506 | void timestamp ( ) 507 | { 508 | # define TIME_SIZE 40 509 | static char time_buffer[TIME_SIZE]; 510 | const struct std::tm *tm_ptr; 511 | std::time_t now; 512 | now = std::time ( NULL ); 513 | tm_ptr = std::localtime ( &now ); 514 | std::strftime ( time_buffer, TIME_SIZE, "%d %B %Y %I:%M:%S %p", tm_ptr ); 515 | std::cout << time_buffer << "\n"; 516 | return; 517 | # undef TIME_SIZE 518 | } 519 | --------------------------------------------------------------------------------