# How to plot polar function onto cartesian grid? [closed]

I have this polar function:

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

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

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

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

thanks

-

## migration rejected from stackoverflow.comApr 4 '15 at 4:44

This question came from our site for professional and enthusiast programmers. Votes, comments, and answers are locked due to the question being closed here, but it may be eligible for editing and reopening on the site where it originated.

## closed as off-topic by Jonas Meyer, Sally, Newb, David K, Zev ChonolesApr 4 '15 at 4:44

This question appears to be off-topic. The users who voted to close gave these specific reasons:

• "This question is missing context or other details: Please improve the question by providing additional context, which ideally includes your thoughts on the problem and any attempts you have made to solve it. This information helps others identify where you have difficulties and helps them write answers appropriate to your experience level." – Newb, Zev Chonoles
• "This question is not about mathematics, within the scope defined in the help center." – Jonas Meyer, Sally, David K
If this question can be reworded to fit the rules in the help center, please edit the question.

-1. Seriously look at en.wikipedia.org/wiki/Polar_coordinate_system and find out how to convert (r,t) into (x,y). If you have a programming question then post it again at SO. – ja72 Jan 1 '11 at 21:39
This is not a simple matter of converting polar to cartesian so this is in fact a programming question. If you would have bothered to use your brain you could've probably figured that out by reading my answer to this question which I posted about 43 minutes before your unnecessary comment and downvote. Jerk. – Raoul Duke Jan 1 '11 at 21:58
Whether it is a valid description doesn't change the fact that it is unprofessional. I also don't understand why this question was migrated. – Qiaochu Yuan Jan 2 '11 at 15:03
– Tobias Kienzler Jan 6 '11 at 8:34

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)

-
Thanks for your answer but I'm sorry to say this is not very helpful. This won't produce a smooth curve. How would you do that exactly with this pseudocode? Also please elaborate on how to iterate from 0 to 2pi because this is a continuous range of real numbers this isn't as easy as your pseudocode makes it out to be. – Raoul Duke Jan 1 '11 at 20:10
The answer exactly matches the question. Never in the original question metions that the answer has to produce a smooth curve (to what criteria?) – ja72 Jan 1 '11 at 21:34
@ jalexiou I wrote "so I get an image like the one above" – Raoul Duke Jan 1 '11 at 22:00

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.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.

-