Skip to content

Instantly share code, notes, and snippets.

@aNNiMON
Created January 6, 2014 15:17
Show Gist options
  • Save aNNiMON/8284266 to your computer and use it in GitHub Desktop.
Save aNNiMON/8284266 to your computer and use it in GitHub Desktop.
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import javax.imageio.ImageIO;
/**
* Creates an image with circle of lines.
* @author aNNiMON
*/
public class CircleTrip {
public static void main(String[] args) {
new CircleTrip(1000, 1000).process(30);
}
private static final String FORMAT_NAME = "png";
private final ArrayList<Point> list;
private final BufferedImage image;
public CircleTrip(int width, int height) {
list = new ArrayList<Point>();
image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
}
private void process(int angles) {
double anglePerPoint = 360d / angles;
double i = 0;
int cx = getWidth() / 2;
int cy = getHeight() / 2;
final int radius = Math.min(cx, cy);
while (i < 360) {
double radAngle = Math.toRadians(i);
list.add(new Point((int) (cx + Math.cos(radAngle) * radius),
(int) (cy + Math.sin(radAngle) * radius)));
i += anglePerPoint;
}
paint(image.getGraphics());
saveImage(image, "image_" + angles);
}
private void paint(Graphics g) {
g.setColor(Color.WHITE);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(Color.BLACK);
final int size = list.size();
for (int i = 0; i < size; i++) {
final int x1 = list.get(i).x;
final int y1 = list.get(i).y;
for (int j = 0; j < size; j++) {
int x2 = list.get(j).x;
int y2 = list.get(j).y;
g.drawLine(x1, y1, x2, y2);
}
}
}
private void saveImage(BufferedImage image, String name) {
try {
File out = new File(name + '.' + FORMAT_NAME);
out.createNewFile();
ImageIO.write(image, FORMAT_NAME, out);
} catch (IOException ex) { }
}
private int getWidth() {
return image.getWidth();
}
private int getHeight() {
return image.getHeight();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment