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;
using System.Drawing.Drawing2D;
namespace rotation
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Random r = new Random();
speed = new double[ballnum];
for (int i = 0; i < ballnum; i++)
{
speed[i] = r.Next(2);
}
ballcenter = new double[ballnum, 2];
for (int j = 0; j < ballnum; j++)
{
ballcenter[j, 0] = r.Next(25,ClientSize.Width - 25);
ballcenter[j, 1] = r.Next(25,ClientSize.Height - 25);
}
balldir = new double[ballnum, 2];
for (int k = 0; k < ballnum; k++)
{
balldir[k, 0] = r.Next(-25,25);
balldir[k, 1] = r.Next(-25,25);
}
timer1.Start();
timer2.Start();
}
private double[] speed;
public string[] colors = new string[] { "Red", "Blue", "Black", "Yellow", "Crimson", "Gold", "Green", "Magenta", "Aquamarine", "Brown", "Red", "DarkBlue" };
private int ballnum=10;
private double[,] ballcenter, balldir;
private void Form1_Paint(object sender, PaintEventArgs e)
{
for (int i = 0; i < ballnum; i++)
{
e.Graphics.DrawEllipse(new Pen(Color.FromName(colors[i]), 2), Convert.ToInt32(ballcenter[i, 0]-25),Convert.ToInt32(ballcenter[i, 1]-25), 50, 50);
}
}
private void timer1_Tick(object sender, EventArgs e)
{
for (int i = 0,j; i < ballnum; i++)
{
for (j = i + 1; j < ballnum; j++)
{
if (Math.Pow(Math.Pow(ballcenter[i, 0] - ballcenter[j, 0], 2) + Math.Pow(ballcenter[i, 1] - ballcenter[j, 1], 2), 0.5) < 50)
{
double speedtemp = speed[i];
speed[i] = speed[j];
speed[j] = speedtemp;
double angle = Math.Atan2(ballcenter[j, 0] - ballcenter[i, 0], ballcenter[j, 1] - ballcenter[i, 1]);
double balldirveci = Math.Pow(Math.Pow(balldir[i, 0], 2) + Math.Pow(balldir[i, 1], 2), 0.5);
double balldirvecj = Math.Pow(Math.Pow(balldir[j, 0], 2) + Math.Pow(balldir[j, 1], 2), 0.5);
balldir[j, 0] = balldirveci * Math.Sin(angle);
balldir[j, 1] = balldirveci * Math.Cos(angle);
balldir[i, 0] = -balldirvecj * Math.Sin(angle);
balldir[i, 1] = -balldirvecj * Math.Cos(angle);
}
}
if (ballcenter[i, 0] > ClientSize.Width - 25)
{
balldir[i, 0] = -balldir[i, 0];
}
if (ballcenter[i, 1] > ClientSize.Height - 25)
{
balldir[i, 1] = -balldir[i, 1];
}
if (ballcenter[i, 0] < 25)
{
balldir[i, 0] = -balldir[i, 0];
}
if (ballcenter[i, 1] < 25)
{
balldir[i, 1] = -balldir[i, 1];
}
ballcenter[i, 0] += balldir[i, 0] * speed[i];
ballcenter[i, 1] += balldir[i, 1] * speed[i];
}
this.Refresh();
}
private int picnum = 0;
private void timer2_Tick(object sender, EventArgs e)
{
Rectangle bounds = this.Bounds;
Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height);
Graphics g = Graphics.FromImage(bitmap);
g.CopyFromScreen(new Point(bounds.Left, bounds.Top), Point.Empty, bounds.Size);
bitmap.Save("test" + picnum + ".jpg");
picnum++;
}
}
}
http://msdn.microsoft.com/en-us/library/system.random(v=vs.110).aspx
No comments:
Post a Comment