Add to Favorites    Make Home Page 5421 Online  
 Language Categories  
 Our Services  

Home » C++ Home » Computer Graphics Home » Program to implement Polygon Clipping Algorithm.

A D V E R T I S E M E N T

Search Projects & Source Codes:

Title Program to implement Polygon Clipping Algorithm.
Description
Category C++ » Computer Graphics
Hits 375575
Code Select and Copy the Code
#include<iostream.h> #include<conio.h> #include<math.h> #include<graphics.h> #include<dos.h> #include<process.h> int pixels[2][4]; float xn1,xn2,yn1,yn2,x3,y3,m; void show_quadrant() { cleardevice(); rectangle(120,40,320,240); rectangle(320,40,520,240); rectangle(120,240,320,440); rectangle(320,240,520,440); for(int i=130;i<=510;i+=10) { if(i==320) continue; outtextxy(i,237,"+"); } for(i=50;i<=430;i+=10) { if(i==240) continue; outtextxy(317,i,"-"); } outtextxy(310,230,"O"); outtextxy(530,240,"X"); outtextxy(320,450,"-Y"); outtextxy(100,240,"-X"); outtextxy(320,30,"Y"); } void midpt(int x1,int y1,int x2,int y2,int xmin,int ymin,int xmax,int ymax) { int fl=1; int pixels[2][4],i,j; for(i=0;i<2;i++) for(j=0;j<4;j++) pixels[i][j]=0; if(y1>ymax) pixels[0][0]=1; if(y1<ymin) pixels[0][1]=1; if(x1>xmax) pixels[0][2]=1; if(x1<xmin) pixels[0][3]=1; if(y2>ymax) pixels[1][0]=1; if(y2<ymin) pixels[1][1]=1; if(x2>xmax) pixels[1][2]=1; if(x2<xmin) pixels[1][3]=1; for(j=0;j<4;j++) { if(pixels[0][j]==0&&pixels[1][j]==0) continue; if(pixels[0][j]==1&&pixels[1][j]==1) { fl=3; break; } fl=2; } switch(fl) { case 1: line(320+x1,240-y1,320+x2,240-y2); break; case 3: cout<<" a" Line Is Not Visible...:-("; break; case 2: int ox1=x1,ox2=x2,oy1=y1,oy2=y2; xn1=x1; yn1=y1; xn2=x2; yn2=y2; fl=0; x3=x1; y3=y1; while(1) { if(!(y1>ymax || y1<ymin || x1>xmax || x1<xmin) && (x3 || y3)!=0.1) break; x3=(x1+x2)/2.0; y3=(y1+y2)/2.0; if(!(y3>ymax || y3<ymin || x3>xmax || x3<xmin)) fl=1; else fl=0; if(fl) { x2=x3; y2=y3; } else { x1=x3; y1=y3; } } xn1=x3; yn1=y3; fl=0; x1=ox1; x2=ox2; y1=oy1; y2=oy2; x3=x2; y3=y2; while(1) { if(!(y2>ymax || y2<ymin || x2>xmax || x2<xmin) && (x3 || y3)!=0.1) break; x3=(x1+x2)/2.0; y3=(y1+y2)/2.0; if(!(y3>ymax || y3<ymin || x3>xmax || x3<xmin)) fl=1; else fl=0; if(fl) { x1=x3; y1=y3; } else { x2=x3; y2=y3; } } xn2=x3; yn2=y3; line(320+xn1,240-yn1,320+xn2,240-yn2); break; } } void show_message() { char *mess[]={"-","=","["," ","P","o","l","y","g","o","n"," ", "C","l","i","p","p","i","n","g"," ","]","=","-"}; int xx=28,xxx=52,i,j; _setcursortype(_NOCURSOR); for(i=0,j=24;i<16,j>=12;i++,j--) { gotoxy(xx,1); cout<<mess[i]; xx++; gotoxy(xxx,1); cout<<mess[j]; xxx--; delay(50); } _setcursortype(_NORMALCURSOR); } void main() { clrscr(); int gd=DETECT,gm,i,j; int xmin,ymin,xmax,ymax,x[10],y[10],ver; int choice,ed[20],num; show_message(); cout<<" " Enter The Co-Ordinates Of The Clipping Window.""; cout<<" " Enter X(min) & Y(min) ":="; cin>>xmin>>ymin; cout<<" " Enter X(max) & Y(max) ":="; cin>>xmax>>ymax; cout<<" " Enter The No Of Vertices Of Polygon ":="; cin>>ver; for(i=0;i<ver;i++) { cout<<" " Vertex X["<<i+1<<"] Y["<<i+1<<"] ":="; cin>>x[i]>>y[i]; } x[ver]=x[0]; y[ver]=y[0]; clrscr(); initgraph(&gd,&gm,"..\bgi"); clearviewport(); show_quadrant(); line(320+xmin,240-ymin,320+xmin,240-ymax); line(320+xmin,240-ymax,320+xmax,240-ymax); line(320+xmax,240-ymax,320+xmax,240-ymin); line(320+xmax,240-ymin,320+xmin,240-ymin); for(i=0;i<ver;i++) line(320+x[i],240-y[i],320+x[i+1],240-y[i+1]); getch(); cleardevice(); show_quadrant(); line(320+xmin,240-ymin,320+xmin,240-ymax); line(320+xmin,240-ymax,320+xmax,240-ymax); line(320+xmax,240-ymax,320+xmax,240-ymin); line(320+xmax,240-ymin,320+xmin,240-ymin); for(i=0;i<ver;i++) { midpt(x[i],y[i],x[i+1],y[i+1],xmin,ymin,xmax,ymax); } getch(); closegraph(); }

Related Source Codes

Script Name Author
Moving ball screen saver karlmarx
The Classic Game of Snake & Ladder Lakshmi Narayana .A
Railway seat reservation question which comes in sapient VyomWorld
To calculate percentile Ravi Mathur
Send to folder ANIMESH SAHU
Analog clock and calendar Nazia & Rida
HIGH/LOW GAME MOLLY ARORA
Data structure (stack Implimentation) Swapnil B Adsure
Memory Game AnirudhSanyal
Easy Calc Anirudh Sanyal
GK Quiz Anirudh Sanyal
Hangman Game Manish Jain
Snakeman Manish Jain
Full month Calendar Nigi
Cursor shapes nigi

A D V E R T I S E M E N T




Google Groups Subscribe to SourceCodesWorld - Techies Talk
Email:

Free eBook - Interview Questions: Get over 1,000 Interview Questions in an eBook for free when you join JobsAssist. Just click on the button below to join JobsAssist and you will immediately receive the Free eBook with thousands of Interview Questions in an ebook when you join.

New! Click here to Add your Code!


ASP Home | C Home | C++ Home | COBOL Home | Java Home | Pascal Home
Source Codes Home Page

 Advertisements  

Google Search

Google

Source Codes World.com is a part of Vyom Network.

Vyom Network : Web Hosting | Dedicated Server | Free SMS, GRE, GMAT, MBA | Online Exams | Freshers Jobs | Software Downloads | Interview Questions | Jobs, Discussions | Placement Papers | Free eBooks | Free eBooks | Free Business Info | Interview Questions | Free Tutorials | Arabic, French, German | IAS Preparation | Jokes, Songs, Fun | Free Classifieds | Free Recipes | Free Downloads | Bangalore Info | Tech Solutions | Project Outsourcing, Web Hosting | GATE Preparation | MBA Preparation | SAP Info | Software Testing | Google Logo Maker | Freshers Jobs

Sitemap | Privacy Policy | Terms and Conditions | Important Websites
Copyright ©2003-2024 SourceCodesWorld.com, All Rights Reserved.
Page URL: http://www.sourcecodesworld.com/source/show.asp?ScriptID=115


Download Yahoo Messenger | Placement Papers | Free SMS | C Interview Questions | C++ Interview Questions | Quick2Host Review