Generating Line, Circle and Ellipse using C++

CAD Package

         CAD or Computer Aided Design is a computer technology which is very useful to draw a solid model, modification, visualize and documentation of 3D models. Geometric modelling concepts are used to code CAD packages.

Line (DDA Algorithm)

{
  float x1,y1,x2,y2,L,del_x,del_y,x_new,y_new;
  cout<<"Enter the Starting point: ";
  cin>>x1>>y1;
  cout<<"Enter the End point: ";
  cin>>x2>>y2;
        x=x2-x1;
        y=y2-y1;
  if (x>=y) 
       L=x;
  else
      L=y;
  del_x=(x/L);
  del_y=(y/L);
  x_new=(x1+0.5);
  y_new=(y1+0.5);
putpixel(320+x_new,-y_new+240,WHITE);
  i=1;
  while(i<=L)
  {
    x_new=x_new+del_x; a[i][0]=x_new;
    y_new=y_new+del_y;a[i][1]=x_new; a[i][2]=1;
    putpixel(x_new+320,-y_new+240,100);
    i++;
  }
}

Fig 1 Output of  DDA Algorithm

Circle (Midpoint Circle Algorithm)

             A circle is a shape with all points the same distance from its center.  Circle is an Ellipse, where both foci are at the same point (the centre). For generating Circle first of all generate 1st octant of circle (Fig.2). Remaining octants generating by using 1st octant coordinates with reflection functions. Reflections are about x-axis, y-axis and origin.

1. Get Parameter od Circle [get()]

          This fuctions get the circle center and diameter values from the user. After getting circle data the first octant values were calculated from the origin. The coordinates values are stored in array here named as "a".
{
   cout<<"Enter the circle center: ";
   cin>>xc>>yc;
   cout<<"Circle diameter: ";
   cin>>d;
   y=d/2;
   putpixel(320,240+y,100);
   f=1.25-y; i=0; x=0;
   while(x<=y)
     {
        a[i][0]=x;
        a[i][1]=y;
        a[i][2]=1;
        i++;
     if(f<0)
       f=f+2*x+3;
    else
     {
     f=f+2*(x-y)+5;
     y=y-1;
     }
 x=x+1;
     }
}

2. Translate to circle center [translation(xc,yc)]

            This fuction was executed for translate the calculated coordinates from the origin to true circle center . Here the octant coordinates which are stored in array "a" was multiplied with trnslation matrix which was defined as array named as "t". By multipling both matrices "a" and "t" the resultant matrix was defined as "b". 
{
   t[0][0]=1; t[0][1]=0; t[0][2]=0;
   t[1][0]=0; t[1][1]=1; t[1][2]=0;
   t[2][0]=xc; t[2][1]=yc; t[2][2]=1;
   for(int j=0;j<i;j++)
   {
      for(int k=0;k<3;k++)
      {
         b[j][k]=0;
         for(int l=0;l<3;l++)
         {
           b[j][k]=b[j][k]+a[j][l]*t[l][k];
         }
     }
  }
}

3. Displaying the coordinates [display()]

        The calculated array values were displyed using this fuction. The coordinate values taken from the array "b". Here the octant 1 was printed as output(Fig.2).
{
  for(int p=0;p<i;p++)
  putpixel(b[p][0]+320,-b[p][1]+240,100);

}

4. Swap values [swapv()]

          The first octant coordinate values calculated from origin (array "a") were swapped 'x' to 'y' and 'y' to 'x'. Again translated to a true circle center and displayed as output.
{
  for(int q=0;q<i;q++)
  {
   temp=a[q][0];
   a[q][0]=a[q][1];
   a[q][1]=temp;
  }
}

5. Reflection about origin [swaps()]

            This fuction is helps to reflect the coordinates about origin.
{
 for(int q=0;q<i;q++)
 {
  temp=-a[q][0];
  a[q][0]=-a[q][1];
  a[q][1]=temp;
 }
}

6. X-Reflection [x_reflection()]

            Reflection fuctions are used to reflect the coordinate values about x-axis or y-axis. X-reflection is only change the sign of 'y' coordinate.
{
 for(int q=0;q<i;q++)
 a[q][1]=-a[q][1];
}

7.Y-Reflection [y_reflection()]

             Y-axis reflection is used to reflect the coordinate values about y-axis. Y-reflection is only change the sign of 'x' coordinate.
{
 for(int q=0;q<i;q++)
 a[q][0]=-a[q][0];

8. Displaying a Circle

             After defining each fuctions above call every function to disply a circle. 
Fig 2 CIrcle with octants
{
  get();
  translation(xc,yc); getch();
  display();                         //1st octant
  swapv();
  translation(xc,yc); getch();
  display();                         //2nd octant
  x_reflection();
  translation(xc,yc); getch();
  display();                         //3rd octant
  swapv();
  translation(xc,yc); getch();
  display();                         //4th octant
  swaps(); 
  translation(xc,yc); getch();
  display();                         //5th octant
  x_reflection();
  translation(xc,yc); getch();
  display();                         //6th octant
  swapv();
  translation(xc,yc); getch();
  display();                         //7th octant
  y_reflection();
  translation(xc,yc); getch();
  display();                         //8th octant
  getch();
}
By calling fuctions as above sequence can get output as like Fig.3
Output of  Midpoint circle algorithm

Ellipse

           A regular oval shape, traced by a point moving i plane so that the sum of its distances from two other points (the foci) is constant. 
An ellipose obtained as the intersection of a cone with inclined plane
                 Giving ellipse center, major and minor axis diameter as input by the user can generate the ellipse. Try this. Sythetic curves algorithms will be posted in future.
{
  cout<<"Enter the Ellipse center: ";
  cin>>xc>>yc;
  cout<<"Semi major diameter: ";
  cin>>A;
  cout<<"Semi minor diameter: ";
  cin>>B;
  for(float u=0;u<=6.28;u+=0.01)
  {
   x=xc+A*(cos (u));
   y=yc+B*(sin (u));
   putpixel(floor(x+320),floor(-y+240),100);
  }
}
Output of  C++ Program
-------ALL THE BEST------




Comments

  1. Worth reading!! It's great that IoT technology can help in improving the manufacturing industry outcome. It can help in eliminating the unplanned equipment downtime. So one must use the right OEE calculation software for their industry. www.downtimecollectionsolutions.com Thanks!!

    ReplyDelete

Post a Comment