Задать движение одной машины только по большому кругу, второй машины только по малому - C#
Формулировка задачи:
В прикладном окне ездят несколько машин, которые движутся по кольцевой дороге.
Помогите изменить функцию,так чтобы машины двигались на встречу друг другу(лево- и правостороннее движение по кругу)?
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Drawing;
namespace Kursovaya
{
delegate void delEv();
class Car //класс машины
{
public event delEv ev;
int num; //номер машины
int speed; //скорость машины
int r; //радиус кольца
string name; //имя машины
int x, y; //текущая позиция машины
bool great, //признак движения по большому кругу
life; //жизнь потока
Thread t; //поток
public Car(int N, string Name, int X, int Y)
{
num = N;
name = Name;
speed = 2;
x = X;
y = Y;
great = true;
life = true;
t = new Thread(new ThreadStart(Move));
t.Start();
}
public int N
{
get
{
return num;
}
}
public int X //свойство координаты X центра машины
{
get
{
return x;
}
}
public int Y //свойство координаты Y центра машины
{
get {
return y;
}
}
int counter; // время существования
void Move() //движение
{
while (life)
{
counter++;
if (great) OnCirclebigroad();
if (!great) OnCirclesmallroad();
if (ev != null)
ev();
Thread.Sleep(100);
}
}
void OnCirclebigroad() //движение по большому кругу
{
r = 160;
x = 170 + (int)Math.Round(r * Math.Cos(counter * 0.1 * speed));
y = 170 + (int)Math.Round(r * Math.Sin(counter * 0.1 * speed));
Console.WriteLine("X=" + x + "Y=" + y + "R=" + r);
if (counter == 32)
{ great = false; counter = 0; }
}
void OnCirclesmallroad() //движение по малому кругу
{
r = 120;
x = 170 + (int)Math.Round(r * Math.Cos(counter * 0.1 * speed));
y = 170 + (int)Math.Round(r * Math.Sin(counter * 0.1 * speed));
Console.WriteLine("X=" + x + "Y=" + y + "R=" + r);
if (counter == 32)
{ great = true; counter = 0; }
}
public void Finish()
{
life = false;
}
}
class Mainwindow : Form //класс главного окна
{
Car l1, l2; //машина
public Mainwindow()
{
this.Text = "Машины в дороге";
l1 = new Car(1, "Машина", 165, 330);
l1.ev += new delEv(HandlerEv);
l2 = new Car(2, "Машина", 312, 96);
l2.ev += new delEv(HandlerEv);
}
void HandlerEv()
{
Invalidate();
}
protected override void OnPaint(PaintEventArgs e) //рисование
{
e.Graphics.DrawEllipse(new Pen(Color.Black, 40), 20, 20, 320, 320);
e.Graphics.DrawEllipse(new Pen(Color.Black, 40), 65, 65, 230, 230);
//рисование машины
e.Graphics.DrawRectangle(new Pen(Color.Red, 4), l1.X, l1.Y, 20, 20);
e.Graphics.DrawRectangle(new Pen(Color.Green, 4), l2.X, l2.Y, 20, 20);
}
protected override void OnClosed(EventArgs e) //закрытие
{
l1.Finish();
}
static void Main() //самая главная функция
{
Mainwindow mw = new Mainwindow();
Application.Run(mw);
}
}
}Решение задачи: «Задать движение одной машины только по большому кругу, второй машины только по малому»
textual
Листинг программы
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}