Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package pl.project.fern;
- import java.awt.image.BufferedImage;
- import java.io.ByteArrayOutputStream;
- import java.io.IOException;
- import javax.imageio.ImageIO;
- //import org.apache.commons.codec.binary.Base64;
- import java.util.Base64;
- public class BarnsleyFern {
- private int dim = 1080;
- private int bgColor = 0xFF000000;
- private int fernColor = 0xFF00FF00;
- private int iterations = 200000;
- String type = "jpg";
- BufferedImage img;
- public String getFern(int bgColor, int fernColor, int iterations) {
- this.bgColor = bgColor;
- this.fernColor = fernColor;
- this.iterations = iterations;
- createFern();
- return this.encodeToString();
- }
- void createFern() {
- double x = 0;
- double y = 0;
- img = new BufferedImage(dim, dim, BufferedImage.TYPE_INT_ARGB);
- for (int i = 0; i < dim; i++) {
- for (int j = 0; j < dim; j++) {
- img.setRGB(i, j, fernColor);
- }
- }
- for (int i = 0; i < iterations; i++) {
- double tmpx, tmpy;
- double r = Math.random();
- if (r <= 0.01) {
- tmpx = 0;
- tmpy = 0.16 * y;
- } else if (r <= 0.08) {
- tmpx = 0.2 * x - 0.26 * y;
- tmpy = 0.23 * x + 0.22 * y + 1.6;
- } else if (r <= 0.15) {
- tmpx = -0.15 * x + 0.28 * y;
- tmpy = 0.26 * x + 0.24 * y + 0.44;
- } else {
- tmpx = 0.85 * x + 0.04 * y;
- tmpy = -0.04 * x + 0.85 * y + 1.6;
- }
- x = tmpx;
- y = tmpy;
- img.setRGB((int) Math.round(dim / 2 + x * dim / 11),
- (int) Math.round(dim - y * dim / 11), bgColor);
- }
- }
- public String encodeToString() {
- String imageString = null;
- ByteArrayOutputStream bos = new ByteArrayOutputStream();
- try {
- ImageIO.write(this.img, type, bos);
- byte[] imageBytes = bos.toByteArray();
- imageString = Base64.getEncoder().encodeToString(imageBytes);
- bos.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return imageString;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement