I'm working on a function-graphing program. The problem I've encountered is defining the function. I'd like to have the user input the equation, but I don't know what sort of data type could hold this information in a usable way. Below is an example of how it works, using a simple quadratic function. You'll notice I defined the function as (x*x). This is because (x^2) refused to function properly.
EDIT: Solved x^2 issue with pow(base,exponent) from math.h
#define scaleM 10
#define toGraph (x*x)
int scale=0;
int x=0;
int xOne,xTwo,yOne,yTwo;
#include <DarkGDK.h>
void DarkGDK(void){
dbSetDisplayMode(800,600,32);
dbSetWindowSize(800,600);
dbSetWindowTitle("GraphFunction by Mike Perron");
dbSyncOn();dbSyncRate(0);
dbSync();
//Draw Axis and scale markers
dbCLS(dbRGB(0,0,0));
dbInk(dbRGB(255,0,0),dbRGB(255,255,255));
dbLine(400,0,400,600);
dbLine(0,300,800,300);
dbInk(dbRGB(255,255,255),dbRGB(0,0,0));
while(scale<=60){
dbLine(399,scale*scaleM,401,scale*scaleM);
scale++;
}
scale=0;
while(scale<=80){
dbLine(scale*scaleM,299,scale*scaleM,301);
scale++;
}
dbInk(dbRGB(120,120,120),dbRGB(0,0,0));
dbText(0,580,"1:1 scale");
dbSync();
//Draw defined function
dbSetWindowTitle("Drawing... (Hold space to draw without syncing)");
dbInk(dbRGB(255,255,255),dbRGB(255,255,255));
x=-400;
while((x<=400)&&(LoopGDK())){
xOne=x+400;
yOne=300-(toGraph/scaleM);
dbDot(xOne,yOne);
x++;
xTwo=x+400;
yTwo=300-(toGraph/scaleM);
dbLine(xOne,yOne,xTwo,yTwo);
if(!dbSpaceKey()){dbSync();}
}
dbSetWindowTitle("GraphFunction by Mike Perron");
//Chill
while(LoopGDK()){
dbSync();
}
return;
}
My site, for various stuff that I make.