mirror of
https://github.com/avinal/Golden-Ratio-Image.git
synced 2026-01-10 07:08:33 +05:30
Source File Added
This commit is contained in:
69
src/TheArtist.java
Normal file
69
src/TheArtist.java
Normal file
@@ -0,0 +1,69 @@
|
||||
/**
|
||||
*
|
||||
* D:\Git\Golden-Ratio-Image\src\TheArtist.java
|
||||
*
|
||||
* @author Avinal Kumar
|
||||
* @since April 26, 2020
|
||||
*/
|
||||
|
||||
package src;
|
||||
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import javax.imageio.ImageIO;
|
||||
import src.depends.StdDraw;
|
||||
|
||||
public class TheArtist {
|
||||
private double px, py, r, degree;
|
||||
private double spacing = 15;
|
||||
private int width, height;
|
||||
private double goldenRatio = (Math.sqrt(5.0) + 1) / 2;
|
||||
|
||||
int iter = 0;
|
||||
boolean smallChaos = false;
|
||||
BufferedImage img = null;
|
||||
|
||||
TheArtist(File f) {
|
||||
try {
|
||||
img = ImageIO.read(f);
|
||||
width = img.getWidth();
|
||||
height = img.getHeight();
|
||||
} catch (IOException e) {
|
||||
System.out.println(e);
|
||||
}
|
||||
}
|
||||
|
||||
private void calculatePointPosition(double x, double y, double grade) {
|
||||
px = x + Math.cos(Math.toRadians(grade)) * (r / 2);
|
||||
py = y + Math.sin(Math.toRadians(grade)) * (r / 2);
|
||||
}
|
||||
|
||||
public void goldenDraw() {
|
||||
StdDraw.clear(StdDraw.WHITE);
|
||||
StdDraw.setXscale(-10, width + 10);
|
||||
StdDraw.setYscale(-1 * (height + 10), 10);
|
||||
StdDraw.enableDoubleBuffering();
|
||||
|
||||
for (int i = 5000; i > 0; i--) {
|
||||
degree = (iter * goldenRatio) * 360;
|
||||
r = Math.sqrt(iter++) * spacing;
|
||||
calculatePointPosition(width / 2, height / 2, (degree % 360));
|
||||
if (px - 10 <= 0 || px + 10 >= width || py - 10 <= 0 || py + 10 >= height) {
|
||||
break;
|
||||
}
|
||||
int p = img.getRGB((int) px, (int) py);
|
||||
// int a = (p >> 24) & 0xff;
|
||||
int r = (p >> 16) & 0xff;
|
||||
int g = (p >> 8) & 0xff;
|
||||
int b = p & 0xff;
|
||||
int avg = (r + g + b) / 3;
|
||||
float luminance = (r * 0.2126f + g * 0.7152f + b * 0.0722f) / 255;
|
||||
|
||||
StdDraw.setPenColor(avg, avg, avg);
|
||||
StdDraw.setPenRadius((1 - luminance) / 70);
|
||||
StdDraw.point(px, -1 * py);
|
||||
StdDraw.show();
|
||||
}
|
||||
}
|
||||
}
|
||||
42
src/TheCanvas.java
Normal file
42
src/TheCanvas.java
Normal file
@@ -0,0 +1,42 @@
|
||||
/**
|
||||
*
|
||||
* D:\Git\Golden-Ratio-Image\src\TheCanvas.java
|
||||
*
|
||||
* @author Avinal Kumar
|
||||
* @since April 26, 2020
|
||||
*/
|
||||
|
||||
package src;
|
||||
|
||||
import java.io.*;
|
||||
import javax.swing.JFileChooser;
|
||||
import javax.swing.filechooser.FileNameExtensionFilter;
|
||||
|
||||
/**
|
||||
* TheCanvas
|
||||
*
|
||||
*
|
||||
*/
|
||||
public class TheCanvas {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
|
||||
File f = openFile();
|
||||
TheArtist art = new TheArtist(f);
|
||||
art.goldenDraw();
|
||||
|
||||
}
|
||||
|
||||
public static File openFile() {
|
||||
JFileChooser open = new JFileChooser();
|
||||
FileNameExtensionFilter filter = new FileNameExtensionFilter("Image Files", "jpg", "jpeg");
|
||||
open.setFileFilter(filter);
|
||||
int status = open.showOpenDialog(null);
|
||||
if (status == JFileChooser.APPROVE_OPTION) {
|
||||
return open.getSelectedFile();
|
||||
} else {
|
||||
return new File("tree.txt");
|
||||
}
|
||||
}
|
||||
}
|
||||
2098
src/depends/StdDraw.java
Normal file
2098
src/depends/StdDraw.java
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user