the fact is that I'm trying to code an app that is able to graph math functions. The code makes graphics of square functions and is fine. My problem is that I want to make cubic functions. This is my code so far:
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
public class plano extends JPanel{
public plano() {
init();
}
public void init() {
this.setBorder(BorderFactory.createLineBorder(Color.black));
}
@Override
public void paintComponent( Graphics g ) {
super.paintComponent(g);
g.setColor(Color.red);
g.drawLine(0, this.getHeight()/2, this.getWidth(), this.getHeight()/2);
g.drawLine(this.getWidth()/2, 0,this.getWidth()/2 , this.getHeight());
}
// y=c*x2 +c*x +c
public void paintSQRFunc(Graphics g,double x3mult, double x2mult,
double x1mult,double cons, double x1,double x2)
{
for(double i=x1;i<x2;i++)
{
double y = ((double)Math.cbrt(i)*x3mult)+((double)Math.pow(i,2)*x2mult)+i*x1mult+cons;
double xp = i+1;
double yp = ((double)Math.cbrt(xp)*x3mult)+((double)Math.pow(xp,2)*x2mult)+xp*x1mult+cons;
g.drawLine((int)coord_x(i), (int)coord_y(y), (int)coord_x(xp), (int)coord_y(yp));
}
}
private double coord_x(double x)
{
double real_x = x+this.getWidth()/2;
return real_x;
}
private double coord_y(double y)
{
double real_y = -y+this.getHeight()/2;
return (real_y);
}
}
And then the main class where I call the methods:
import java.awt.event.ActionEvent;
import javax.swing.*;
import java.awt.event.ActionListener;
import graficadora.Graficadora;
public class Ventana extends JFrame implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
Graficadora.plano.paintSQRFunc(Graficadora.plano.getGraphics(),1,1,0,0,-200,200);
As you see, I tried to make it able to graph cubic functions. When I set the parameters to graph a cubic function, there is my mistake:
(It is supposed to graph y=x^3. The graph appears to be tilted in 90º.)