C#, WPF – 1523
Not terribly serious; this was mainly an attempt whether it actually worked.
What I'm doing here is using an ItemsControl (Menu, since it's shorter) in WPF to display particles via data binding and templating. A data template controls how the particles look like (in this case a simple circle) and data binding controls color and position of the particles.
Each particle has a position, a color and a direction which get updated by a timer. Initially a bunch of particles are generated in the center of the window with (normal-distributed) random directions. That collection gets data-bound to the ItemsControl which then handles displaying the particles automatically, whenever the properties are updated. Particles are dragged down a bit due to gravity.
It got a little lengthy, admittedly. But at least it looks nice:
It can surely be made shorter by omitting:
- gravity, causing particles to expand uniformly to all sides;
- the normal distribution for the initial vectors, causing the particles to expand in a sparse »box«;
- the brightness variation, leaving particles black.
I opted not to do so in the interest of aesthetics. This solution is way longer than almost anything else regardless. The data-binding and templating approach might be elegant but it's also quite verbose compared with simply updating a bitmap. (Note: I got it down to 1356 by leaving all of the above out, but it looks horrible then.)
The code is distributed in three files, which are given here in formatted form for legibility:
App.xaml
<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" StartupUri="W.xaml"/>
W.xaml
<Window x:Class="W" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Menu Name="i">
<Menu.ItemTemplate>
<DataTemplate>
<Ellipse Width="2" Height="2" Fill="#000" Opacity="{Binding O}">
<Ellipse.RenderTransform>
<TranslateTransform X="{Binding X}" Y="{Binding Y}"/>
</Ellipse.RenderTransform>
</Ellipse>
</DataTemplate>
</Menu.ItemTemplate>
<Menu.Template>
<ControlTemplate>
<ItemsPresenter/>
</ControlTemplate>
</Menu.Template>
<Menu.ItemsPanel>
<ItemsPanelTemplate>
<Canvas/>
</ItemsPanelTemplate>
</Menu.ItemsPanel>
</Menu>
</Window>
W.xaml.cs
using M = System.Math;
using T = System.Timers.Timer;
using System;
using System.ComponentModel;
partial class W
{
int a;
T t = new T(99);
public W()
{
InitializeComponent();
Height = Width = 500;
var r = new Random();
Func<double> n = () => 2 * (M.Sqrt(-2 * M.Log(r.NextDouble())) * M.Sin(6 * r.NextDouble()));
var l = new System.Collections.Generic.List<P>();
for (; a++ < 300; )
l.Add(new P { X = 250, Y = 250, f = n(), g = n() });
i.ItemsSource = l;
t.Elapsed += delegate
{
foreach (P x in l)
{
x.X += x.f;
x.Y += x.g += .2;
x.O = M.Max(1 - M.Sqrt(M.Pow(250 - x.X, 2) + M.Pow(250 - x.Y, 2)) / 250, 0);
}
};
t.Start();
}
}
class P : System.Windows.ContentElement, INotifyPropertyChanged
{
public double y, f, g;
public double X { get; set; }
public double O { get; set; }
public double Y
{
get { return y; }
set
{
y = value;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(""));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}