com.github.bloodshura.x.assets.util.MandelbrotSet Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of shurax-assets Show documentation
Show all versions of shurax-assets Show documentation
An API for reading, writing and manipulating fonts, images and sounds.
/*
* Copyright (c) 2013-2018, João Vitor Verona Biazibetti - All Rights Reserved
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see .
*
* https://www.github.com/BloodShura
*/
package com.github.bloodshura.x.assets.util;
import com.github.bloodshura.x.assets.XAssets;
import com.github.bloodshura.x.assets.image.Image;
import javax.annotation.Nonnull;
import java.awt.image.BufferedImage;
public class MandelbrotSet {
private int iterations;
private double zoom;
public MandelbrotSet() {
this.iterations = 100;
this.zoom = 1.0D;
}
public void generate(@Nonnull BufferedImage image) {
double zX;
double zY;
double cX;
double cY;
double tmp;
for (int y = 0; y < image.getHeight(); y++) {
for (int x = 0; x < image.getWidth(); x++) {
int iter = getIterations();
zX = 0.0D;
zY = 0.0D;
cX = (x - image.getWidth() / 2) / getZoom();
cY = (y - image.getHeight() / 2) / getZoom();
while (zX * zX + zY * zY < 4 && iter > 0) {
tmp = zX * zX - zY * zY + cX;
zY = 2.0D * zX * zY + cY;
zX = tmp;
iter--;
}
image.setRGB(x, y, iter | (iter << 8));
}
}
}
public void generate(@Nonnull Image image) {
generate(image.getImage());
}
@Nonnull
public Image generate(int width, int height) {
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
generate(image);
return XAssets.getLoader().getImage(image);
}
public int getIterations() {
return iterations;
}
public double getZoom() {
return zoom;
}
public void setIterations(int iterations) {
this.iterations = iterations;
}
public void setZoom(double zoom) {
this.zoom = zoom;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy