Showing posts with label ComputerGraphics. Show all posts
Showing posts with label ComputerGraphics. Show all posts

Sunday, November 5, 2017

Sutherland PolyGon CLipping

Code:

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#define round(a) ((int)(a+0.5))
int k;
float xmin,ymin,xmax,ymax,arr[20],m;
void clipl(float x1,float y1,float x2,float y2)
{
    if(x2-x1)
        m=(y2-y1)/(x2-x1);
    else
        m=100000;
    if(x1 >= xmin && x2 >= xmin)
    {
        arr[k]=x2;
        arr[k+1]=y2;
        k+=2;
    }
    if(x1 < xmin && x2 >= xmin)
    {
        arr[k]=xmin;
        arr[k+1]=y1+m*(xmin-x1);
        arr[k+2]=x2;
        arr[k+3]=y2;
        k+=4;
    }
    if(x1 >= xmin  && x2 < xmin)
    {
        arr[k]=xmin;
        arr[k+1]=y1+m*(xmin-x1);
        k+=2;
    }
}

void clipt(float x1,float y1,float x2,float y2)
{
    if(y2-y1)
        m=(x2-x1)/(y2-y1);
    else
        m=100000;
    if(y1 <= ymax && y2 <= ymax)
    {
        arr[k]=x2;
        arr[k+1]=y2;
        k+=2;
    }
    if(y1 > ymax && y2 <= ymax)
    {
        arr[k]=x1+m*(ymax-y1);
        arr[k+1]=ymax;
        arr[k+2]=x2;
        arr[k+3]=y2;
        k+=4;
    }
    if(y1 <= ymax  && y2 > ymax)
    {
        arr[k]=x1+m*(ymax-y1);
        arr[k+1]=ymax;
        k+=2;
    }
}

void clipr(float x1,float y1,float x2,float y2)
{
    if(x2-x1)
        m=(y2-y1)/(x2-x1);
    else
        m=100000;
    if(x1 <= xmax && x2 <= xmax)
    {
        arr[k]=x2;
        arr[k+1]=y2;
        k+=2;
    }
    if(x1 > xmax && x2 <= xmax)
    {
        arr[k]=xmax;
        arr[k+1]=y1+m*(xmax-x1);
        arr[k+2]=x2;
        arr[k+3]=y2;
        k+=4;
    }
    if(x1 <= xmax  && x2 > xmax)
    {
        arr[k]=xmax;
        arr[k+1]=y1+m*(xmax-x1);
        k+=2;
    }
}

void clipb(float x1,float y1,float x2,float y2)
{
    if(y2-y1)
        m=(x2-x1)/(y2-y1);
    else
        m=100000;
    if(y1 >= ymin && y2 >= ymin)
    {
        arr[k]=x2;
        arr[k+1]=y2;
        k+=2;
    }
    if(y1 < ymin && y2 >= ymin)
    {
        arr[k]=x1+m*(ymin-y1);
        arr[k+1]=ymin;
        arr[k+2]=x2;
        arr[k+3]=y2;
        k+=4;
    }
    if(y1 >= ymin  && y2 < ymin)
    {
        arr[k]=x1+m*(ymin-y1);
        arr[k+1]=ymin;
        k+=2;
    }
}

void main()
{
    int gdriver=DETECT,gmode,n,poly[20];
    float xi,yi,xf,yf,polyy[20];
    clrscr();
    cout<<"Coordinates of rectangular clip window :\nxmin,ymin             :";
    cin>>xmin>>ymin;
    cout<<"xmax,ymax             :";
    cin>>xmax>>ymax;
    cout<<"\n\nPolygon to be clipped :\nNumber of sides       :";
    cin>>n;
    cout<<"Enter the coordinates :";
    for(int i=0;i < 2*n;i++)
    cin>>polyy[i];
    polyy[i]=polyy[0];
    polyy[i+1]=polyy[1];
    for(i=0;i < 2*n+2;i++)
    poly[i]=round(polyy[i]);
    initgraph(&gdriver,&gmode,"C:\\TURBOC3\\BGI");
    setcolor(RED);
    rectangle(xmin,ymax,xmax,ymin);
    cout<<"\t\tUNCLIPPED POLYGON";
    setcolor(WHITE);
    fillpoly(n,poly);
  getch();
    cleardevice();
    k=0;
    for(i=0;i < 2*n;i+=2)
    clipl(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]);
    n=k/2;
    for(i=0;i < k;i++)
    polyy[i]=arr[i];
    polyy[i]=polyy[0];
    polyy[i+1]=polyy[1];
    k=0;
    for(i=0;i < 2*n;i+=2)
    clipt(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]);
    n=k/2;
    for(i=0;i < k;i++)
    polyy[i]=arr[i];
    polyy[i]=polyy[0];
    polyy[i+1]=polyy[1];
    k=0;
    for(i=0;i < 2*n;i+=2)
    clipr(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]);
    n=k/2;
    for(i=0;i < k;i++)
    polyy[i]=arr[i];
    polyy[i]=polyy[0];
    polyy[i+1]=polyy[1];
    k=0;
    for(i=0;i < 2*n;i+=2)
    clipb(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]);
    for(i=0;i < k;i++)
    poly[i]=round(arr[i]);
    if(k)
    fillpoly(k/2,poly);
    setcolor(RED);
    rectangle(xmin,ymax,xmax,ymin);
    cout<<"\tCLIPPED POLYGON";
    getch();
    closegraph();
}

Output:


This Program is taken from pracspedia.com we at gndecprogramming do not believe in copying but this is done because of less time.....

Mid Point Eclipse Drawing Algoritm

Code:

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void disp();
float x,y;
int xc,yc;
void main()
{
int gd=DETECT,gm;
int a,b;
float p1,p2;
clrscr();
initgraph(&gd,&gm,"");
scanf("%d%d",&xc,&yc);
scanf("%d%d",&a,&b);
x=0;y=b;
disp();
p1=(b*b)-(a*a*b)+(a*a)/4;
while((2.0*b*b*x)<=(2.0*a*a*y))
{
x++;
if(p1<=0)
p1=p1+(2.0*b*b*x)+(b*b);
else
{
y--;
p1=p1+(2.0*b*b*x)+(b*b)-(2.0*a*a*y);
}
disp();
x=-x;
disp();
x=-x;
}
x=a;
y=0;
disp();
p2=(a*a)+2.0*(b*b*a)+(b*b)/4;
while((2.0*b*b*x)>(2.0*a*a*y))
{
y++;
if(p2>0)
p2=p2+(a*a)-(2.0*a*a*y);
else
{
x--;
p2=p2+(2.0*b*b*x)-(2.0*a*a*y)+(a*a);
}
disp();
y=-y;
disp();
y=-y;
}
getch();
closegraph();
}
void disp()
{
putpixel(xc+x,yc+y,10);
putpixel(xc-x,yc+y,10);
putpixel(xc+x,yc-y,10);
putpixel(xc+x,yc-y,10);
}


Output:



This Program is Taken from Studentcpu.com, We at gndecprogramming donot believe in copying but this time we did it because of less time .....

Saturday, November 4, 2017

Transformation Computer Graphics

Question:
Write a program to draw any 2-D object and perform the transformations on it according to the input parameters from the user, namely: Translation, Rotation or Scaling

Code:

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main(){

    int g=DETECT,gmode;
    initgraph(&g,&gmode,"C://TURBOC3//BGI");
    line(200,300,300,200);
    int x;
    cout<<"Enter Your Choice\nPress 1 For Translation\n2 For Rotation\n3 For Scaling\n";
    cin>>x;

    if(x==1){
cleardevice();
int tx,ty;
line(200,300,300,200);
cout<<"Enter Translation Along x and y\n";
cout<<"Tx =";
cin>>tx;
cout<<"Ty =";
cin>>ty;
line(200+tx,300+ty,300+tx,200+ty);
    }else if(x==2){
cleardevice();
line(200,300,300,200);
cout<<"Enter Rotation Angle:\n";
cout<<"a=";
float a;
cin>>a;
a=a*(3.142/180);
cout<<200*cos(a)-300*sin(a)<<","<<200*sin(a)+300*cos(a)<<","<<300*cos(a)-200*sin(a)<<","<<300*sin(a)+200*cos(a);
//line((int)200*cos(a)-300*sin(a),(int)200*sin(a)+300*cos(a),(int)300*cos(a)-200*sin(a),(int)300*sin(a)+200*cos(a));
line(200*cos(a)-300*sin(a),200*sin(a)+300*cos(a),300*cos(a)-200*sin(a),300*sin(a)+200*cos(a));
    }else{
cleardevice();
float sx,sy;
line(200,300,300,200);
cout<<"Enter Scaling along x and y\n";
cout<<"Sx =";
cin>>sx;
cout<<"Sy =";
cin>>sy;
line(200*sx,300*sy,300*sx,200*sy);
    }

    getch();
}

Output:

Tuesday, October 10, 2017

MidPoint Circle Generation Algorithm

Question: Write a program to generate a complete moving wheel using Midpoint circle drawing algorithm and DDA line drawing algorithm.

Code:
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<graphics.h>
#include<dos.h>
#include<math.h>
int u=1;
void dda(int x1, int y1, int x2, int y2) {
  int s, dx, dy, m;
  float xi, yi, x, y;
  dx = x2 - x1;
  dy = y2 - y1;
  if (abs(dx) > abs(dy))
    s = abs(dx); else
    s = abs(dy);
  xi = dx / (float) s;
  yi = dy / (float) s;
  x = x1;
  y = y1;
  putpixel(x1 + 0.5, y1 + 0.5, 15);
  for (m = 0; m < s; m++) {
    x += xi;
    y += yi;
    putpixel(x + 0.5, y + 0.5, 15);
  }
}
void main(){

    int g=DETECT,gmode;
    initgraph(&g,&gmode,"C://TURBOC3//BGI");

    int r;
    cout<<"Enter Radius \n";
    cin>>r;

    int x=0,y=r;
    int p=1-r;
    int xc=50,yc=200;
    while(xc<750)
    {  cleardevice();
       x=0;y=r;p=1-r;
      while(x<y){

      if(p<=0){
p=p+2*x+3;
      }else{
p=p+2*(x-y)+5;
y--;
      }
      x++;
      putpixel(xc+x,yc+y,10);
      putpixel(xc+y,yc+x,10);
      putpixel(xc+x,yc-y,10);
      putpixel(xc-y,yc+x,10);
      putpixel(xc-x,yc-y,10);
      putpixel(xc+y,yc-x,10);
      putpixel(xc-x,yc+y,10);
      putpixel(xc-y,yc-x,10);


      if(u%20==0){
      dda(xc-x,yc+y,xc+x,yc-y);
      dda(xc+x,yc+y,xc-x,yc-y);
      dda(xc+y,yc+x,xc-y,yc-x);
      dda(xc-y,yc+x,xc+y,yc-x);
    }u++;
  }
  xc=xc+5;
  delay(90);
  }
getch();
}

Output:






Tuesday, October 3, 2017

Bresenham VS DDA

Write a program to input the line coordinates from the user to generate a line using Bresenham’s method and DDA algorithm. Compare the lines for their values on theplotted line.

Code:


#include<iostream.h>
#include<conio.h>
#include<graphics.h>
void main(){

    int g=DETECT,gmode;
    initgraph(&g,&gmode,"C://TURBOC3//BGI");
    float x1,x2,y1,y2;
    cout<<"Enter coordinates\n";
    cout<<"x1:";
    cin>>x1;
    cout<<"y1:";
    cin>>y1;
    cout<<"x2:";
    cin>>x2;
    cout<<"y2:";
    cin>>y2;
    int x3=x1+100;
    int x4=x2+100;
    int y3=y1,y4=y2;
    int y=y2-y1;
    int x=x2-x1;
    float m=y/x;
    int d=2*y-x;
    putpixel(x1,y1,10);
    if(m<=1){
    while(x1!=x2){
      if(d>0){
y1++;
d=d+2*(y-x);
      }else{
      d=d+2*y;
      }
      x1++;
      putpixel(x1,y1,10);
    }}else{
      while(y1!=y2){
      if(d>0){
x1++;
d=d+2*(y-x);
      }else{
      d=d+2*y;
      }
      y1++;
      putpixel(x1,y1,10);
    }
    }
    if(m<=1){
    while(x3!=x4){
    x3++;
    y3=y3+m;
    putpixel(x3,y3,10);
    }
    }else{
    while(y3!=y4){
    y3++;
    x3=x3+1/m;
    putpixel(x3,y3,10);
    }

    }
    setcolor(15);
    settextstyle(1,HORIZ_DIR,2);
    outtextxy(x2+15,y2,"Bresenham");
    outtextxy(x2+135,y2,"DDA");
    getch();
}

   Output:


Sunday, September 24, 2017

DDA(Digital Differential Algorithm)

Code:

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
void main(){

    int g=DETECT,gmode;
    initgraph(&g,&gmode,"C://TURBOC3//BGI");
    float x1,x2,y1,y2;
    cout<<"Enter coordinates\n";
    cout<<"x1:";
    cin>>x1;
    cout<<"y1:";
    cin>>y1;
    cout<<"x2:";
    cin>>x2;
    cout<<"y2:";
    cin>>y2;
    float m=(y2-y1)/(x2-x1);
    putpixel(x1,y1,10);
    if(m<=1){
    while(x1!=x2){
    x1++;
    y1=y1+m;
    putpixel(x1,y1,10);
    }
    }else{
    while(y1!=y2){
    y1++;
    x1=x1+1/m;
    putpixel(x1,y1,10);
    }

    }
    getch();
}


Output:



Sunday, September 3, 2017

CG3

Code:

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
void main(){

    int g=DETECT,gmode;
    initgraph(&g,&gmode,"C://TURBOC3//BGI");
    setcolor(10);
    setbkcolor(15);
    settextstyle(SANS_SERIF_FONT,HORIZ_DIR,5);
    outtextxy(100,100,"HELLO WORLD!");
    setcolor(20);
    settextstyle(DEFAULT_FONT,HORIZ_DIR,5);
    outtextxy(100,200,"HELLO WORLD!");
    setcolor(25);
    settextstyle(BOLD_FONT,HORIZ_DIR,5);
    outtextxy(100,300,"HELLO WORLD!");
    getch();
}

Output:

CG2

Code:

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
void main(){
    
    int g=DETECT,gmode;
    initgraph(&g,&gmode,"C://TURBOC3//BGI");
    line(10,90,300,90);
    setcolor(10);
    rectangle(10,100,300,250);
setcolor(30);
    circle(100,180,50);
setcolor(15);
    line(160,230,200,120);
    line(200,120,240,230);
    line(240,230,160,230);

    getch();
}

Output:

Wednesday, August 30, 2017

Computer Graphics (PUT PIXEL)

CODE:


#include<iostream.h>
#include<conio.h>
#include<graphics.h>
void main(){
int g=DETECT,gmode;
initgraph(&g,&gmode,"C://TURBOC3//BGI");
putpixel(100,100,10);
getch();
}


Output: