我怎样才能画出双阿基米德螺线?
How can I draw a double archimedean spiral?
根据我们老师的说法,这张图片是阿基米德螺线:
问题是我在网上搜索了阿基米德螺线的绘制方法,结果只找到了这样的东西:
所以我不知道如何绘制像第一张图片那样的东西,我已经尝试过以某种方式构建一个螺旋,然后以另一种方式放置相同的螺旋,但它没有用,我使用的代码来自
public class ArchimideanSpiral extends JFrame {
public ArchimideanSpiral()
{
super("Archimidean Spiral");
setSize(500,500);
setVisible(true);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
public void paint(Graphics g)
{
int x = getSize().width / 2 - 10;
int y = getSize().height/ 2 - 10;
int width = 20;
int height = 20;
int startAngle = 0;
int arcAngle = 180;
int depth = 10;
for (int i = 0; i < 10; i++) {
width = width + 2 * depth;
y = y - depth;
height = height + 2 * depth;
if (i % 2 == 0) {
g.drawArc(x, y, width, height, startAngle, -arcAngle);
} else {
x = x - 2 * depth;
g.drawArc(x, y, width, height, startAngle, arcAngle);
}
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
new ArchimideanSpiral();
}
}
但是如果我尝试以相反的方式放置相同的螺旋线,它不起作用,所以我迷路了。
我用来实现的技巧是使用 directionMuliplier
让每个部分的螺旋方向不同(顺时针/逆时针)。它用于调整螺旋中点的 x/y 值。例如。一个螺旋中中心点右上角的值,将在另一个螺旋中变为左下角。
private Point2D getPoint(double angle, int directionMuliplier) {
double l = angle*4;
double x = directionMuliplier * Math.sin(angle)*l;
double y = directionMuliplier * Math.cos(angle)*l;
return new Point2D.Double(x, y);
}
这就是如何调用该方法来生成可以在绘画方法中使用的 GeneralPath
。
GeneralPath gp = new GeneralPath();
gp.moveTo(0, 0);
// create the Archimmedian spiral in one direction
for (double a = 0d; a < Math.PI * 2 * rotations; a += step) {
Point2D p = getPoint(a, 1);
gp.lineTo(p.getX(), p.getY());
}
gp.moveTo(0, 0);
// now reverse the direction
for (double a = 0d; a < Math.PI * 2 * rotations; a += step) {
Point2D p = getPoint(a, -1);
gp.lineTo(p.getX(), p.getY());
}
它可能是这样的:
根据我们老师的说法,这张图片是阿基米德螺线:
问题是我在网上搜索了阿基米德螺线的绘制方法,结果只找到了这样的东西:
所以我不知道如何绘制像第一张图片那样的东西,我已经尝试过以某种方式构建一个螺旋,然后以另一种方式放置相同的螺旋,但它没有用,我使用的代码来自
public class ArchimideanSpiral extends JFrame {
public ArchimideanSpiral()
{
super("Archimidean Spiral");
setSize(500,500);
setVisible(true);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
public void paint(Graphics g)
{
int x = getSize().width / 2 - 10;
int y = getSize().height/ 2 - 10;
int width = 20;
int height = 20;
int startAngle = 0;
int arcAngle = 180;
int depth = 10;
for (int i = 0; i < 10; i++) {
width = width + 2 * depth;
y = y - depth;
height = height + 2 * depth;
if (i % 2 == 0) {
g.drawArc(x, y, width, height, startAngle, -arcAngle);
} else {
x = x - 2 * depth;
g.drawArc(x, y, width, height, startAngle, arcAngle);
}
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
new ArchimideanSpiral();
}
}
但是如果我尝试以相反的方式放置相同的螺旋线,它不起作用,所以我迷路了。
我用来实现的技巧是使用 directionMuliplier
让每个部分的螺旋方向不同(顺时针/逆时针)。它用于调整螺旋中点的 x/y 值。例如。一个螺旋中中心点右上角的值,将在另一个螺旋中变为左下角。
private Point2D getPoint(double angle, int directionMuliplier) {
double l = angle*4;
double x = directionMuliplier * Math.sin(angle)*l;
double y = directionMuliplier * Math.cos(angle)*l;
return new Point2D.Double(x, y);
}
这就是如何调用该方法来生成可以在绘画方法中使用的 GeneralPath
。
GeneralPath gp = new GeneralPath();
gp.moveTo(0, 0);
// create the Archimmedian spiral in one direction
for (double a = 0d; a < Math.PI * 2 * rotations; a += step) {
Point2D p = getPoint(a, 1);
gp.lineTo(p.getX(), p.getY());
}
gp.moveTo(0, 0);
// now reverse the direction
for (double a = 0d; a < Math.PI * 2 * rotations; a += step) {
Point2D p = getPoint(a, -1);
gp.lineTo(p.getX(), p.getY());
}
它可能是这样的: