0% found this document useful (0 votes)
21 views5 pages

Dynamic Particle Animation in C#

The document defines a C# class named 'Particles' that simulates moving circles and lines on a Windows Forms application. It includes methods for setting up the simulation, moving the circles, and drawing them on the screen, along with their connections based on proximity. The class also contains helper methods for shuffling speed values and calculating distances between points.

Uploaded by

fer.tiktk
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)
21 views5 pages

Dynamic Particle Animation in C#

The document defines a C# class named 'Particles' that simulates moving circles and lines on a Windows Forms application. It includes methods for setting up the simulation, moving the circles, and drawing them on the screen, along with their connections based on proximity. The class also contains helper methods for shuffling speed values and calculating distances between points.

Uploaded by

fer.tiktk
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

using System;

using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];

namespace CX
{
public class Particles
{
#region variables
public static Control _Instance;
public static (int, int)[] AvailableSpeeds;
public static int _CircleNum;
public static int _ConnectLines;
public static Random rand = new Random();
public static List<Circles> _Particles = new List<Circles>();
public static List<Lines> _Lines = new List<Lines>();
private static Bitmap _bufferBitmap;
public static Color _LineColor;
#endregion
public static void Setup(Control Instance, int CircleNum, Color
CircleColor, int ConnectLines, Color LineColor) //start
{
_Instance = Instance;
_CircleNum = CircleNum;
_ConnectLines = ConnectLines;
_LineColor = LineColor;
_bufferBitmap = new Bitmap(_Instance.Width, _Instance.Height);
List<(int, int)> speedsList = new List<(int, int)>();

for (int j = -1; j <= 1; j++)


{
for (int k = -1; k <= 1; k++)
{
if (j != 0 || k != 0)
{
[Link]((j, k));
}
}
}

Shuffle(speedsList);

while ([Link] < CircleNum * 2)


{
[Link](speedsList);
Shuffle(speedsList);
}

AvailableSpeeds = [Link](CircleNum * 2).ToArray();

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


{
var speedIndex = i % [Link];
var (speedX, speedY) = AvailableSpeeds[speedIndex];
_Particles.Add(new Circles()
{
Size = [Link](2, 2),
Pos = new Point([Link]([Link]),
[Link]([Link])),
Speed = new Point(speedX, speedY),
RGB = new int[] { [Link](256), [Link](256),
[Link](256) },
Opacity = 255,
});
}

for (int i = 0; i < _Particles.Count; i++)


{
for (int j = i + 1; j < _Particles.Count; j++)
{
int x1 = _Particles[i].Pos.X;
int y1 = _Particles[i].Pos.Y;
int x2 = _Particles[j].Pos.X;
int y2 = _Particles[j].Pos.Y;

Lines newLine = new Lines


{
Size = 2,
Pos = new Point(x1, y1),
Pos2 = new Point(x2, y2),
Color = LineColor,
Opacity = 50
};
_Lines.Add(newLine);
}
}

public static void Start() // Nuevo método para suscribir el evento Paint
{
_Instance.Paint += DrawCirclesAndLines;
}

public static void Shuffle<T>(List<T> list)


{
int n = [Link];
while (n > 1)
{
n--;
int k = [Link](n + 1);
T value = list[k];
list[k] = list[n];
list[n] = value;
}
}
public static void MoveCircles(List<Circles> circles)
{
Random random = new Random();

for (int i = 0; i < [Link]; i++)


{
Circles circle = circles[i];
int newX = [Link].X + [Link].X;
int newY = [Link].Y + [Link].Y;

if (newX - [Link] < 0 || newX + [Link] >


_Instance.[Link])
{
[Link] = new Point(-[Link].X, [Link].Y);
}

if (newY - [Link] < 0 || newY + [Link] >


_Instance.[Link])
{
[Link] = new Point([Link].X, -[Link].Y);
}

[Link] = new Point(newX, newY);


circles[i] = circle;
}

public static void DrawCirclesAndLines(object sender, PaintEventArgs e)


{
if (_Instance.WindowState != [Link])
{

using (Graphics bufferGraphics = [Link](_bufferBitmap))


{
[Link] =
[Link];
[Link] =
[Link];
[Link] =
[Link];
[Link] =
[Link];
[Link] =
[Link];
[Link] =
[Link];
[Link](_Instance.BackColor);
DrawCircles(bufferGraphics);
DrawLines(bufferGraphics);
}

[Link](_bufferBitmap, [Link]);

}
}
public static void DrawCircles(Graphics g)
{
foreach (var circle in _Particles)
{
[Link](new SolidBrush([Link]([Link],
[Link][0], [Link][1], [Link][2])),
[Link].X - [Link], [Link].Y -
[Link],
[Link] * 2, [Link] * 2);
}
}
public static void DrawLines(Graphics g)
{
_Lines.Clear();
for (int i = 0; i < _Particles.Count; i++)
{
for (int j = i + 1; j < _Particles.Count; j++)
{
int x1 = _Particles[i].Pos.X;
int y1 = _Particles[i].Pos.Y;

int x2 = _Particles[j].Pos.X;
int y2 = _Particles[j].Pos.Y;

Lines newLine = new Lines


{
Size = 2,
Pos = new Point(x1, y1),
Pos2 = new Point(x2, y2),
Color = _LineColor,
Opacity = 25
};
_Lines.Add(newLine);
}
}
foreach (var line in _Lines)
{
double distance = CalculateDistance([Link], line.Pos2);

if (distance <= 45.0f)


{
using (Pen pen = new Pen([Link]([Link],
[Link]), [Link]))
{
[Link](pen, [Link], line.Pos2);
}
}
}
}
public static double CalculateDistance(Point p1, Point p2)
{
return [Link]([Link](p2.X - p1.X, 2) + [Link](p2.Y - p1.Y, 2));
}

}
public class Circles
{
public int Size { get; set; }
public Point Pos { get; set; }
public Point Speed { get; set; }
public int[] RGB { get; set; }
public int Opacity { get; set; }
}
public class Lines
{
public int Size { get; set; }
public Point Pos { get; set; }
public Point Pos2 { get; set; }
public Color Color { get; set; }
public int Opacity { get; set; }
}
}

You might also like