0

特定の範囲(ユーザーが入力)の変数tを使用して[x(t)、y(t)]をプロットするプログラムを作成しています。これまで、t、x(t)、y(t)の値を保持する3つのベクトルを作成しました。私の現在のアプローチは、ポイントのベクトル(約1000ポイント)を作成してから、ベクトル内の2つの隣接するポイントの間に線(またはパス)を描画することです。しかし、結果は私が期待したものではありません。

ユーザーからの完全な形式:

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);
}

そして、これが上記の2つの関数で得られるものです。 ここに画像の説明を入力してください

質問: プロットをより良くする(スケールアップする)方法はありますか?!

4

1 に答える 1

2

パラメトリック方程式を間違ってグラフ化しているように見えます。

パラメトリック方程式の場合、tmintからtmaxまで繰り返す必要があります。各値で、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);
于 2013-03-11T22:01:18.827 に答える