Skip to content

Commit 35c927b

Browse files
committed
Add code, README, and examples
1 parent ef6f987 commit 35c927b

File tree

5 files changed

+132
-0
lines changed

5 files changed

+132
-0
lines changed

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,30 @@
11
# RgbRandomWalk
22
Creates fractal-like images with three 2D random walks in red, green, and blue.
3+
4+
The intensity of a pixel is proportional (until it reaches the brightest value) to the number of times it is visited. See [Wikipedia](https://en.wikipedia.org/wiki/Random_walk) for more info on random walks.
5+
6+
Made just for fun.
7+
8+
9+
## Usage
10+
`java RgbRandomWalk <imagefilepath> <width> <height> <steps>`
11+
12+
`imagefilepath` is the image file to write. Output in in PNG format regardless of specified extension. This can be changed in source file. **Note: will overwrite any existing file. Use caution.**
13+
14+
`width` and `height` are the size of image.
15+
16+
`steps` is the number of steps to take for each random walk.
17+
18+
## Examples
19+
20+
![Example 1](example1.png?raw=true)
21+
22+
Width: 600, height: 400, steps: 500000
23+
24+
![Example 2](example2.png?raw=true)
25+
26+
Width: 600, height: 400, steps: 1000000
27+
28+
![Example 3](example3.png?raw=true)
29+
30+
Width: 1920, height: 1080, steps: 6000000

RgbRandomWalk.java

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import java.awt.*;
2+
import java.io.*;
3+
import javax.imageio.*;
4+
import java.awt.image.*;
5+
import java.util.Random;
6+
7+
public class RgbRandomWalk {
8+
private static Random prng = new Random();
9+
10+
private static int step(int pos, int upperBound) {
11+
if (pos <= 0) {
12+
return prng.nextInt(2);
13+
} else if (pos >= upperBound - 1) {
14+
return upperBound - 1 - prng.nextInt(2);
15+
} else {
16+
return pos - 1 + prng.nextInt(3);
17+
}
18+
}
19+
20+
public static void main(String[] args) {
21+
long start = System.currentTimeMillis();
22+
String fileName = "";
23+
int width = 1920, height = 1080;
24+
int increment = 8;
25+
int steps = 6000000;
26+
String imgFormat = "png";
27+
28+
if (args.length != 4) {
29+
System.out.println("Usage: java RgbRandomWalk <imagefilepath> <width> <height> <steps>");
30+
System.exit(0);
31+
} else {
32+
try {
33+
fileName = args[0];
34+
width = Integer.parseInt(args[1], 10);
35+
height = Integer.parseInt(args[2], 10);
36+
steps = Integer.parseInt(args[3], 10);
37+
38+
} catch (NumberFormatException e) {
39+
System.out.println("Usage: java RgbRandomWalk <imagefilepath> <width> <height> <steps>");
40+
System.out.println("Error: Invalid number.");
41+
System.exit(0);
42+
}
43+
44+
if (width <= 0 || height <= 0 || steps <= 0) {
45+
System.out.println("Usage: java RgbRandomWalk <imagefilepath> <width> <height> <steps>");
46+
System.out.println("Error: Negative number.");
47+
System.exit(0);
48+
}
49+
}
50+
51+
BufferedImage img;
52+
Color c;
53+
// Good parameters are 1920 x 1080, intensity increment 8, and steps = 6000000.
54+
55+
int rX = width / 2, rY = height / 2;
56+
int gX = width / 2, gY = height / 2;
57+
int bX = width / 2, bY = height / 2;
58+
int r = 0, g = 0, b = 0;
59+
// int sRGB, other;
60+
61+
62+
img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
63+
64+
rX = width / 2;
65+
rY = height / 2;
66+
gX = width / 2;
67+
gY = height / 2;
68+
bX = width / 2;
69+
bY = height / 2;
70+
71+
for (int j = 0; j < steps; j++) {
72+
73+
74+
rX = step(rX, width);
75+
rY = step(rY, height);
76+
gX = step(gX, width);
77+
gY = step(gY, height);
78+
bX = step(bX, width);
79+
bY = step(bY, height);
80+
81+
c = new Color(img.getRGB(rX, rY));
82+
img.setRGB(rX, rY, (c.getRGB() & 0xff00ffff) | (Math.min(c.getRed() + increment, 255) << 16));
83+
84+
c = new Color(img.getRGB(gX, gY));
85+
img.setRGB(gX, gY, (c.getRGB() & 0xffff00ff) | (Math.min(c.getGreen() + increment, 255) << 8));
86+
87+
c = new Color(img.getRGB(bX, bY));
88+
img.setRGB(bX, bY, (c.getRGB() & 0xffffff00) | (Math.min(c.getBlue() + increment, 255)));
89+
}
90+
91+
try {
92+
File outputfile = new File(fileName);
93+
ImageIO.write(img, imgFormat, outputfile);
94+
} catch (Exception e) {
95+
System.out.println("Error: cannot open the file.");
96+
System.exit(0);
97+
}
98+
99+
100+
long time = System.currentTimeMillis()-start;
101+
System.out.printf("Time used: %dm, %ds, %dms.%n", time / 60000, (time / 1000) % 60, time % 1000);
102+
}
103+
104+
}

example1.png

294 KB
Loading

example2.png

456 KB
Loading

example3.png

1.89 MB
Loading

0 commit comments

Comments
 (0)