-6

我有这个极地功能:

r = A / log(B * tan(t / 2 * N)

其中 A、B、N 是任意参数,t 是极坐标系中的角度 theta。

示例图A=8, B=0.5, N=4

示例图

如何将此函数绘制到笛卡尔坐标网格上,以便获得与上述类似的图像?

谢谢

4

2 回答 2

2

不完整的伪代码示例,但您应该明白:

for t in [0, 2pi):
    r = /* whatever you got depending on t */
    x = r * cos(t)
    y = r * sin(t)
    draw line to (x,y)
于 2011-01-01T19:47:49.380 回答
1

好的,我想通了。一些示例 Java 代码:

import static java.lang.Math.*;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;


public class TestPolarPlot {
    public static void main(String[] args) {
    final int width = 512;
    final int height = 512;
    BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
    Graphics g = img.getGraphics();
    g.setColor(Color.black);
    g.fillRect(0, 0, width, height);
    g.setColor(Color.white);
    final double A = 8;
    final double B = 0.5;
    final double N = 4;
    final double scale = 128;
    final double zoom = 50;
    final double step = 1 / scale;
    Point last = null;
    final Point origin = new Point(width/2, height/2);

    for (double t = 0; t <= 2*PI; t+= step) {
        final double r = zoom * polarFunction(t, A, B, N);
        final int x = (int)round(r * cos(t));
        final int y = (int)round(r * sin(t));
        Point next = new Point(x, y);
        if (last != null) {
            g.drawLine(origin.x + last.x, origin.y + last.y,
                origin.x + next.x, origin.y + next.y);
        }
        last = next;
    }

    JFrame frame = new JFrame("testit");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(new JLabel(new ImageIcon(img)));
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}

    public static double polarFunction(double t, double A, double B, double N) {
        return A / log(B * tan(t / (2 * N)));
    }
}

我没想到这会产生平滑的曲线,但效果很好。

替代文字 替代文字

于 2011-01-01T20:56:23.573 回答