این برنامه 12 عدد از کاربر میگیره نمودار خطی و میله ای رسم میکنه ترسیم خط بروش dda
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <string.h>
#include <iostream.h>
// Algoritm DDA
void linedda(int x1,int y1,int x2, int y2,int c)
{
int dx=x2-x1;
int dy=y2-y1;
float x=x1;
float y=y1;
float xi,yi,step,k;
if (fabs(dx)>fabs(dy))
step=abs(dx);
else
step=abs(dy);
xi=dx/step;
yi=dy/step;
for(k=0;k<=step;k++)
{
putpixel(floor(x),floor(y),c);
x+=xi;
y+=yi;
}
}
void gui()
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
/* initialize graphics mode */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* return with error code */
}
}
int guie()
{
getch();
closegraph();
return 0;
}
void bound(int x,int y,int f,int b)
{
int cu=getpixel(x,y);
if (cu!=b)
if(cu!=f)
{
putpixel(x,y,f);
bound(x+1,y,f,b);
bound(x,y+1,f,b);
bound(x,y-1,f,b);
bound(x-1,y,f,b);
}
}
void bar(int b,int c)
{
linedda(c,440,c,b,5);
linedda(c+10,440,c+10,b,5);
linedda(c,b,c+10,b,5);
linedda(c,440,c+10,440,5);
bound(c+1,439,10,5);
}
int main(void)
{
const n=12;
int v[n],i,j,x,x1;
char *s;
/* draw a line */
for (i=0;i<n;i++)
{
printf("Please enter number (%d): ",i+1);
cin>>x;
if (x<=100)
v[i]=x;
else
v[i]=x%100;
}
gui();
cleardevice();
outtextxy(250,10,"In The Name of God");
outtextxy(400,470,"(Zalaghi,Bonyadi,Mahdavi)");
// bar diagram
for(i=240,j=100;i<=420;i+=20,j-=10)
{
linedda(100,i,100,i+20,10);
linedda(98,i,102,i,15);
switch (j)
{
case 100:s="100";
break;
case 90 :s="90";
break;
case 80:s="80";
break;
case 70 :s="70";
break;
case 60:s="60";
break;
case 50 :s="50";
break;
case 40:s="40";
break;
case 30 :s="30";
break;
case 20:s="20";
break;
case 10 :s="10";
break;
}
outtextxy(70,i-3,s);
}
linedda(98,440,102,440,15);
outtextxy(70,437,"0");
linedda(100,440,400,440,10);
for(i=0,j=130;i<n;i++,j+=20)
{
x=v[i]/10*20+(v[i]%10)*2;
bar(440-x,j);
}
// Linear Diagram
for(i=20,j=100;i<=200;i+=20,j-=10)
{
linedda(100,i,100,i+20,10);
linedda(98,i,102,i,15);
switch (j)
{
case 100:s="100";
break;
case 90 :s="90";
break;
case 80:s="80";
break;
case 70 :s="70";
break;
case 60:s="60";
break;
case 50 :s="50";
break;
case 40:s="40";
break;
case 30 :s="30";
break;
case 20:s="20";
break;
case 10 :s="10";
break;
}
outtextxy(70,i-3,s);
}
linedda(98,220,102,220,15);
outtextxy(70,217,"0");
linedda(100,220,400,220,10);
for(i=0,j=130;i<n-1;i++,j+=20)
{
x=(v[i]/10)*20+(v[i]%10)*2;
x1=(v[i+1]/10)*20+(v[i+1]%10)*2;
//putpixel(j,220-x,15);
linedda(j,220-x,j+20,220-x1,15);
}
/* clean up */
guie();
}