Plasma effect: Difference between revisions
Content added Content deleted
(→{{header|J}}: ensure viewmat available) |
m (clarify) |
||
Line 1: | Line 1: | ||
{{draft task}} |
{{draft task}} |
||
The [[wp:Plasma_effect|plasma effect]] is a visual effect created by applying various functions, notably sine and cosine, to the color values of screen pixels. When animated the effect may give the impression of a colorful flowing liquid. |
The [[wp:Plasma_effect|plasma effect]] is a visual effect created by applying various functions, notably sine and cosine, to the color values of screen pixels. When animated (not a task requirement) the effect may give the impression of a colorful flowing liquid. |
||
'''The task''': create a plasma effect. |
'''The task''': create a plasma effect. |
Revision as of 11:43, 9 March 2016
Plasma effect is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
The plasma effect is a visual effect created by applying various functions, notably sine and cosine, to the color values of screen pixels. When animated (not a task requirement) the effect may give the impression of a colorful flowing liquid.
The task: create a plasma effect.
- See also
J
<lang j>require 'trig viewmat' plasma=: 3 :0
'w h'=. y X=. (i. % <:) w Y=. (i. % <:) h x1=. sin X*16 y1=. sin Y*32 xy1=. sin (Y+/X)*16 xy2=. sin (Y +&.*:/ X)*32 xy1+xy2+y1+/x1
)</lang>
<lang j> viewmat plasma 256 256</lang>
Java
<lang java>import java.awt.*; import java.awt.event.*; import java.awt.image.*; import static java.awt.image.BufferedImage.*; import static java.lang.Math.*; import javax.swing.*;
public class PlasmaEffect extends JPanel {
float[][] plasma; float hueShift = 0; BufferedImage img;
public PlasmaEffect() { Dimension dim = new Dimension(640, 640); setPreferredSize(dim); setBackground(Color.white);
img = new BufferedImage(dim.width, dim.height, TYPE_INT_RGB); plasma = createPlasma(dim.height, dim.width);
// animate about 24 fps and shift hue value with every frame new Timer(42, (ActionEvent e) -> { hueShift = (hueShift + 0.02f) % 1; repaint(); }).start(); }
float[][] createPlasma(int w, int h) { float[][] buffer = new float[h][w];
for (int y = 0; y < h; y++) for (int x = 0; x < w; x++) {
double value = sin(x / 16.0); value += sin(y / 8.0); value += sin((x + y) / 16.0); value += sin(sqrt(x * x + y * y) / 8.0); value += 4; // shift range from -4 .. 4 to 0 .. 8 value /= 8; // bring range down to 0 .. 1
// requires VM option -ea assert (value >= 0.0 && value <= 1.0) : "Hue value out of bounds";
buffer[y][x] = (float) value; } return buffer; }
void drawPlasma(Graphics2D g) { int h = plasma.length; int w = plasma[0].length; for (int y = 0; y < h; y++) for (int x = 0; x < w; x++) { float hue = hueShift + plasma[y][x] % 1; img.setRGB(x, y, Color.HSBtoRGB(hue, 1, 1)); } g.drawImage(img, 0, 0, null); }
@Override public void paintComponent(Graphics gg) { super.paintComponent(gg); Graphics2D g = (Graphics2D) gg; g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
drawPlasma(g); }
public static void main(String[] args) { SwingUtilities.invokeLater(() -> { JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setTitle("Plasma Effect"); f.setResizable(false); f.add(new PlasmaEffect(), BorderLayout.CENTER); f.pack(); f.setLocationRelativeTo(null); f.setVisible(true); }); }
}</lang>