Unit-1: Introduction to CGMA

Advantages of interactive graphics

In interactive Computer Graphics user have some controls over the picture, i.e., the user can make any change in the produced image.

Interactive Computer Graphics require two-way communication between the computer and the user. A User can see the image and make any change by sending his command with an input device.

Advantages:

  1. Higher Quality
  2. More precise results or products
  3. Greater Productivity
  4. Lower analysis and design cost
  5. Significantly enhances our ability to understand data and to perceive trends.

Representative use of computer graphics

The use of computer graphics is wide spread. It is used in various areas such as industry, business, government organizations, education, entertainment and most recently the home. User friendliness is one of the main factors underlying the success and popularity of any system. It is now a well established fact that graphical interfaces provide in attractive and easy interaction between users and computers.

In industry, business, government and educational organizations, computer graphics is most commonly used to create 2D and 3D graphics of mathematical, physical and economic functions in form of histograms, bars, and pie-chats. These graphs and charts are very useful for decision making.
The desktop publishing on personal computers allow the use of graphics for the creation and dissemination of information. Many organizations does the in-house creation and dissemination of documents. The desktop publishing allows user to create documents which contain text, tables, graphs, and other forms of drawn or scanned images or pictures. This is one approach towards the office automation.The computer-aided drafting uses graphics to design components and systems electrical, mechanical, electromechanical and electronic devices such as automobile bodies, structures of building, airplane, slips, very large-scale integrated chips, optical systems and computer networks.
Use of graphics in simulation makes mathematic models and mechanical systems more realistic and easy to study. The interactive graphics supported by animation software proved their use in production of animated movies and cartoons films.
There is lot of development in the tools provided by computer graphics. This allows user to create artistic pictures which express messages and attract attentions. Such pictures are very useful in advertising.

Conceptual Framework for Interactive Graphics

Conceptual Framework has the following elements:

– Graphics Library – Between application and display hardware there is graphics library / API.

– Application Program – An application program maps all application objects to images by invoking graphics.

– Graphics System – An interface that interacts between Graphics library and Hardware.

– Modifications to images are the result of user interaction.

Scan Converting a Straight Line

A straight line may be defined by two endpoints & an equation. In fig the two endpoints are described by (x1,y1) and (x2,y2). The equation of the line is used to determine the x, y coordinates of all the points that lie between these two endpoints.

Scan Converting a Straight Line
bcastudyguide.com

Using the equation of a straight line, y = mx + b where m = Scan Converting a Straight Line & b = the y interrupt, we can find values of y by incrementing x from x =x1, to x = x2. By scan-converting these calculated x, y values, we represent the line as a sequence of pixels.

Algorithm for drawing line using equation:

Step1: Start Algorithm

Step2: Declare variables x1,x2,y1,y2,dx,dy,m,b,

Step3: Enter values of x1,x2,y1,y2.
              The (x1,y1) are co-ordinates of a starting point of the line.
              The (x2,y2) are co-ordinates of a ending point of the line.

Step4: Calculate dx = x2– x1

Step5: Calculate dy = y2-y1

Step6: Calculate m = Scan Converting a Straight Line

Step7: Calculate b = y1-m* x1

Step8: Set (x, y) equal to starting point, i.e., lowest point and xendequal to largest value of x.

              If dx < 0
                  then x = x2
              y = y2
                        xend= x1
        If dx > 0
              then x = x1
                  y = y1
                        xend= x2

Step9: Check whether the complete line has been drawn if x=xend, stop

Step10: Plot a point at current (x, y) coordinates

Step11: Increment value of x, i.e., x = x+1

Step12: Compute next value of y from equation y = mx + b

Step13: Go to Step9.

Program to draw a line using LineSlope Method

  1. #include <graphics.h>  
  2. #include <stdlib.h>  
  3. #include <math.h>  
  4. #include <stdio.h>  
  5. #include <conio.h>  
  6. #include <iostream.h>  
  7.   
  8. class bresen  
  9. {  
  10.     float x, y, x1, y1, x2, y2, dx, dy, m, c, xend;  
  11.     public:  
  12.     void get ();  
  13.     void cal ();  
  14. };  
  15.     void main ()  
  16.     {  
  17.     bresen b;  
  18.     b.get ();  
  19.     b.cal ();  
  20.     getch ();  
  21.    }  
  22.     Void bresen :: get ()  
  23.    {  
  24.     print (“Enter start & end points”);  
  25.     print (“enter x1, y1, x2, y2”);  
  26.     scanf (“%f%f%f%f”,sx1, sx2, sx3, sx4)  
  27. }  
  28. void bresen ::cal ()  
  29. {  
  30.     /* request auto detection */  
  31.     int gdriver = DETECT,gmode, errorcode;  
  32.     /* initialize graphics and local variables */  
  33.     initgraph (&gdriver, &gmode, ” “);  
  34.     /* read result of initialization */  
  35.     errorcode = graphresult ();  
  36.     if (errorcode ! = grOK)    /*an error occurred */  
  37.     {  
  38.         printf(“Graphics error: %s \n”, grapherrormsg (errorcode);  
  39.         printf (“Press any key to halt:”);  
  40.         getch ();  
  41.         exit (1); /* terminate with an error code */  
  42.     }  
  43.     dx = x2-x1;  
  44.     dy=y2-2y1;  
  45.     m = dy/dx;  
  46.     c = y1 – (m * x1);  
  47.     if (dx<0)  
  48.     {  
  49.         x=x2;  
  50.         y=y2;  
  51.         xend=x1;  
  52.     }  
  53.     else  
  54.     {  
  55.         x=x1;  
  56.         y=y1;  
  57.         xend=x2;  
  58.     }  
  59. while (x<=xend)  
  60.     {  
  61.         putpixel (x, y, RED);  
  62.         y++;  
  63.         y=(x*x) +c;  
  64.     }  
  65. }  

scan converting a Circle:

Circle is an eight-way symmetric figure. The shape of circle is the same in all quadrants. In each quadrant, there are two octants. If the calculation of the point of one octant is done, then the other seven points can be calculated easily by using the concept of eight-way symmetry.

For drawing, circle considers it at the origin. If a point is P1(x, y), then the other seven points will be

Defining a Circle

So we will calculate only 45°arc. From which the whole circle can be determined easily.

If we want to display circle on screen then the putpixel function is used for eight points as shown below:

          putpixel (x, y, color)
          putpixel (x, -y, color)
          putpixel (-x, y, color)
          putpixel (-x, -y, color)
          putpixel (y, x, color)
          putpixel (y, -x, color)
          putpixel (-y, x, color)
          putpixel (-y, -x, color)

Example: Let we determine a point (2, 7) of the circle then other points will be (2, -7), (-2, -7), (-2, 7), (7, 2), (-7, 2), (-7, -2), (7, -2)

These seven points are calculated by using the property of reflection. The reflection is accomplished in the following way:

The reflection is accomplished by reversing x, y co-ordinates.

Defining a Circle

There are two standards methods of mathematically defining a circle centered at the origin.

  1. Defining a circle using Polynomial Method
  2. Defining a circle using Polar Co-ordinates

Program to draw a circle using Polynomial Method:

  1. #include<graphics.h>  
  2. #include<conio.h>  
  3. #include<math.h>  
  4. voidsetPixel(int x, int y, int h, int k)  
  5. {  
  6.     putpixel(x+h, y+k, RED);  
  7.     putpixel(x+h, -y+k, RED);  
  8.     putpixel(-x+h, -y+k, RED);  
  9.     putpixel(-x+h, y+k, RED);  
  10.     putpixel(y+h, x+k, RED);  
  11.     putpixel(y+h, -x+k, RED);  
  12.     putpixel(-y+h, -x+k, RED);  
  13.     putpixel(-y+h, x+k, RED);  
  14. }  
  15. main()  
  16. {  
  17.     intgd=0, gm,h,k,r;  
  18.     double x,y,x2;  
  19.     h=200, k=200, r=100;  
  20.     initgraph(&gd, &gm, “C:\\TC\\BGI “);  
  21.     setbkcolor(WHITE);  
  22.     x=0,y=r;  
  23.     x2 = r/sqrt(2);  
  24.     while(x<=x2)  
  25.     {  
  26.         y = sqrt(r*r – x*x);  
  27.         setPixel(floor(x), floor(y), h,k);  
  28.         x += 1;  
  29.     }  
  30.     getch();  
  31.     closegraph();  
  32.     return 0;  
  33. }  

Program to draw a circle using Polar Coordinates:

  1. #include <graphics.h>  
  2. #include <stdlib.h>  
  3. #define color 10  
  4. void eightWaySymmetricPlot(int xc,int yc,int x,int y)  
  5. {  
  6.     putpixel(x+xc,y+yc,color);  
  7.     putpixel(x+xc,-y+yc,color);  
  8.     putpixel(-x+xc,-y+yc,color);  
  9.     putpixel(-x+xc,y+yc,color);  
  10.     putpixel(y+xc,x+yc,color);  
  11.     putpixel(y+xc,-x+yc,color);  
  12.     putpixel(-y+xc,-x+yc,color);  
  13.     putpixel(-y+xc,x+yc,color);  
  14. }  
  15. void PolarCircle(int xc,int yc,int r)  
  16. {  
  17.     int x,y,d;  
  18.     x=0;  
  19.     y=r;  
  20.     d=3-2*r;  
  21.     eightWaySymmetricPlot(xc,yc,x,y);  
  22.     while(x<=y)  
  23.     {  
  24.         if(d<=0)  
  25.         {  
  26.             d=d+4*x+6;  
  27.         }  
  28.         else  
  29.         {  
  30.             d=d+4*x-4*y+10;  
  31.             y=y-1;  
  32.         }  
  33.         x=x+1;  
  34.         eightWaySymmetricPlot(xc,yc,x,y);  
  35.     }  
  36. }  
  37. int main(void)  
  38. {  
  39.     int gdriver = DETECT, gmode, errorcode;  
  40.     int xc,yc,r;  
  41.     initgraph(&gdriver, &gmode, “c:\\turboc3\\bgi”);  
  42. errorcode = graphresult();  
  43. if (errorcode != grOk)    
  44.     {  
  45.         printf(“Graphics error: %s\n”, grapherrormsg(errorcode));  
  46.         printf(“Press any key to halt:”);  
  47.         getch();  
  48.         exit(1);               
  49.     }  
  50. printf(“Enter the values of xc and yc ,that is center points of circle : “);  
  51.     scanf(“%d%d”,&xc,&yc);  
  52.     printf(“Enter the radius of circle : “);  
  53.     scanf(“%d”,&r);  
  54.     PolarCircle(xc,yc,r);  
  55.     getch();  
  56.     closegraph();  
  57.     return 0;  
  58. }  

Scan Converting a Ellipse:

The ellipse is also a symmetric figure like a circle but is four-way symmetry rather than eight-way.

Scan Converting a Ellipse

Program to Implement Ellipse Drawing Algorithm:

  1. #include<stdio.h>  
  2. #include<conio.h>  
  3. #include<graphics.h>  
  4. #include<math.h>  
  5. void disp();  
  6. float x,y;  
  7. intxc,yc;  
  8. void main()  
  9. {  
  10.                 intgd=DETECT,gm,a,b;  
  11.                 float p1,p2;  
  12.                 clrscr();  
  13.                 initgraph(&gd,&gm,”c:\\turboc3\\bgi”);  
  14.                 printf(“*** Ellipse Generating Algorithm ***\n”);  
  15.                 printf(“Enter the value of Xc\t”);  
  16.                 scanf(“%d”,&xc);  
  17.                 printf(“Enter the value of yc\t”);  
  18.                 scanf(“%d”,&yc);  
  19.                 printf(“Enter X axis length\t”);  
  20.                 scanf(“%d”,&a);  
  21.                 printf(“Enter Y axis length\t”);  
  22.                 scanf(“%d”,&b);  
  23.                 x=0;y=b;  
  24.                 disp();  
  25.                 p1=(b*b)-(a*a*b)+(a*a)/4;  
  26.                 while((2.0*b*b*x)<=(2.0*a*a*y))  
  27.                 {  
  28.                                 x++;  
  29.                                 if(p1<=0)  
  30.                                 p1=p1+(2.0*b*b*x)+(b*b);  
  31.                                 else  
  32.         {  
  33.                                                 y–;  
  34.                                                 p1=p1+(2.0*b*b*x)+(b*b)-(2.0*a*a*y);  
  35.          }  
  36.                                disp();  
  37.                                x=-x;  
  38.                                disp();  
  39.                                x=-x;  
  40.                                delay(50);  
  41.                  }  
  42.                  x=a;  
  43.                  y=0;  
  44.                  disp();  
  45.                  p2=(a*a)+2.0*(b*b*a)+(b*b)/4;  
  46.                  while((2.0*b*b*x)>(2.0*a*a*y))  
  47.                 {  
  48.                                 y++;  
  49.                                 if(p2>0)  
  50.                                 p2=p2+(a*a)-(2.0*a*a*y);  
  51.                                 else  
  52.         {  
  53.                                                 x–;  
  54.                                                 p2=p2+(2.0*b*b*x)-(2.0*a*a*y)+(a*a);  
  55.          }  
  56.                                 disp();  
  57.                                 y=-y;  
  58.                                 disp();  
  59.                                 y=-y;  
  60.                                 delay(50);     
  61.             }  
  62.                 getch();  
  63.                 closegraph();  
  64. }  
  65.  void disp()  
  66. {  
  67.               putpixel(xc+x,yc+y,7);  
  68.                putpixel(xc-x,yc+y,7);  
  69.                putpixel(xc+x,yc-y,7);  
  70.           putpixel(xc+x,yc-y,7);  
  71.   }