0% found this document useful (0 votes)
12 views3 pages

Particul

The document outlines a C# Windows Forms application that simulates particles moving on the screen. It initializes particle properties such as position, speed, and size, and updates their positions in a timer event to create an animation effect. The particles are drawn as glowing triangles, with a smooth rendering approach using high-quality graphics settings.

Uploaded by

gabrielwryx
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views3 pages

Particul

The document outlines a C# Windows Forms application that simulates particles moving on the screen. It initializes particle properties such as position, speed, and size, and updates their positions in a timer event to create an animation effect. The particles are drawn as glowing triangles, with a smooth rendering approach using high-quality graphics settings.

Uploaded by

gabrielwryx
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

1{public partial class Login : Form}

private const int ParticleCount = 80;


private const int DrawCount = 80; // Number of particles to draw
private readonly Random _random = new Random();
private readonly PointF[] _particlePositions = new PointF[ParticleCount];
private readonly PointF[] _particleTargetPositions = new PointF[ParticleCount];
private readonly float[] _particleSpeeds = new float[ParticleCount];
private readonly float[] _particleSizes = new float[ParticleCount];
private readonly float[] _particleRadii = new float[ParticleCount];
private readonly float[] _particleRotations = new float[ParticleCount];
private readonly PointF[] _vertices = new PointF[3]; // Reuse vertices array

2{public Login()}

DoubleBuffered = true;

InitializeParticles();

Timer timer = new Timer


{
Interval = 3 // Roughly 60 FPS
};
timer.Tick += (sender, args) =>
{
UpdateParticles();
Invalidate(); // Causes the form to be redrawn
};
timer.Start();

{3}
private void InitializeParticles()
{
Size screenSize = Screen.PrimaryScreen.Bounds.Size;
for (int i = 0; i < ParticleCount; i++)
{
_particlePositions[i] = new PointF(0, 0);
_particleTargetPositions[i] = new PointF(_random.Next(screenSize.Width),
screenSize.Height * 2);
_particleSpeeds[i] = 1 + _random.Next(25);
_particleSizes[i] = _random.Next(8);
_particleRadii[i] = _random.Next(4);
_particleRotations[i] = 0;
}
}

private void UpdateParticles()


{
Size screenSize = Screen.PrimaryScreen.Bounds.Size;
for (int i = 0; i < ParticleCount; i++)
{
if (_particlePositions[i].X == 0 || _particlePositions[i].Y == 0)
{
_particlePositions[i] = new PointF(_random.Next(screenSize.Width + 1),
15f);
_particleSpeeds[i] = 1 + _random.Next(25);
_particleRadii[i] = _random.Next(4);
_particleSizes[i] = _random.Next(8);
_particleTargetPositions[i] = new
PointF(_random.Next(screenSize.Width), screenSize.Height * 2);
}

float deltaTime = 2.5f / 60; // Assuming 60 FPS


_particlePositions[i] = Lerp(_particlePositions[i],
_particleTargetPositions[i], deltaTime * (_particleSpeeds[i] / 60));
_particleRotations[i] += deltaTime;

if (_particlePositions[i].Y > screenSize.Height)


{
_particlePositions[i] = new PointF(0, 0);
_particleRotations[i] = 0;
}
}
}

private PointF Lerp(PointF start, PointF end, float t)


{
return new PointF(start.X + (end.X - start.X) * t, start.Y + (end.Y - start.Y)
* t);
}

protected override void OnPaint(PaintEventArgs e)


{
base.OnPaint(e);

e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

for (int i = 0; i < DrawCount; i++)


{
DrawTriangleWithGlow(e.Graphics, _particlePositions[i], _particleSizes[i],
_particleRotations[i]);
}
}

private void DrawTriangleWithGlow(Graphics graphics, PointF position, float size,


float rotation)
{
float angle = (float)(Math.PI * 2 / 3); // 120 degrees for equilateral
triangle
PointF[] vertices = new PointF[3];

for (int i = 0; i < 3; i++)


{
vertices[i] = new PointF(
position.X + size * (float)Math.Cos(rotation + i * angle),
position.Y + size * (float)Math.Sin(rotation + i * angle)
);
}

graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

// Draw glow effect


int maxGlowLayers = 10;
for (int j = 0; j < maxGlowLayers; j++)
{
int alpha = 25 - 2 * j; // Gradually decrease alpha for each layer
using (Brush glowBrush = new SolidBrush(Color.FromArgb(alpha, 213, 0,
255))) // Semi-transparent red
{
float glowSize = size + j * 4; // Gradually increase the glow size
graphics.FillEllipse(glowBrush, position.X - glowSize / 2, position.Y
- glowSize / 2, glowSize, glowSize);
}
}

// Draw triangle
using (Brush brush = new SolidBrush(Color.FromArgb(213, 0, 255))) // Solid red
color for the triangle
{
graphics.FillPolygon(brush, vertices);
}
}

{4}
public class Particle
{
public PointF Position { get; set; }
public PointF Velocity { get; set; }
public int Radius { get; set; }
public Color Color { get; set; }

{5}
private void timer1_Tick(object sender, EventArgs e)
{
UpdateParticles();
Invalidate();
}

You might also like