Es buena solución integrar la función en la instrucción para dibujar los puntos, simplifica el sketch. Solo cuida el orden de los paréntesis en la fórmula, a veces falta uno que otro y saltan errores como conejos.
Hice los ajustes necesarios en el ejemplo
Usé la gráfica y=AsinBx. Se pueden modificar las constantes A y B, mediante los botones táctiles, como resultado la gráfica cambia en tiempo real.
Acá el sketch:
#include <GD2UB.h>
Bitmap title, xaxis, yaxis, Gtipo1E, Gtipo1, Gtipo2E, Gtipo2, Gtipo3E, Gtipo3, Gtipo4E, Gtipo4;
int graph=1;
int A=100;
int B=2;
//Marco para graficar
int PX0=50, PY0=50, PXMAX=PX0+410, PYMAX=PY0+230, PYBase= PY0+225;
void setup()
{
GD.begin();
title.fromtext(30, "Graph test");
xaxis.fromtext(28, "X axis");
yaxis.fromtext(28, "Y axis");
Gtipo1E.fromtext(26, " 2"); Gtipo1.fromtext(28, "y = 0.001x");
Gtipo2E.fromtext(26, " 2"); Gtipo2.fromtext(28, "y = 0.5x");
Gtipo3E.fromtext(26, " 2"); Gtipo3.fromtext(28, "y = 0.5x-0.0008x");
Gtipo4E.fromtext(26, " 2"); Gtipo4.fromtext(28, "y = AsinBx");
}
void loop()
{
GD.ClearColorRGB(0x000030);
GD.Clear(); GD.get_inputs();
GD.ColorRGB(0xffffff);
title.draw(PXMAX/2, PY0-20);
yaxis.draw(PX0-20, PYMAX/2, DEGREES(270));
xaxis.draw(PXMAX/2, PYMAX+20);
GD.Tag(1); Gtipo1E.draw(130+20, 90-25+PYMAX+20); Gtipo1.draw(100+20, 100-30+PYMAX+20); GD.Tag(255);
GD.Tag(2); Gtipo2.draw(88+20, 100+PYMAX+20); GD.Tag(255);
GD.Tag(3); Gtipo3E.draw(175+20, 100+25+PYMAX+20); Gtipo3.draw(130+20, 100+30+PYMAX+20); GD.Tag(255);
GD.Tag(4); Gtipo4.draw(100+20, 100+60+PYMAX+20); GD.Tag(255);
MarcoG();
if(graph==1){
GD.ColorRGB(0x50ff00); GD.Begin(LINE_STRIP); GD.LineWidth(24); Trace1();
GD.ColorRGB(0xffffff); GD.Begin(POINTS); GD.PointSize(4*16); Trace1Random();}
if(graph==2){
GD.ColorRGB(0x50ff00); GD.Begin(LINE_STRIP); GD.LineWidth(24); Trace2();
GD.ColorRGB(0xffffff); GD.Begin(POINTS); GD.PointSize(4*16); Trace2();}
if(graph==3){
GD.ColorRGB(0x50ff00); GD.Begin(LINE_STRIP); GD.LineWidth(24); Trace3();
GD.ColorRGB(0xffffff); GD.Begin(POINTS); GD.PointSize(4*16); Trace3();}
if(graph==4){
GD.ColorRGB(0x50ff00); GD.Begin(LINE_STRIP); GD.LineWidth(24); SIN();
GD.ColorRGB(0xffffff); GD.Begin(POINTS); GD.PointSize(1*16); SIN();
GD.cmd_fgcolor(0x005000);
GD.Tag(5); GD.cmd_button(500, 250, 120, 60, 29, 0, "B-"); GD.Tag(255);
GD.Tag(6); GD.cmd_button(660, 250, 120, 60, 29, 0, "B+"); GD.Tag(255);
GD.Tag(7); GD.cmd_button(580, 170, 120, 60, 29, 0, "A+"); GD.Tag(255);
GD.Tag(8); GD.cmd_button(580, 330, 120, 60, 29, 0, "A-"); GD.Tag(255);
if (GD.inputs.tag==5)
{
B=B-1;
if(B<=1){B=1;}
}
if (GD.inputs.tag==6)
{
B=B+1;
if(B>=10){B=10;}
}
if (GD.inputs.tag==7)
{
A=A+10;
if(A>=150){A=150;}
}
if (GD.inputs.tag==8)
{
A=A-10;
if(A<=50){A=50;}
}
}
if (GD.inputs.tag==1)
{
graph=1;
}
if (GD.inputs.tag==2)
{
graph=2;
}
if (GD.inputs.tag==3)
{
graph=3;
}
if (GD.inputs.tag==4)
{
graph=4;
}
GD.swap();
}
void Trace1()
{
for (float x = PX0+10; x < (PXMAX-10); x += 20) {
float y = PYBase - ((0.001*x*(x)));
GD.Vertex2f(x*16, y*16);
}
}
void Trace1Random()
{
for (float x = PX0+10; x < (PXMAX-10); x += 20) {
float y = PYBase - ((0.001*x*(x+random(-30,30))));
GD.Vertex2f(x*16, y*16);
}
}
void Trace2()
{
for (float x = PX0+10; x < (PXMAX-10); x += 20) {
float y = PYBase - ((0.5*x));
GD.Vertex2f(x*16, y*16);
}
}
void Trace3()
{
for (float x = PX0+10; x < (PXMAX-10); x += 20) {
float y = PYBase - ((0.5*x)-0.0008*x*x);
GD.Vertex2f(x*16, y*16);
}
}
void SIN(){
for ( long i = 0 ; i<=359 ; i++)
{
GD.Vertex2f((i+PX0)*16, (PYBase-(A*sin((B*i*PI)/180)))*16);
}
}
void MarcoG()
{
GD.Begin(LINES);
GD.Vertex2f(PX0*16, PY0*16); GD.Vertex2f(PX0*16, (PYMAX+170)*16);
GD.Vertex2f(PX0*16, (PYMAX-4)*16); GD.Vertex2f(PXMAX*16, (PYMAX-4)*16);
}
