2

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º.)

image

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • 2
    Two approaches are examined [here](http://stackoverflow.com/a/20556929/230513); neither uses `getGraphics()`, which should not be used in the manner shown. – trashgod Dec 21 '13 at 13:02

1 Answers1

2

It is supposed to graph y=x^3. The graph appears to be tilted in 90º.

I didn't try your code but I strongly suspect the problem is here:

double yp = ((double)Math.cbrt(xp)*x3mult) + // <-- Math.cbrt() ?
            ((double)Math.pow(xp,2)*x2mult)+xp*x1mult+cons;

If you want to display a third grade polynome then Math.cbrt(xp) doesn't make sense, as per Math.cbrt(double a) javadoc:

Returns the cube root of a double value.

This cube root just "rotates" the function graph in 90°. It should be:

double yp = ((double)Math.pow(xp,3)*x3mult) + 
            ((double)Math.pow(xp,2)*x2mult)+xp*x1mult+cons;

Also consider take a look to @trashgod examples shown here. Or you may want to take a look to third-party libraries such as JFreeChart. There are some examples to start with shown here and here.

Community
  • 1
  • 1
dic19
  • 17,821
  • 6
  • 40
  • 69