Graphing functions

蓝咒 提交于 2020-01-17 04:35:13

问题


I'm new to Java in general, not to mention working with GUIs and I'm working on an assignment to graph the 3 functions:

(the four leafed cloves for cosine) with domain restrictions

0,4

-10,10

0 < x < 2pi

I've so far made a crude graph of the first 2 functions, and no idea how to plot the third.

What I have:

import java.awt.*;
import javax.swing.JComponent;
import javax.swing.JFrame;


public class DrawingStuff extends JComponent {

public void paintComponent(Graphics g)
{   
     //w is x, and h is y (as in x/y values in a graph)
     int w = this.getWidth()/2;
     int h = this.getHeight()/2;

 Graphics2D g1 = (Graphics2D) g;
 g1.setStroke(new BasicStroke(2));
 g1.setColor(Color.black);
 g1.drawLine(0,h,w*2,h);
 g1.drawLine(w,0,w,h*2); 
 g1.drawString("0", w - 7, h + 13);


 Graphics2D g2 = (Graphics2D) g;
 g2.setStroke(new BasicStroke(2));
  g2.setColor(Color.red);
  //line1
  Polygon p = new Polygon();
  for (int x = 0; x <= 4; x++) {
      p.addPoint(w+x, h - ((x*x*x) + x - 3));

  }
  g2.drawPolyline(p.xpoints, p.ypoints, p.npoints);

  Polygon p1 = new Polygon();
  for (int x = -10; x <= 10; x++) {
      p1.addPoint(w + x, h - ((x*x*x)/100) - x + 10);
  }
  g2.drawPolyline(p1.xpoints, p1.ypoints, p1.npoints); 
}

public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setSize(800, 600);
    frame.setTitle("Graphs");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null);  
    DrawingStuff draw = new DrawingStuff();
    frame.add(draw);
    frame.setVisible(true);
    }
}

which gives me

What I'd like to do now is graph the cosine function, which I'm not sure how to plot because it's cos(2x) and not 2cos(x).

I'd also like to change the scaling so that the segments don't look so tiny (and maybe graph markers? but not particularly important).

And I'd like to possibly add something so that only one function is displayed at a time.


回答1:


To add the last function, do the same as before (create a polygon with points, etc), but with your new function:

addPoint(w + x, h - Math.round(scale*Math.cos(2*x)))

It's a bit trickier than the others as Math.cos returns a double and addPoint takes an int. To get around this we round to the nearest int. However, the range of cosine, [-1,1], only contains three integers we can round to. This makes for an ugly cosine wave (one that looks like a triangle wave).

To alleviate this ugliness, a scale factor is used. A factor of say, 10, would make the range [-10,10], in which there are many more integers, leading to a smoother wave.

This scale factor is also useful for your other functions, namely to make them bigger as you wanted:

int scale = 10;
for (int x = 0; x <= 4; x++) {
    p.addPoint(w+scale*x, h - scale*((x*x*x) + x - 3));
}
//...lines skipped
for (int x = -10; x <= 10; x++) {
  p1.addPoint(w + scale*x, h - scale*((x*x*x)/100) - x + 10);
}

To display only one function at a time, just have if statements in your code.

Say you have a JRadioButton for each function:

if(button1.isSelected()){
    Polygon p = new Polygon();
    for (int x = 0; x <= 4; x++) {
        p.addPoint(w+x, h - ((x*x*x) + x - 3));
    }
    g2.drawPolyline(p.xpoints, p.ypoints, p.npoints);
}
else if(button1.isSelected()){
    //other polygon
}
//etc...



回答2:


+Building on the accepted answer to produce the requested cos(2x) graph.

    final double MAX_X = 2 * Math.PI;
    final double SCALE = w / MAX_X;

    for (int x = 0; x <= w; ++x) {
        double xScaled = x / SCALE;
        p.addPoint(w + x, h - (int)Math.round(SCALE * Math.cos(2 * xScaled)));
    }

Resulting cos(2x) graph in [0, 2pi]



来源:https://stackoverflow.com/questions/43970030/graphing-functions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!