Skip to main content

CG 2

 Write C++ program to implement Cohen Southerland line clipping algorithm.

The code for above problem is as follows:


 //cohen-sutherland line clipping algorithm  
 #include<iostream.h>  
 #include<stdlib.h>  
 #include<math.h>  
 #include<graphics.h>  
 #include<dos.h>  
   
 typedef struct coordinate  
 {  
      int x,y;  
      char code[4];  
 }PT;  
   
 void drawwindow();  
 void drawline(PT p1,PT p2);  
 PT setcode(PT p);  
 int visibility(PT p1,PT p2);  
 PT resetendpt(PT p1,PT p2);  
   
 int main()  
 {  
      int gd=DETECT,v,gm;  
      PT p1,p2,p3,p4,ptemp;  
   
      cout<<"\nEnter x1 and y1\n";  
      cin>>p1.x>>p1.y;  
      cout<<"\nEnter x2 and y2\n";  
      cin>>p2.x>>p2.y;  
   
      initgraph(&gd,&gm,"c:\\turboc3\\bgi");  
      drawwindow();  
      delay(500);  
   
      drawline(p1,p2);  
      delay(500);  
      cleardevice();  
   
      delay(500);  
      p1=setcode(p1);  
      p2=setcode(p2);  
      v=visibility(p1,p2);  
      delay(500);  
   
      switch(v)  
      {  
      case 0: drawwindow();  
                delay(500);  
                drawline(p1,p2);  
                break;  
      case 1:     drawwindow();  
                delay(500);  
                break;  
      case 2:     p3=resetendpt(p1,p2);  
                p4=resetendpt(p2,p1);  
                drawwindow();  
                delay(500);  
                drawline(p3,p4);  
                break;  
      }  
   
      delay(5000);  
      closegraph();  
 return 0;  
 }  
   
 void drawwindow()  
 {  
      line(150,100,450,100);  
      line(450,100,450,350);  
      line(450,350,150,350);  
      line(150,350,150,100);  
 }  
   
 void drawline(PT p1,PT p2)  
 {  
      line(p1.x,p1.y,p2.x,p2.y);  
 }  
   
 PT setcode(PT p)     //for setting the 4 bit code  
 {  
      PT ptemp;  
   
      if(p.y<100)  
           ptemp.code[0]='1';     //Top  
      else  
           ptemp.code[0]='0';  
   
      if(p.y>350)  
           ptemp.code[1]='1';     //Bottom  
      else  
           ptemp.code[1]='0';  
   
      if(p.x>450)  
           ptemp.code[2]='1';     //Right  
      else  
           ptemp.code[2]='0';  
   
      if(p.x<150)  
           ptemp.code[3]='1';     //Left  
      else  
           ptemp.code[3]='0';  
   
      ptemp.x=p.x;  
      ptemp.y=p.y;  
   
      return(ptemp);  
 }  
   
 int visibility(PT p1,PT p2)  
 {  
      int i,flag=0;  
   
 for(i=0;i<4;i++)  
   {  
    if((p1.code[i]!='0')||(p2.code[i]!='0'))  
    flag=2;  
   }  
 for(i=0;i<4;i++)  
  {  
   if((p1.code[i]==p2.code[i]) &&(p1.code[i]=='1'))  
   flag=1;  
  }  
 if(flag==0) //Line is inside window....Accept  
  return(0);  
   
 if(flag==1) //Line is outside window ...Reject  
  return(1);  
   
  return(2);  
 }  
   
 PT resetendpt(PT p1,PT p2)  
 {  
      PT temp;  
      int x,y,i;  
      float m,k;  
   
      if(p1.code[3]=='1')  
           x=150;  
   
      if(p1.code[2]=='1')  
           x=450;  
   
      if((p1.code[3]=='1') || (p1.code[2]=='1'))  
      {  
           m=(float)(p2.y-p1.y)/(p2.x-p1.x);  
           k=(p1.y+(m*(x-p1.x)));  
           temp.y=k;  
           temp.x=x;  
   
           if(temp.y<=350 && temp.y>=100)  
                return (temp);  
      }  
   
      if(p1.code[0]=='1')  
           y=100;  
   
      if(p1.code[1]=='1')  
           y=350;  
   
      if((p1.code[0]=='1') || (p1.code[1]=='1'))  
      {  
           m=(float)(p2.y-p1.y)/(p2.x-p1.x);  
           k=(float)p1.x+(float)(y-p1.y)/m;  
           temp.x=k;  
           temp.y=y;  
   
           return(temp);  
      }  
      else  
           return(p1);  
 }  

Comments

Popular posts from this blog

Circle Triangle Pattern

  Group A   Practical 3 a   Problem Statement :   a) Write C++ program to draw the following pattern. Use DDA line and Bresenham‘s circle drawing algorithm. Apply the concept of encapsulation. Check Out Code Here  ðŸ‘‡ Output :  Code can get updated so also come back later to see if there is any changes. Also if there is any problem with code you can comment below. If you like it, do share with your friends.😊

DSL A2

  Write a Python program to store marks scored in subject “Fundamental of Data Structure” by N students in the class. Write functions to compute following:  a) The average score of class  b) Highest score and lowest score of class  c) Count of students who were absent for the test  d) Display mark with highest frequency The Code for above problem is as follows : """ @author: SPPU SE GURU """ total=int(input ("Enter total no. of students in your class ")) pre=int(input("No. of students appeared for FDS exam : ")) U=[] for i in range (total): i=i+1 U.append(i) R=[] for i in range(pre): roll=int(input("Enter Roll no. of students present for test :- ")) R.append(roll) M=[] for i in range(pre): print("Enter Roll no. ",R[i],end=" ") marks=int(input("Marks:- ")) M.append(marks) print("********** Solutions Are as fo...

DSL E29

 Data Structure Lab : Practical 29 : Problem Statement:  Queues are frequently used in computer programming, and a typical example is the creation of a job queue by an operating system. If the operating system does not use priorities, then the jobs are processed in the order they enter the system. Write C++ program for simulating job queue. Write functions to add job and delete job from queue Check Out Code Here  ðŸ‘‡ // Author : SPPU CSE GURU #include<iostream> using namespace std; class Queue{ private: int front,rear,max; public: int Q[10]; Queue() { front=0; rear=-1; max=10; } void EnQueue(); void DeQueue(); void Display(); }; void Queue::EnQueue() { int data; cout<<"Enter Job no. to Add "<<endl; cin>>data; if(rear...