Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
2011_10_18_2.docx
Скачиваний:
12
Добавлен:
12.07.2019
Размер:
32.5 Кб
Скачать

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

namespace WindowsFormsApplication1 {

public partial class Form1 : Form {

public Form1() {

InitializeComponent();

balls = new Ball[30];

Random r = new Random();

for (int i = 0; i < balls.Length; i++) {

var oldColors = balls.Take(i).Select(p => p.color).ToArray();

balls[i] = new Ball();

balls[i].X = r.Next(10, Width - 40);

balls[i].Y = r.Next(10, Height - 50);

while (true) {

Color temp = Color.FromArgb(r.Next(0, 255), r.Next(0, 255), r.Next(0, 255));

balls[i].color = temp;

if (oldColors.Any(p => p.B == temp.B && p.G == temp.G && p.R == temp.R)) {

continue;

}

break;

}

}

}

//A colorA

//B colorB

//colorB -> colorA

public void megre(Color ca, Color temp) {

for (int i = 0; i < balls.Length; i++) {

Ball p = balls[i];

if (p.color.B == temp.B && p.color.G == temp.G

&& p.color.R == temp.R) {

p.color = ca;

}

}

}

public bool needMegre() {

Ball p = balls[0];

Color temp = p.color;

for (int i = 1; i < balls.Length; i++) {

if (balls[i].color != temp) {

return true;

}

}

return false;

}

public void getMinDistIndex(out int index1, out int index2) {

double minDist = -1;

index1 = -1;

index2 = -1;

for (int i = 0; i < balls.Length; i++) {

for (int j = 0; j < balls.Length; j++) {

if (i == j) {

continue;

}

if (balls[i].color == balls[j].color) {

continue;

}

double curDist = getDist(i, j);

if (minDist == -1) {

minDist = curDist;

index1 = i;

index2 = j;

} else if (curDist < minDist) {

minDist = curDist;

index1 = i;

index2 = j;

}

}

}

}

public double getDist(int i, int j) {

return (balls[i].X - balls[j].X) * (balls[i].X - balls[j].X)

+ (balls[i].Y - balls[j].Y) * (balls[i].Y - balls[j].Y);

}

public Ball[] balls;

private void Form1_Paint(object sender, PaintEventArgs e) {

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

int x1 = balls[links[i].index1].X;

int y1 = balls[links[i].index1].Y;

int x2 = balls[links[i].index2].X;

int y2 = balls[links[i].index2].Y;

e.Graphics.DrawLine(Pens.Black, x1, y1, x2, y2);

}

for (int i = 0; i < balls.Length; i++) {

balls[i].draw(e.Graphics);

}

}

private void Form1_MouseDown(object sender, MouseEventArgs e) {

if (e.Button == MouseButtons.Right) {

if (needMegre() == false) {

MessageBox.Show("дерево найдено");

return;

}

int index1;

int index2;

getMinDistIndex(out index1, out index2);

megre(balls[index1].color, balls[index2].color);

GraphLink newLine = new GraphLink()

{index1 = index1, index2 = index2};

links.Add(newLine);

Invalidate();

}

}

List<GraphLink> links = new List<GraphLink>();

}

class GraphLink {

public int index1;

public int index2;

}

public class Ball {

public int X;

public int Y;

public Color color;

public void draw(Graphics g) {

g.FillEllipse(new SolidBrush(color), X - 5, Y - 5, 10, 10);

}

}

}

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

namespace WindowsFormsApplication1 {

public partial class Form1 : Form {

public Form1() {

InitializeComponent();

balls = new Ball[60];

Random r = new Random();

for (int i = 0; i < balls.Length; i++) {

var oldColors = balls.Take(i).Select(p => p.color).ToArray();

balls[i] = new Ball();

balls[i].X = r.Next(10, Width - 40);

balls[i].Y = r.Next(10, Height - 50);

while (true) {

Color temp = Color.FromArgb(r.Next(0, 255), r.Next(0, 255), r.Next(0, 255));

balls[i].color = temp;

if (oldColors.Any(p => p.B == temp.B && p.G == temp.G && p.R == temp.R)) {

continue;

}

break;

}

}

}

//A colorA

//B colorB

//colorB -> colorA

public void megre(Color ca, Color temp) {

for (int i = 0; i < balls.Length; i++) {

Ball p = balls[i];

if (p.color.B == temp.B && p.color.G == temp.G

&& p.color.R == temp.R) {

p.color = ca;

}

}

}

public bool needMegre() {

Ball p = balls[0];

Color temp = p.color;

for (int i = 1; i < balls.Length; i++) {

if (balls[i].color != temp) {

return true;

}

}

return false;

}

public void getMinDistIndex(out int index1, out int index2) {

double minDist = -1;

index1 = -1;

index2 = -1;

for (int i = 0; i < balls.Length; i++) {

for (int j = 0; j < balls.Length; j++) {

if (i == j) {

continue;

}

if (balls[i].color == balls[j].color) {

continue;

}

double curDist = getDist(i, j);

if (minDist == -1) {

minDist = curDist;

index1 = i;

index2 = j;

} else if (curDist < minDist) {

minDist = curDist;

index1 = i;

index2 = j;

}

}

}

}

public double getDist(int i, int j) {

return (balls[i].X - balls[j].X) * (balls[i].X - balls[j].X)

+ (balls[i].Y - balls[j].Y) * (balls[i].Y - balls[j].Y);

}

public Ball[] balls;

private void Form1_Paint(object sender, PaintEventArgs e) {

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

int x1 = balls[links[i].index1].X;

int y1 = balls[links[i].index1].Y;

int x2 = balls[links[i].index2].X;

int y2 = balls[links[i].index2].Y;

e.Graphics.DrawLine(Pens.Black, x1, y1, x2, y2);

}

for (int i = 0; i < balls.Length; i++) {

balls[i].draw(e.Graphics);

}

}

private void Form1_MouseDown(object sender, MouseEventArgs e) {

if (e.Button == MouseButtons.Right) {

if (needMegre() == false) {

MessageBox.Show("дерево найдено");

return;

}

int index1;

int index2;

getMinDistIndex(out index1, out index2);

megre(balls[index1].color, balls[index2].color);

GraphLink newLine = new GraphLink()

{index1 = index1, index2 = index2};

links.Add(newLine);

Invalidate();

}

}

List<GraphLink> links = new List<GraphLink>();

private void timer1_Tick(object sender, EventArgs e) {

if (needMegre() == false) {

timer1.Enabled = false;

return;

}

int index1;

int index2;

getMinDistIndex(out index1, out index2);

megre(balls[index1].color, balls[index2].color);

GraphLink newLine = new GraphLink() { index1 = index1, index2 = index2 };

links.Add(newLine);

Invalidate();

}

}

class GraphLink {

public int index1;

public int index2;

}

public class Ball {

public int X;

public int Y;

public Color color;

public void draw(Graphics g) {

g.FillEllipse(new SolidBrush(color), X - 5, Y - 5, 10, 10);

}

}

}

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

namespace WindowsFormsApplication1 {

public partial class Form1 : Form {

public Form1() {