Created
January 1, 2011 21:48
-
-
Save steos/762036 to your computer and use it in GitHub Desktop.
example of how to plot polar functions including a cool formula for describing spiral arms of galaxies
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import static java.lang.Math.*; | |
import java.awt.Color; | |
import java.awt.Graphics; | |
import java.awt.Image; | |
import java.awt.Point; | |
import java.awt.image.BufferedImage; | |
import javax.swing.ImageIcon; | |
import javax.swing.JFrame; | |
import javax.swing.JLabel; | |
public class PolarFunctionPlotter { | |
public static void main(String[] args) { | |
PolarFunctionPlotter plotter = new PolarFunctionPlotter(512, 512); | |
plotter.setForeground(Color.white); | |
plotter.setBackground(Color.black); | |
plotter.clear(); | |
// polar rose | |
plotter.setForeground(Color.pink); | |
plotter.setZoom(60); | |
plotter.setResolution(1024); | |
plotter.plot(new PolarFunction() { | |
@Override public double compute(double t) { | |
return 4 * cos(8 * t + 3); | |
} | |
}); | |
// limacon | |
plotter.setForeground(Color.yellow); | |
plotter.setZoom(40); | |
plotter.setResolution(128); | |
plotter.plot(new PolarFunction() { | |
@Override public double compute(double t) { | |
return 2 + 3 * sin(t); | |
} | |
}); | |
// spiral arm given by r = A / log(B*tan(t/2*N)) | |
// props to Harry I. Ringermacher and Lawrence R. Mead | |
// http://arxiv.org/pdf/0908.0892 (warning: it's a PDF) | |
plotter.setZoom(20); | |
plotter.setForeground(Color.red); | |
plotter.plot(new PolarFunction() { | |
@Override public double compute(double t) { | |
return 8 / log(0.5 * tan(t / (2 * 4))); | |
} | |
}); | |
final JFrame frame = new JFrame("PolarFunctionPlotterTest"); | |
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
frame.getContentPane().add(new JLabel(new ImageIcon(plotter.getCanvas()))); | |
frame.pack(); | |
frame.setLocationRelativeTo(null); | |
frame.setVisible(true); | |
} | |
public static interface PolarFunction { | |
public double compute(double t); | |
} | |
private double zoom; | |
private double resolution; | |
private int width; | |
private int height; | |
private Color background; | |
private Color foreground; | |
private final BufferedImage canvas; | |
private final Point origin; | |
public PolarFunctionPlotter(int width, int height) { | |
zoom = 10; | |
resolution = 32; | |
foreground = Color.black; | |
background = Color.white; | |
canvas = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR); | |
origin = new Point(width / 2, height / 2); | |
this.width = width; | |
this.height = height; | |
} | |
public void clear() { | |
final Graphics g = canvas.getGraphics(); | |
g.setColor(background); | |
g.fillRect(0, 0, width, height); | |
} | |
public void plot(PolarFunction fn) { | |
final Graphics g = canvas.getGraphics(); | |
g.setColor(foreground); | |
final double step = 1 / resolution; | |
Point last = null; | |
for (double t = 0; t <= 2 * Math.PI; t+= step) { | |
final double r = zoom * fn.compute(t); | |
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; | |
} | |
} | |
public double getZoom() { | |
return zoom; | |
} | |
public void setZoom(double zoom) { | |
this.zoom = zoom; | |
} | |
public double getResolution() { | |
return resolution; | |
} | |
public void setResolution(double resolution) { | |
this.resolution = resolution; | |
} | |
public int getWidth() { | |
return width; | |
} | |
public void setWidth(int width) { | |
this.width = width; | |
} | |
public int getHeight() { | |
return height; | |
} | |
public void setHeight(int height) { | |
this.height = height; | |
} | |
public Color getBackground() { | |
return background; | |
} | |
public void setBackground(Color background) { | |
this.background = background; | |
} | |
public Color getForeground() { | |
return foreground; | |
} | |
public void setForeground(Color foreground) { | |
this.foreground = foreground; | |
} | |
public Image getCanvas() { | |
return this.canvas; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment