如何将极函数绘制到笛卡尔网格上?

发布于 2024-10-10 03:07:58 字数 274 浏览 0 评论 0原文

我有这个极坐标函数:

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

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

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

Sample graph

我怎样才能将这个函数绘制到笛卡尔坐标网格上,这样我就可以得到像上面这样的图像,

谢谢?

I have this polar function:

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

where A, B, N are arbitrary parameters and t is the angle theta in the polar coordinate system.

Example graph for A=8, B=0.5, N=4

Sample graph

How can I plot this function onto a Cartesian coordinate grid so I get an image like the one above?

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情何以堪。 2024-10-17 03:07:58

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

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)

Incomplete pseudocode sample, but you should get the idea:

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)
喵星人汪星人 2024-10-17 03:07:58

好吧,我明白了。一些 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)));
    }
}

我没想到它会创建平滑的曲线,但它效果很好。

替代文本
替代文本

Ok, I figured it out. Some example Java code:

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

I didn't expect this to create smooth curves but it works pretty well.

alt text
alt text

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文