0

I want to draw geometric diagram like straight line,triangle,circle etc using C language.I want some help for coding n algo....:)

2
Contributors
3
Replies
5
Views
6 Years
Discussion Span
Last Post by kiranroy
0
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<dos.h>
# define GRIDSIZE 2
//*****************************DECLARATIONS****************************//
int a[42][36];
int b[42][36];
char c[100];
int t=0;
void save_numbrl(int ,int );//save numbrl value
void sympoint(int,int,int,int,int);
void Ngdpt(int,int,int); //nearest grid point search function.
int nb[22][13];//strore code for each cell.
int mini(int ,int );//return min between two integer.
void midpoint_circle(int,int,int);
void put_pixel(int x,int y)
{
setcolor(YELLOW);
circle(x,y,2);
putpixel(x,y,YELLOW);
putpixel(x-1,y,YELLOW);
putpixel(x+1,y,YELLOW);
putpixel(x,y+1,YELLOW);
putpixel(x,y-1,YELLOW);
putpixel(x-1,y-1,YELLOW);
putpixel(x+1,y+1,YELLOW);
putpixel(x-1,y+1,YELLOW);
putpixel(x+1,y-1,YELLOW);
}
//****************************PLOT_GRIDS**********************************//
void print_braille_grid()//print simulated grid point funtion
{
  int i,j,x,y,m=0,n=0;
  for(i=1;i<630;i+=15)
	{   y=0;
		while(y<2)
		      {
			 for(j=1;j<470;j+=18)
			 {
			     x=0;
			     while(x<3)
			      {
			       putpixel(i,j,15);
				  a[n][m]=i;
				  b[n][m]=j;

				x++;j+=8;m++;
				if(m>35)
				m%=36;
				}

			}
		  y++;i+=8 ;n++;
		  if(n>41)
		   n%=42;
		  }
       }
}
//*************INITIALISE_CELLS_NUMBRL_VALUE_TO_ZERO*******************//
void zero()
{int i,j;
for(i=1;i<22;i++)
 {
 for(j=1;j<13;j++)
 {nb[i][j]=0;
 }
 }
 }

//********************* MIDPOINT_LINE_ALGO**********************************//
int midpoint(int x1,int x2) //return middle point
{
  return ((x1+x2)/2);
}

int distance(int x1,int y1,int x2,int y2)//return distance between two points
{   int dist;
dist=sqrt(pow((x1-x2),2)+pow((y1-y2),2));
return dist;
}

void draw_line(int x1,int y1,int x2,int y2)//draw the line whose end point are given
  {
  int m1,m2,g1,g2,i,j,z=5;//*********
  m1=midpoint(x1,x2);
  m2=midpoint(y1,y2);
  putpixel(m1,m2,14);
  putpixel(x1,y1,14);
  putpixel(x2,y2,14);
  //putpixel(x1,y1,z);
  t++;
  if(t%5==0)//*********
  {
  Ngdpt(x1,y1,z);
  Ngdpt(x2,y2,z);
  Ngdpt(m1,m2,z);
  }
if(distance(m1,m2,x1,y1)>4)
 {
 draw_line(x1,y1,m1,m2);
 draw_line(m1,m2,x2,y2);
 }
}
//**************************MIDPOINT_CIRCLE*********************************//

void midpoint_circle(int xc,int yc,int r) //mid point algo
{
int p,x,y,l=0;

x=0;
y=r;

sympoint(x,y,xc,yc,l);
p=1-r;
do
{if(p<0)
 x=x+1;
 else
 {x+=1;
  y-=1;
  }
  if(p<0)
  p=p+2*x+1;
  else
  p=p+2*(x-y)+1;
  l++;
  sympoint(x,y,xc,yc,l);
}while(x<y);
}
void sympoint(int x,int y,int xc,int yc,int l)
{
int z=6;//**********
putpixel(xc+x,yc+y,14);
putpixel(xc-x,yc+y,14);
putpixel(xc+x,yc-y,14);
putpixel(xc-x,yc-y,14);
putpixel(xc+y,yc+x,14);
putpixel(xc-y,yc+x,14);
putpixel(xc+y,yc-x,14);
putpixel(xc-y,yc-x,14);
if(l%5==0)//*********
{
Ngdpt(xc+x,yc+y,z);
Ngdpt(xc-x,yc+y,z);
Ngdpt(xc+x,yc-y,z);
Ngdpt(xc-x,yc-y,z);
Ngdpt(xc+y,yc+x,z);
Ngdpt(xc-y,yc+x,z);
Ngdpt(xc+y,yc-x,z);
Ngdpt(xc-y,yc-x,z);
}
}
//*********NEAREST_GRID_POINT_CALCULATION_FUNCTIONS**********************//

void Ngdpt(int x,int y,int z)//nearest grid point search.
{
    int i,j,p1=2000,p2=2000,m=0;
    int d,g=1000;
   for(i=x-z;i<=x+z;i+=1)
    {
       for(j=y-z;j<=y+z;j+=1)
	 {
	   if(getpixel(i,j)==15)
		{
		   d=distance(x,y,i,j);
		   m=mini(g,d);
		   if(m==1)
		      {
			g=d;
			p1=i;
			p2=j;

		      }
	      }
	}

}
if(z==13)
{
setcolor(14);
//circle(p1,p2,1);
//circle(p1,p2,2);
//circle(p1,p2,3);
//circle(p1,p2,4);
circle(p1,p2,5);
}
else
{save_numbrl(p1,p2);//save numbrl to array nb.
 put_pixel(p1,p2);
 }

}
int mini(int d,int k)//return minumum value
{
    if(d>k)
       return 1;
    else
       return 0;
}

//******NUMBRL_CALCULATION_&_SAVING_STORING_SHOWING_FUNCTIONS************//

int cellx(int c) //return cell value of x direction;
{int i,j=0,d;
  for(i=0;i<42;i++)
 {
     for(j=0;j<36;j++)
     {
       if(c==a[i][j])
       {
	  d=i/2;
	}

      }
   }return (d+1);
 }
int celly(int c)//return cell value of y direction;
{int i,j,d;
  for(i=0;i<42;i++)
 {
    for(j=0;j<36;j++)
    {
     if(c==b[i][j])
       {
	  d=j/3;

	}

      }
  }return (d+1);
}

int search(int x,int y)//search for each cell position numbrl values;
{ int i,j,p;
  for(i=0;i<42;i++)
 {
  for(j=0;j<36;j++)
  {
     if(x==a[i][j] && y==b[i][j])
      if(i%2==0)
	  {if(j%3==0)
	    p=10;
	  else if(j%3==1)
	    p=20;
	  else
	   p=40;}
     else
	{ if(j%3==0)
	    p=1;
	  else if(j%3==1)
	    p=2;
	  else
	    p=4;
	    }


    }
  }
 return p;
 }
void save_numbrl(int x,int y)//save each cell's numbrl values to nb array.
{
nb[cellx(x)][celly(y)]+=search(x,y);
}
void show_numbrl()//show numbrl value for each cell
{
int i,j,k;
FILE *f;
f=fopen("c:/project/Braille.txt","w"); //file open
fprintf(f,"----------------------------------------------------------------------------------------------------------\n");
fprintf(f,"\t\t<<<< %s >>>>",c);
for(j=1;j<12;j++)

{ k=1;
  for(i=1;i<22;i++)
 {
  if(nb[i][j]!=0)
   {  if(k==1)
     fprintf(f,"Row(%d) ::",j);
     k++;
   fprintf(f,"\t(%d,%d,%d),",i,j,nb[i][j]);//save in the file
  }
 }
 fprintf(f,"\n");
 }
fclose(f); //close the file
}

//**************************MAIN_FUNC******************************//

int main()
{
 float x[10],y[10];
  int n,m,k,i,l;
    /* request auto detection */
  int gdriver = DETECT, gmode;
   /* initialize graphics mode */
  initgraph(&gdriver, &gmode, "c:\\tc\\bgi" );
  zero();// function to initialise the numbrl array nb[8][8];

printf("   Enter 1 for line\n\t 2 for circle \n\t 3 triangle  \n\t 4 parallelogram \n\t 5 for regular polygon::");
scanf("%d",&n);
cleardevice();
print_braille_grid();
switch(n)
{
case 1:
     strcpy(c,"LINE");
     Ngdpt(250,250,13);
     draw_line(100,100,400,400);
     outtextxy(100,100," A");
     outtextxy(400,400," B");

     break;
case 2:
     strcpy(c,"CIRCLE");
     Ngdpt(300,200,13);
     midpoint_circle(300,200,100);
     outtextxy(300,200,"C");
     break;
case 3:
     strcpy(c,"TRIANGLE");
     Ngdpt(midpoint(100,300),midpoint(100,300),13);
     Ngdpt(midpoint(100,300),midpoint(300,300),13);
     Ngdpt(midpoint(100,100),midpoint(300,100),13);
     draw_line(100,100,300,300);
     draw_line(100,300,300,300);
     draw_line(100,300,100,100);
     outtextxy(100,300," A");
     outtextxy(100,100," B");
     outtextxy(300,300,"C");
     break;

case 4:
     strcpy(c,"PARALLEOGRAM");
    // Ngdpt(midpoint(200,100),midpoint(100,200),13);
     //Ngdpt(midpoint(100,300),midpoint(200,200),13);
     //Ngdpt(midpoint(400,300),midpoint(100,200),13);
     Ngdpt(midpoint(100,300),midpoint(100,100),13);
     draw_line(50,200,100,100);  //AB
     draw_line(100,100,300,100);  //BC
     draw_line(300,100,250,200);  //CD
     draw_line(250,200,50,200);  //DA
   /*  outtextxy(100,200,"A");
     outtextxy(200,100,"B");
     outtextxy(400,100,"C");
     outtextxy(300,200,"D"); */
     break;	     //for parallelogram

case 5:
strcpy(c,"POLYGON");
Ngdpt(midpoint(200,120),midpoint(100,200),13);
Ngdpt(midpoint(120,120),midpoint(200,300),13);
Ngdpt(midpoint(120,200),midpoint(300,400),13);
Ngdpt(midpoint(200,280),midpoint(400,300),13);
Ngdpt(midpoint(280,280),midpoint(300,200),13);
Ngdpt(midpoint(280,200),midpoint(200,100),13);
draw_line(200,100,120,200);
draw_line(120,200,120,300);
draw_line(120,300,200,400);
draw_line(200,400,280,300);
draw_line(280,300,280,200);
draw_line(280,200,200,100);break;
default:break;
}
show_numbrl();//this will show u the cell no. with cell position values;
getch();
closegraph();
return 0;
}

We implement this code to draw diagram in selecting grid as in braille system.now we want to minimize to select the points and to make better of these diagram.....

Edited by WaltP: Added CODE Tags -- please use them

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.