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

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(5, panel1.Width - 5);

balls[i].Y = r.Next(5, panel1.Height - 5);

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;

public float getY(float X) {

return (float)Math.Sin(X);

}

private void panel1_Paint(object sender, PaintEventArgs e) {

List<PointF> points = new List<PointF>();

for (float x = -(float)Math.PI; x < Math.PI; x += 0.2F) {

PointF newP = new PointF(x, getY(x));

points.Add(newP);

}

GraphPainter p = new GraphPainter();

p.draw(points.ToArray(), e.Graphics, panel1.Width, panel1.Height);

/*

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 panel1_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);

panel1.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 GraphPainter {

//0, 3, 8

//0, 3/8, 8/8

//[a..b]

//[0..b-a]+a

//[0..1]*(b-a)+a

//(a-min)/(max-min)

//[0..maxDrawX]

//[0..1]*MaxDrawX

public void draw(PointF[] points, Graphics g, int maxDrawX, int maxDrawY) {

float minX = points.Select(p => p.X).Min();

float maxX = points.Select(p => p.X).Max();

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

if (maxX == minX) {

points[i].X = 0.5F;

} else {

points[i].X = (points[i].X - minX) / (maxX - minX);

}

points[i].X = points[i].X * maxDrawX;

}

float minY = points.Select(p => p.Y).Min();

float maxY = points.Select(p => p.Y).Max();

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

if (maxY == minY) {

points[i].Y = 0.5F;

} else {

points[i].Y = (points[i].Y - minY) / (maxY - minY);

points[i].Y = 1 - points[i].Y;

}

points[i].Y = points[i].Y * maxDrawY;

}

for (int i = 0; i < points.Length - 1; i++) {

g.DrawLine(Pens.Black, points[i], points[i + 1]);

}

}

}

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() {