We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi everyone! I'm working on a function plotter, utilizing controlP5 as an interface, so that I can easily manipulate the values of the functions. The problem is that im generating points to make the function's line; to minimize the lag, I tried to reduce the number of points, but by doing so, the graph looks pretty bad (or, if the number of points is low enough, there is not even a clear line). I need some way that I can connect those points one to the next one, so the function still looks clear.
import controlP5.*;
import java.util.*;
PVector middle;
float numbera = 0;
float numberb = 0;
float numberc = 0;
float numberd = 0;
ControlP5 cp5;
Slider mySlider;
Knob Knob;
void setup()
{
size(800, 800);
middle = new PVector(width/2, height/2, 0);
cp5 = new ControlP5(this);
cp5.addSlider("numbera")
.setPosition(0, 0)
.setSize(width, height/50)
.setRange(-1000, 1000)
.setSliderMode(Slider.FLEXIBLE)
.setValue(0)
;
cp5.addSlider("numberb")
.setPosition(0, height/50)
.setSize(width, height/50)
.setRange(-5, 5)
.setSliderMode(Slider.FLEXIBLE)
.setValue(0)
;
cp5.addSlider("numberc")
.setPosition(0, (height/50)*2)
.setSize(width, height/50)
.setRange(-100, 100)
.setSliderMode(Slider.FLEXIBLE)
.setValue(0)
;
cp5.addSlider("numberd")
.setPosition(0, (height/50)*3)
.setSize(width, height/50)
.setRange(-height, height)
.setSliderMode(Slider.FLEXIBLE)
.setValue(0)
;
Knob = cp5.addKnob("grade")
.setRange(1, 4)
.setValue(0)
.setPosition(10, height/2+height/3)
.setRadius(50)
.setNumberOfTickMarks(3)
.snapToTickMarks(true)
.setDragDirection(Knob.VERTICAL)
;
}
void draw()
{
frameRate(60);
colorMode(RGB);
int grade = int(cp5.get(Knob.class, "grade").getValue());
float numbera = -(1/(cp5.get(Slider.class, "numbera").getValue()));
float numberb = (cp5.get(Slider.class, "numberb").getValue());
float numberc = -(cp5.get(Slider.class, "numberc").getValue());
float numberd = -(cp5.get(Slider.class, "numberd").getValue());
background(200);
String desc = "f(x) = "+(int(-1/numbera))+"X³ +("+(int(numberb))+")X² +("+(int(-numberc))+")X+("+(int(-numberd))+")";
textSize(20);
text(desc, 0, 85);
ellipse(middle.x, middle.y, 5, 5);
fill(0);
line(middle.x, height, middle.x, 0);
line(width, middle.y, 0, middle.y);
stroke(0);
switch(grade)
{
case(1):
{
linearPoints(numberc, numberd, -height, height);
break;
}
case(2):
{
quadraticPoints(numberb, numberc, numberd, -height, height);
break;
}
case(3):
{
cubicPoints(numbera, numberb, numberc, numberd, -height, height);
break;
}
}
stroke(0);
}
void linearPoints(float a, float b, float xmin, float xmax)
{
float y;
for (float x = xmin; x < xmax; x = x + 0.09)
{
y = a*x + b;
point(middle.x+x, middle.y+y);
stroke(255, 0, 0);
}
}
void quadraticPoints(float a, float b, float c, float xmin, float xmax)
{
float y;
for (float x = xmin; x < xmax; x = x + 0.8)
{
y = a*x*x + b*x + c;
point(middle.x+x, middle.y+y);
stroke(255, 0, 0);
textSize(20);
}
}
void cubicPoints(float a, float b, float c, float d, float xmin, float xmax)
{
float y;
for (float x = xmin; x < xmax; x = x+0.02)
{
y = a*x*x*x + b*x*x + c*x + d;
point(middle.x+x, middle.y+y);
stroke(255, 0, 0);
}
}
Answers
These past discussions might be relevant:
Thanks!!