Particle fountain: Difference between revisions

New post.
(Added Easylang)
(New post.)
 
(5 intermediate revisions by 3 users not shown)
Line 257:
 
=={{header|EasyLang}}==
[https://easylang.dev/show/#cod=ZVHLDsIgELzzFZN48RErVKtpjF9iOGCphthCQrSxf+8uttVEDrDMzO4ObDQWJ8hM5YXwFO2llGIG0dQer7OGxxEc9xwntPuFuw9+MdX9FsPTW+wOpahCEyLKshTBw3jXmkctAFwJddRE4RGwzRmi5TxbmPOxglqgDVZRzQ/5OhOuiS8ksdF4G9rrwPUj57DBbgC7KWM+qLGm9xULLOmYVFNu/i2bFIoVGW9VU5v4Z3s01oauZneabegBrFykLKpnv/Y1VqfkStOf9eP9JynFWPMUJE0htc/EGw== Run it]
[https://easylang.dev/show/#cod=ZVHLDoIwELz3Kybx4iNgIRAlhi8xPVSKphHapFECf+9uBTWxh3Y7M7s77QZtUEOmeSkcBVkhpVhBdK3DeFZwOIHjieOIDr/w8MYvurnfgn86g+JQicZ3PqCqKuEdtLO9frQCwJVQy03w8DgyQss6NrDmY4dsg96bjEq+yfFMuCK+lMQG7YzvrzM3LZzFHvkMDp+M9axGQq8rN9jSUSyqT27+LRsVGStS3pqu1eHP9WKs90PL7hTbUDPY2EBZVM987Svs6uhK0ZdNy/0nKcZIeAaShhDbp+IF Run it]
 
<syntaxhighlight>
rad = 0.25125
n = 14006000
#
len x[] n ; len y[] n
Line 268:
color 999
on animate
for i = 1 to 832
ind = (ind + 1) mod1 n
x[ind] = 50 + randomf
y[ind] = i / 24
vx[ind] = (randomf - 0.5) * 0.4
vy[ind] = 2 + randomf * 0.1
Line 285:
</syntaxhighlight>
 
=={{header|Java}}==
<syntaxhighlight lang="java">
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.image.BufferStrategy;
import java.util.Arrays;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadLocalRandom;
 
import javax.swing.JFrame;
 
public final class ParticleFountainTask {
 
public static void main(String[] args) {
EventQueue.invokeLater( () -> {
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("Particle Fountain");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
ParticleFountain particleFountain = new ParticleFountain(3_000, 1_000, 750);
frame.add(particleFountain);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
particleFountain.start();
} );
}
 
private static final class ParticleFountain extends Canvas {
 
public ParticleFountain(int aParticleCount, int aWidth, int aHeight) {
particleCount = aParticleCount;
width = aWidth;
height = aHeight;
saturation = 0.6;
spread = 1.5;
range = 1.5;
reciprocate = false;
setPreferredSize( new Dimension(width, height) );
addKeyListener( new InputHandler() );
executorService = Executors.newSingleThreadExecutor();
}
 
public void start() {
requestFocus();
createBufferStrategy(2);
executorService.execute( new DrawingCycle() );
}
 
private final class DrawingCycle implements Runnable {
public DrawingCycle() {
positions = new double[2 * particleCount];
velocities = new double[2 * particleCount];
lifetimes = new double[particleCount];
points = new Point[particleCount];
Arrays.fill(points, new Point(0, 0) );
random = ThreadLocalRandom.current();
}
 
@Override
public void run() {
bufferStrategy = getBufferStrategy();
while ( true ) {
update(0.005);
draw();
}
}
 
private void update(double animationSpeed) {
int xIndex = 0;
int yIndex = 1;
pointIndex = 0;
 
for ( int index = 0; index < particleCount; index++ ) {
boolean showParticle = false;
if ( lifetimes[index] <= 0.0 ) {
if ( random.nextDouble() < animationSpeed ) {
lifetimes[index] = 2.5;
positions[xIndex] = width / 20;
positions[yIndex] = height / 10;
velocities[xIndex] =
10 * ( spread * random.nextDouble() - spread / 2 + additionalXSpeed() );
velocities[yIndex] = ( random.nextDouble() - 2.9 ) * height / 20.5;
showParticle = true;
}
} else {
if ( positions[yIndex] > height / 10 && velocities[yIndex] > 0 ) {
velocities[yIndex] *= -0.3; // bounce particle
}
velocities[yIndex] += animationSpeed * height / 10;
positions[xIndex] += velocities[xIndex] * animationSpeed;
positions[yIndex] += velocities[yIndex] * animationSpeed;
lifetimes[index] -= animationSpeed;
showParticle = true;
}
 
if ( showParticle ) {
points[pointIndex] = new Point((int) ( positions[xIndex] * 10 ),
(int) ( positions[yIndex] * 10 ));
pointIndex += 1;
}
xIndex += 2;
yIndex = xIndex + 1;
}
}
 
private void draw() {
Graphics2D graphics2D = (Graphics2D) bufferStrategy.getDrawGraphics();
graphics2D.setColor(Color.BLACK);
graphics2D.fillRect(0, 0, getWidth(), getHeight());
for ( int i = 0; i < pointIndex; i++ ) {
graphics2D.setColor(Color.getHSBColor(random.nextFloat(), (float) saturation, 1.0F));
graphics2D.fillOval(points[i].x, points[i].y, 5, 5);
}
graphics2D.dispose();
bufferStrategy.show();
}
private double additionalXSpeed() {
return ( reciprocate ) ? range * Math.sin(System.currentTimeMillis() / 1_000) : 0.0;
}
private double[] positions;
private double[] velocities;
private double[] lifetimes;
private int pointIndex;
private Point[] points;
private BufferStrategy bufferStrategy;
private ThreadLocalRandom random;
 
} // End DrawingCycle class
private final class InputHandler extends KeyAdapter {
@Override
public void keyPressed(KeyEvent aKeyEvent) {
final int keyCode = aKeyEvent.getKeyCode();
switch ( keyCode ) {
case KeyEvent.VK_UP -> saturation = Math.min(saturation + 0.1, 1.0);
case KeyEvent.VK_DOWN -> saturation = Math.max(saturation - 0.1, 0.0);
case KeyEvent.VK_PAGE_UP -> spread = Math.min(spread + 0.1, 5.0);
case KeyEvent.VK_PAGE_DOWN -> spread = Math.max(spread - 0.1, 0.5);
case KeyEvent.VK_RIGHT -> range = Math.min(range + 0.1, 2.0);
case KeyEvent.VK_LEFT -> range = Math.max(range + 0.1, 0.1);
case KeyEvent.VK_SPACE -> reciprocate = ! reciprocate;
case KeyEvent.VK_Q -> Runtime.getRuntime().exit(0);
default -> { /* Take no action */ }
}
}
} // End InputHandler class
private int particleCount;
private int width;
private int height;
private double saturation;
private double spread;
private double range;
private boolean reciprocate;
private ExecutorService executorService;
} // End ParticleFountain class
 
} // End ParticleFountainTask class
</syntaxhighlight>
 
=={{header|Julia}}==
Line 1,171 ⟶ 1,352:
{{libheader|DOME}}
{{libheader|Wren-dynamic}}
<syntaxhighlight lang="ecmascriptwren">import "dome" for Window, Platform, Process
import "graphics" for Canvas, Color
import "math" for Math, Point
Line 1,310 ⟶ 1,491:
 
var Game = ParticleDisplay.new(3000, 800, 800)</syntaxhighlight>
 
=={{header|XPL0}}==
{{trans|EasyLang}}
[[File:XPL0_Fountain.gif|right]]
<syntaxhighlight lang "XPL0">func real RandomF;
return float(Ran(1000)) / 1000.;
 
def N = 6000;
real X(N), Y(N);
real VX(N), VY(N);
def Color = 15;
int I, Ind;
[SetVid($12);
Ind:= 0;
repeat
for I:= 1 to 32 do
[Ind:= rem((Ind+1)/N);
X(Ind):= 50. + RandomF;
Y(Ind):= float(I)/4.;
VX(Ind):= (RandomF - 0.5) * 0.4;
VY(Ind):= 2. + RandomF*0.1;
];
WaitForVSync;
Clear;
for I:= 0 to N-1 do
[Point(fix(X(I)), 480-fix(Y(I)), Color);
X(I):= X(I) + VX(I); Y(I):= Y(I) + VY(I);
VY(I):= VY(I) - 0.025;
];
until KeyHit;
]</syntaxhighlight>
871

edits