The equation is not graphing properly, how do I fix it? I am trying to make a program that accepts a quadratic equation from a user, solves the equation and also graphs the equation. I am pretty new to Processing so if I made any obvious mistakes, please help me to point it out. I adapted the code from the graph from the grafica library but I can't seem to implement it properly. Here is my code:
import javax.swing.JOptionPane; import grafica.*; GPlot plot1; String input;
void setup(){ size(500, 500); background(255);
float[] firstPlotPos = new float[] {0, 0}; float[] panelDim = new float[] {200, 200}; float[] margins = new float[] {60, 70, 40, 30};
GPlot plot1 = new GPlot(this); plot1.setPos(firstPlotPos); plot1.setMar(0, margins[1], margins[2], 0); plot1.setDim(panelDim); plot1.setAxesOffset(0); plot1.setTicksLength(-4); plot1.getXAxis().setDrawTickLabels(false);
String num = JOptionPane.showInputDialog(null,"Enter the coefficient of x²: ", "Text input", JOptionPane.QUESTION_MESSAGE); int num1=Integer.parseInt(num); String num4 = JOptionPane.showInputDialog(null,"Enter the coefficient of x: ", "Text input", JOptionPane.QUESTION_MESSAGE); int num2=Integer.parseInt(num4); String num5 = JOptionPane.showInputDialog(null,"Enter the constant: ", "Text input", JOptionPane.QUESTION_MESSAGE); int num3=Integer.parseInt(num5);
println(quadratic(num1, num2, num3)); println(quadratic1(num1, num2, num3));
int nPoints = 21; GPointsArray points1 = new GPointsArray(nPoints);
for (int i = 0; i < nPoints; i++) { points1.add(i,(num1((i)(i))+(num2*(i))+num3)); }
plot1.setPoints(points1); plot1.setTitleText("Quadratic equation graph"); plot1.getTitle().setRelativePos(1); plot1.getTitle().setTextAlignment(CENTER); plot1.getYAxis().setAxisLabelText("Y Axis");
plot1.beginDraw(); plot1.drawBox(); plot1.drawXAxis(); plot1.drawYAxis(); plot1.drawTopAxis(); plot1.drawRightAxis(); plot1.drawTitle(); plot1.drawPoints(); plot1.drawLines(); plot1.endDraw(); }
float quadratic(float a, float b, float c){ return (-b + sqrt( bb - 4ac)) / (2a); } float quadratic1(float d, float e, float f){ return (-e - sqrt( ee - 4df)) / (2d); }