mirror of
https://github.com/avinal/Golden-Ratio-Image.git
synced 2026-01-10 23:28:34 +05:30
Added Comments
This commit is contained in:
@@ -14,16 +14,25 @@ import java.io.IOException;
|
|||||||
import javax.imageio.ImageIO;
|
import javax.imageio.ImageIO;
|
||||||
import src.depends.StdDraw;
|
import src.depends.StdDraw;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The class creates and draws the image according to golden ratio.
|
||||||
|
*/
|
||||||
public class TheArtist {
|
public class TheArtist {
|
||||||
private double px, py, r, degree;
|
private double px, py;// pixel cordinate
|
||||||
private double spacing = 15;
|
private double r, degree;
|
||||||
private int width, height;
|
private double spacing = 15; // spacing between the drawn points
|
||||||
private double goldenRatio = (Math.sqrt(5.0) + 1) / 2;
|
private int width, height; // height and Widht of the image
|
||||||
|
private double goldenRatio = (Math.sqrt(5.0) + 1) / 2; // The Golden Ratio
|
||||||
|
|
||||||
int iter = 0;
|
int iter = 0;
|
||||||
boolean smallChaos = false;
|
|
||||||
BufferedImage img = null;
|
BufferedImage img = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The constructor initializes with a image object to operate on. It also gives
|
||||||
|
* the height and widht of the mage.
|
||||||
|
*
|
||||||
|
* @param f A image file object
|
||||||
|
*/
|
||||||
TheArtist(File f) {
|
TheArtist(File f) {
|
||||||
try {
|
try {
|
||||||
img = ImageIO.read(f);
|
img = ImageIO.read(f);
|
||||||
@@ -34,11 +43,22 @@ public class TheArtist {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calculates the position of the next point to be drawn using cos and sin
|
||||||
|
* functions.
|
||||||
|
*
|
||||||
|
* @param x The x coordinate of the current pixel of the image
|
||||||
|
* @param y The y coordinate of the current pixel of the image
|
||||||
|
* @param grade The angle by which the point should be positioned
|
||||||
|
*/
|
||||||
private void calculatePointPosition(double x, double y, double grade) {
|
private void calculatePointPosition(double x, double y, double grade) {
|
||||||
px = x + Math.cos(Math.toRadians(grade)) * (r / 2);
|
px = x + Math.cos(Math.toRadians(grade)) * (r / 2);
|
||||||
py = y + Math.sin(Math.toRadians(grade)) * (r / 2);
|
py = y + Math.sin(Math.toRadians(grade)) * (r / 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The Actual Drawing Routing. Uses StdDraw.
|
||||||
|
*/
|
||||||
public void goldenDraw() {
|
public void goldenDraw() {
|
||||||
StdDraw.clear(StdDraw.WHITE);
|
StdDraw.clear(StdDraw.WHITE);
|
||||||
StdDraw.setXscale(-10, width + 10);
|
StdDraw.setXscale(-10, width + 10);
|
||||||
@@ -53,16 +73,16 @@ public class TheArtist {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
int p = img.getRGB((int) px, (int) py);
|
int p = img.getRGB((int) px, (int) py);
|
||||||
// int a = (p >> 24) & 0xff;
|
// int a = (p >> 24) & 0xff; // alpha
|
||||||
int r = (p >> 16) & 0xff;
|
int r = (p >> 16) & 0xff; // red
|
||||||
int g = (p >> 8) & 0xff;
|
int g = (p >> 8) & 0xff; // green
|
||||||
int b = p & 0xff;
|
int b = p & 0xff; // blue
|
||||||
int avg = (r + g + b) / 3;
|
int avg = (r + g + b) / 3; // because i like the gray scaled ones 😊
|
||||||
float luminance = (r * 0.2126f + g * 0.7152f + b * 0.0722f) / 255;
|
float luminance = (r * 0.2126f + g * 0.7152f + b * 0.0722f) / 255; // brightness
|
||||||
|
|
||||||
StdDraw.setPenColor(avg, avg, avg);
|
StdDraw.setPenColor(avg, avg, avg); // set all gray 🤗
|
||||||
StdDraw.setPenRadius((1 - luminance) / 70);
|
StdDraw.setPenRadius((1 - luminance) / 70); /** set the point size */
|
||||||
StdDraw.point(px, -1 * py);
|
StdDraw.point(px, -1 * py); // draw the point
|
||||||
StdDraw.show();
|
StdDraw.show();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,24 +13,26 @@ import javax.swing.JFileChooser;
|
|||||||
import javax.swing.filechooser.FileNameExtensionFilter;
|
import javax.swing.filechooser.FileNameExtensionFilter;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TheCanvas
|
* This Class conatains the file chooser and main function.
|
||||||
*
|
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public class TheCanvas {
|
public class TheCanvas {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
|
||||||
File f = openFile();
|
File f = openFile();
|
||||||
TheArtist art = new TheArtist(f);
|
TheArtist art = new TheArtist(f);
|
||||||
art.goldenDraw();
|
art.goldenDraw();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Chooses Image File
|
||||||
|
*
|
||||||
|
* @return File object of the chosen File
|
||||||
|
*/
|
||||||
public static File openFile() {
|
public static File openFile() {
|
||||||
JFileChooser open = new JFileChooser();
|
JFileChooser open = new JFileChooser();
|
||||||
FileNameExtensionFilter filter = new FileNameExtensionFilter("Image Files", "jpg", "jpeg");
|
FileNameExtensionFilter filter = new FileNameExtensionFilter("Image Files", "jpg", "jpeg", "png");
|
||||||
open.setFileFilter(filter);
|
open.setFileFilter(filter);
|
||||||
int status = open.showOpenDialog(null);
|
int status = open.showOpenDialog(null);
|
||||||
if (status == JFileChooser.APPROVE_OPTION) {
|
if (status == JFileChooser.APPROVE_OPTION) {
|
||||||
|
|||||||
Reference in New Issue
Block a user