0% found this document useful (0 votes)
10 views2 pages

Particle Linea

The document contains a C# Windows Forms application that simulates particles moving within a defined area. It initializes a specified number of particles with random sizes and speeds, updates their positions at regular intervals, and renders them on the form. The particles bounce off the edges of the form when they reach the boundaries.

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)
10 views2 pages

Particle Linea

The document contains a C# Windows Forms application that simulates particles moving within a defined area. It initializes a specified number of particles with random sizes and speeds, updates their positions at regular intervals, and renders them on the form. The particles bounce off the edges of the form when they reach the boundaries.

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
You are on page 1/ 2

using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace test_security
{
public partial class Form7 : Form
{
private List<Particle> particles;
private Timer timer;
private Random random;
private readonly int particleCount = 100;

public Form7()
{
InitializeComponent();
InitializeParticles();
InitializeTimer();
}

private void InitializeParticles()


{
particles = new List<Particle>();
random = new Random();

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


{
int size = random.Next(2, 15);
int speedX = random.Next(-5, 6);
int speedY = random.Next(-5, 6);
int x = random.Next(ClientSize.Width);
int y = random.Next(ClientSize.Height);
particles.Add(new Particle(new Point(x, y), size, speedX, speedY));
}
}

private void InitializeTimer()


{
timer = new Timer();
timer.Interval = 30; // Tiempo de actualización en milisegundos
timer.Tick += Timer_Tick;
timer.Start();
}

private void Timer_Tick(object sender, EventArgs e)


{
foreach (Particle particle in particles)
{
particle.Update(ClientRectangle);
}
Invalidate();
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
foreach (Particle particle in particles)
{
e.Graphics.DrawLine(Pens.Green, particle.Position,
particle.PreviousPosition);
}
}
}

public class Particle


{
public Point Position { get; private set; }
public Point PreviousPosition { get; private set; }
public int Size { get; private set; }
public int SpeedX { get; private set; }
public int SpeedY { get; private set; }

public Particle(Point position, int size, int speedX, int speedY)


{
Position = position;
PreviousPosition = position;
Size = size;
SpeedX = speedX;
SpeedY = speedY;
}

public void Update(Rectangle bounds)


{
PreviousPosition = Position;
Position = new Point(Position.X + SpeedX, Position.Y + SpeedY);

if (Position.X < bounds.Left || Position.X > bounds.Right - Size)


{
SpeedX *= -1;
}

if (Position.Y < bounds.Top || Position.Y > bounds.Bottom - Size)


{
SpeedY *= -1;
}
}
}
}

You might also like