我怎样画一个双阿基米德螺旋?

问题描述 投票:2回答:1

据我们的老师说,这张照片是一个马来酰亚胺螺旋:archimidean

问题是在互联网上我搜索方法绘制阿基米德螺旋,我只找到这样的东西:qazxsw poi

所以我不知道如何绘制像第一张图像的东西,我已经尝试过的是在某种程度上构建一个螺旋,然后在相反的方向上放置相同的螺旋,但它没有用,我使用的代码来自enter image description here

Java: Draw a circular spiral using drawArc

但如果我试图以相反的方式放置相同的螺旋,它就不起作用所以我迷失了。

java swing awt java-2d
1个回答
2
投票

我用来实现的技巧就是使用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(); } } 让螺旋线朝每个部位的不同方向(顺时针/逆时针)方向移动。它用于调整螺旋中点的x / y值。例如。一个螺旋中心点右上角的值将在另一个螺旋中左下方。

directionMuliplier

这就是如何调用该方法来生成可用于绘制方法的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()); }

© www.soinside.com 2019 - 2024. All rights reserved.