Ask Question

Name:
Title:
Your Question:

Answer Question

Name:
Your Answer:
User Submitted Source Code!


Description:
  graphics.c
Language: C/C++
Code:

     // assigment 2 dda line 
#include<stdlib.h>
#include<stdio.h>
#include<graphics.h>

void linedot(float x,float y,float dx,float dy,int len);
void dash(float x,float y,float dx,float dy,int len);
void dcontinous(float x,float y,float dx,float dy,int len);
void main()
{
int x1,x2,y1,y2,ch,s1,s2,len;
float dx,dy,x,y;
int gd=DETECT,gm;
     printf("/n ENTER THE CO-ORDINATES [max +/-320 ,+/- 240]");
     scanf("%d%d%d%d",&x1,&x2,&y1,&y2);
 
     x1=320+x1;  x2=320+x2; 
     y1=240-y1;     y2=240-y2;

5c               dx=abs(x2-x1);
               dy=abs(y2-y1);

     if(dy>=dx)
          len=dy;
     else
          len=dx;
     
     dx=(x2-x1)/(float) len;
     dy=(y2-y1)/(float) len;

     if(x2>x1)
             s1=1;
     else if(x1>x2)
           s1=-1;
     else 
          s1=0;

     if(y2>y1)
          s2=1;
     else if(y1>y2)
          s2=-1;
     else 
          s1=0;

x=x1+0.5*s1;
y=y1+0.5*s2;

     while(1)
     {     
     printf("\n Enter Your choice \n 1. Continous \n 2. Dashed \n 3. Linedot \n 4. Thick\n 5. Exit ");
     scanf("%d",&ch);     
     switch(ch)
     {
           case 1 :     initgraph(&gd,&gm,"");
                    setbkcolor(WHITE);
                    setcolor(BLACK);
                    line(320,0,320,480);
                    line(0,240,640,240);
                    dcontinous(x,y,dx,dy,len);  delay(5000);
                    getch();
                    closegraph();
                                break;
          case 2 :
                               initgraph(&gd,&gm,"");
                    setbkcolor(WHITE);
                    setcolor(BLACK);
                    line(320,0,320,480);
                    line(0,240,640,240);
                    dash(x,y,dx,dy,len);
                    delay(5000);                    
                    getch();
                    closegraph();
                                break;
          case 3 :
                     initgraph(&gd,&gm,"");
                    setbkcolor(WHITE);
                    setcolor(BLACK);
                    line(320,0,320,480);
                    line(0,240,640,240);
                    linedot(x,y,dx,dy,len); delay(5000);
                    getch();
                    closegraph();
                                break;
     }

}
}
void dcontinous(float x,float y,float dx,float dy,int len)
{     int i=1;
while(i<=len)
{
putpixel(x,y,0);
x=x+dx;
y=y+dy;
i++;
}     }
void dash(float x,float y,float dx,float dy,int len)
{
int i=1,j=0;

     while(i<=len)
{
     for(j=0;j<5;j++)
     {
     putpixel(x,y,0);
     x=x+dx;
     y=y+dy;                        i++;
     }
     for(j=0;j<5;j++)
     {
     x=x+dx;
     y=y+dy;                        i++;     
     }
}
}
void linedot(float x,float y,float dx,float dy,int len)
{
int i=1,j=0;
     while(i<=len)
     {      for(j=0;j<5;j++)     
          {putpixel(x,y,0);
          x=x+dx;
          y=y+dy;
          i++;
          }
          for(j=0;j<5;j++)     
          {
           if(j==2)
          putpixel(x,y,0);
          x=x+dx;
          y=y+dy;
          i++;
          }
          }
}




























Comments: