java - Java 中的绘图函数

标签 java graph plot graphics2d

我正在编写一个程序来绘制[x(t),y(t)],其中变量t在一定范围内(由用户输入)。到目前为止,我已经创建了 3 个 vector 来保存 t、x(t) 和 y(t) 的值。我当前的方法是创建点 vector (大约 1000 个点),然后在 vector 中的两个相邻点之间绘制一条线(或路径)。然而,结果并不如我所料。

来自用户的完整格式:

Plot [x(t),y(t)] x=a..b y=a..b t=a..b where a,b are the range of x,y,t 

例如,用户可以输入函数:

x(t) = 5*sin(3t + 5), t=-19..19
y(t) = 4*cos(2t), t=-19.19

这是我的绘图代码:

public static void drawGraph(String sf, String sx, String sy, String st) {
  JFrame mygraph = new JFrame("PlotGraph v0.1");

  final Vector<Double> range_t = getRangeT(st); //get the range of t
  //create a corresponding vectors of x and y based on values of t
  final Vector<Double> range_x = getRangeX(range_t,funcX,var);
  final Vector<Double> range_y = getRangeY(range_t,funcY,var);

  //draw the graph to a JPanel, our graph is actually just a collection of points connecting 2 points
  mygraph.add(new JPanel() {


 public void paintComponent(Graphics g) {
          super.paintComponent(g);
          Graphics2D g2 = (Graphics2D) g;      g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
          g2.setColor(Color.BLUE );
          g2.setStroke(new BasicStroke(1));
          GeneralPath gpx = new GeneralPath();
          GeneralPath gpy = new GeneralPath();
          for (int i=0; i<998; i++) {
            gpy.moveTo(range_t.get(i),range_y.get(i));
                  gpy.curveTo(range_t.get(i),range_y.get(i),range_t.get(i+1),range_y.get(i+1),range_t.get(i+2),range_y.get(i+2));
            gpx.moveTo(range_t.get(i),range_x.get(i));
            gpx.curveTo(range_t.get(i),range_x.get(i),range_t.get(i+1),range_x.get(i+1),range_t.get(i+2),range_x.get(i+2));

            //g2.draw(lineY);
            g2.draw(gpx);
            g2.draw(gpy);
          }
          g2.dispose(); 
        }
      });

  mygraph.setBounds(125,25,650,600);
  mygraph.setLocationRelativeTo(null);
  mygraph.setDefaultCloseOperation(mygraph.EXIT_ON_CLOSE);
  mygraph.setVisible(true);
}

这是我从上面两个函数得到的结果: enter image description here

问题: 有什么办法可以使情节更好(按比例放大)?!

最佳答案

在我看来,您绘制的参数方程是错误的。

对于参数方程,您应该从 tmin 到 tmax 迭代 t。在每个值处,计算 x(t) 和 y(t),并在此处绘制一个点 - (x(t), y(t))。

看起来您正在 (t, y(t)) 等处绘制点。例如,您需要一个辅助函数来评估每个变量的用户输入函数:

public double evaluateX(double t) { ... }
public double evaluateY(double t) { ... }

在这些函数中,您必须将用户的文本解析为代码(也许是标记化?),然后对其进行评估。

然后,你可以有一个这样的循环:

GeneralPath gp = new GeneralPath();
double tstep = (tmax - tmin) / 998;
for (double t=tmin; t += tstep; t<tmax) {
    double x = evaluateX(t);
    double y = evaluateY(t);
    if (t == 0) {
        gp.moveTo(x, y);
    } else {
        gp.lineTo(x, y);
    }
}
g2d.draw(gp);

从这里开始,比例因子应该很容易实现。尝试这样的事情:

GeneralPath gp = new GeneralPath();
final double scale = 3.0;
double tstep = (tmax - tmin) / 998;
for (double t=tmin; t += tstep; t<tmax) {
    double x = scale * evaluateX(t);
    double y = scale * evaluateY(t);
    if (t == 0) {
        gp.moveTo(x, y);
    } else {
        gp.lineTo(x, y);
    }
}
g2d.draw(gp);

关于java - Java 中的绘图函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15349165/

相关文章:

java - 迭代有向边的ArrayList的HashMap,提取有向边数据

java - 等价于 Java 中的 const(C++)

java - 如何在使用 Collat​​or 排序时避免忽略连字符

c++ - Gnuplot 持久图形窗口

c++ - 我使用哪种增强图算法?

python - matplotlib 基于额外变量绘制(不是散点)颜色

java - 在运行时更改 jar/war 文件中的 config.properties 文件并热部署更改?

java - 寻找具有良好 API 的 Java 模糊测试库或模糊测试工具

python - Matplotlib 扩大x轴标签之间的空间

matlab - Matlab 中的正负 Log10 比例 Y 轴