gpt4 book ai didi

java - 如何用方法绘制外旋轮线

转载 作者:行者123 更新时间:2023-12-01 13:55:58 25 4
gpt4 key购买 nike

我是java新手,这是第一次使用方法。我一直在尝试创建一个外旋轮线,但它只输出一条直线。我的方法有问题吗?如果是这样,我正在寻求任何解决问题的提示/提示。

我已经在下面发布了相关代码。

  public class myEpitrochoid{

public myEpitrochoid( ) {

double a = 50;
double b = 5;
double k = 10;
int num = 100;

drawEpitrochoid( a, b, k, num);

}

private void drawEpitrochoid (double a, double b, double k, int num) {

for ( int t=1 ; t<=num ; t++ ) {

t = t/num;
double x = (a+b)*cos(2*PI*t) - k * cos(2*PI*(a+b)*(t/b));
double y = (a+b)*sin(2*PI*t) - k * sin(2*PI*(a+b)*(t/b));
yertle.moveTo(x,y);

}

}

}

最佳答案

这是一个工作示例

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Path2D;
import java.beans.Transient;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;


@SuppressWarnings("serial")
public class EpitrochoidDemo extends JPanel {

private Path2D.Double points = new Path2D.Double();
private double t = 0.0;
private double a = 50;
private double b = 5;
private double k = 1.7;
private Ellipse2D.Double stationaryCircle;
protected double num = 100.0;

public EpitrochoidDemo() {
setBackground(Color.black);
stationaryCircle = new Ellipse2D.Double(400 - a, 400 - a,
2 * a, 2 * a);
nextPoint();

}


public void nextPoint() {
double x = (a + b) * Math.cos(2 * Math.PI * t) - k
* Math.cos(2 * Math.PI * (a + b) * (t / b));
double y = (a + b) * Math.sin(2 * Math.PI * t) - k
* Math.sin(2 * Math.PI * (a + b) * (t / b));
x += 400;
y += 400;

// The Path2D needs an initial point
if (t == 0.0)
points.moveTo(x, y);
// Draw a line from previous point to this point
else
points.lineTo(x, y);
t += 0.001;
}

@Override
@Transient
public Dimension getPreferredSize() {
return new Dimension(800, 800);
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();

// Pretty graphics
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

// Draw axis and stationary circle and timer in corner
g2d.setColor(Color.white);
g2d.drawString("t=" + t, 10, 10);
g2d.draw(new Line2D.Double(0, 400, 800, 400));
g2d.draw(new Line2D.Double(400, 0, 400, 800));
g2d.draw(stationaryCircle);
g2d.setColor(Color.red);
g2d.draw(points);
}

public static void main(String[] args) {
JFrame frame = new JFrame();
final EpitrochoidDemo e = new EpitrochoidDemo();

frame.getContentPane().add(e);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);

final Timer timer = new Timer(15, new ActionListener() {

@Override
public void actionPerformed(ActionEvent arg0) {
e.nextPoint();
e.repaint();

if (e.t >= e.num) {
((Timer) arg0.getSource()).stop();
}

}
});
timer.start();
}
}

我使用与您相同的公式(也发布了 here ),如图所示,它工作得很好 enter image description here

关于java - 如何用方法绘制外旋轮线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19624528/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com