Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
72
Task/Brownian-tree/Java/brownian-tree-1.java
Normal file
72
Task/Brownian-tree/Java/brownian-tree-1.java
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
import java.awt.Graphics;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.util.*;
|
||||
import javax.swing.JFrame;
|
||||
|
||||
public class BrownianTree extends JFrame implements Runnable {
|
||||
|
||||
BufferedImage I;
|
||||
private List<Particle> particles;
|
||||
static Random rand = new Random();
|
||||
|
||||
public BrownianTree() {
|
||||
super("Brownian Tree");
|
||||
setBounds(100, 100, 400, 300);
|
||||
setDefaultCloseOperation(EXIT_ON_CLOSE);
|
||||
I = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
|
||||
I.setRGB(I.getWidth() / 2, I.getHeight() / 2, 0xff00);
|
||||
particles = new LinkedList<Particle>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void paint(Graphics g) {
|
||||
g.drawImage(I, 0, 0, this);
|
||||
}
|
||||
|
||||
public void run() {
|
||||
for (int i = 0; i < 20000; i++) {
|
||||
particles.add(new Particle());
|
||||
}
|
||||
while (!particles.isEmpty()) {
|
||||
for (Iterator<Particle> it = particles.iterator(); it.hasNext();) {
|
||||
if (it.next().move()) {
|
||||
it.remove();
|
||||
}
|
||||
}
|
||||
repaint();
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
BrownianTree b = new BrownianTree();
|
||||
b.setVisible(true);
|
||||
new Thread(b).start();
|
||||
}
|
||||
|
||||
private class Particle {
|
||||
|
||||
private int x, y;
|
||||
|
||||
private Particle() {
|
||||
x = rand.nextInt(I.getWidth());
|
||||
y = rand.nextInt(I.getHeight());
|
||||
}
|
||||
|
||||
/* returns true if either out of bounds or collided with tree */
|
||||
private boolean move() {
|
||||
int dx = rand.nextInt(3) - 1;
|
||||
int dy = rand.nextInt(3) - 1;
|
||||
if ((x + dx < 0) || (y + dy < 0)
|
||||
|| (y + dy >= I.getHeight()) || (x + dx >= I.getWidth())) {
|
||||
return true;
|
||||
}
|
||||
x += dx;
|
||||
y += dy;
|
||||
if ((I.getRGB(x, y) & 0xff00) == 0xff00) {
|
||||
I.setRGB(x - dx, y - dy, 0xff00);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
90
Task/Brownian-tree/Java/brownian-tree-2.java
Normal file
90
Task/Brownian-tree/Java/brownian-tree-2.java
Normal file
|
|
@ -0,0 +1,90 @@
|
|||
import java.awt.Point;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
|
||||
public class BasicBrownianTree {
|
||||
|
||||
private int pixelsLost;
|
||||
private Point p;
|
||||
private Point nextP;
|
||||
private int pixelCount;
|
||||
private int width;
|
||||
private int height;
|
||||
private int color;
|
||||
private BufferedImage img;
|
||||
|
||||
public BasicBrownianTree( int argb, int size, double density ) {
|
||||
pixelsLost = 0;
|
||||
p = new Point();
|
||||
nextP = new Point();
|
||||
width = size;
|
||||
height = size;
|
||||
color = argb;
|
||||
pixelCount = (int) ( width * height * density );
|
||||
img = new BufferedImage( width, height, BufferedImage.TYPE_INT_ARGB );
|
||||
}
|
||||
|
||||
public void generate() {
|
||||
// print text to the console
|
||||
System.out.println( "Drawing " + pixelCount + " pixels" );
|
||||
int background = img.getRGB( 0, 0 );
|
||||
img.setRGB( width / 2, height / 2, color );
|
||||
|
||||
for( int i = 0; i < pixelCount; i++ ) {
|
||||
p.x = (int) ( Math.random() * width );
|
||||
p.y = (int) ( Math.random() * height );
|
||||
|
||||
while ( true ) {
|
||||
int dx = (int) ( Math.random() * 3 ) - 1;
|
||||
int dy = (int) ( Math.random() * 3 ) - 1;
|
||||
nextP.setLocation( p.x + dx, p.y + dy );
|
||||
// handle out-of-bounds
|
||||
if ( nextP.x < 0 || nextP.x >= width || nextP.y < 0
|
||||
|| nextP.y >= height ) {
|
||||
// increment the number of pixels lost and escape the loop
|
||||
pixelsLost++;
|
||||
break;
|
||||
}
|
||||
if ( img.getRGB( nextP.x, nextP.y ) != background ) {
|
||||
img.setRGB( p.x, p.y, color );
|
||||
break;
|
||||
}
|
||||
p.setLocation( nextP );
|
||||
}
|
||||
// Print a message every 2%
|
||||
if ( i % ( pixelCount / 50 ) == 0 ) {
|
||||
System.out.println( "Done with " + i + " pixels" );
|
||||
}
|
||||
}
|
||||
// We're done. Let the user know how many pixels were lost
|
||||
System.out.println( "Finished. Pixels lost = " + pixelsLost );
|
||||
}
|
||||
|
||||
public BufferedImage getImage() {
|
||||
return img;
|
||||
}
|
||||
|
||||
public int getWidth() {
|
||||
return width;
|
||||
}
|
||||
|
||||
public int getHeight() {
|
||||
return height;
|
||||
}
|
||||
|
||||
public static void main( String[] args ) {
|
||||
// create the new generator
|
||||
BasicBrownianTree generator = new BasicBrownianTree( 0x664444ff, 400, 0.4 );
|
||||
// generate the image
|
||||
generator.generate();
|
||||
try {
|
||||
// save the image to the file "image.png"
|
||||
ImageIO.write( generator.getImage(), "png", new File( "image.png" ) );
|
||||
} catch ( IOException e ) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue