messimmous

Untitled

Oct 11th, 2015
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 17.42 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10.  
  11. namespace WindowsFormsApplication6
  12. {
  13.     public partial class Form1x : Form
  14.     {
  15.         CCircleStorage UsuallyStorage = new CCircleStorage();
  16.         CCircleStorage PaintedStorage = new CCircleStorage();
  17.         float DeltaX = 0;
  18.         float DeltaY = 0;
  19.         int IndMov = 0;
  20.         bool isClicked = false;
  21.         int Connect = 0;
  22.         bool Vision;
  23.         public Form1x()
  24.         {
  25.             InitializeComponent();
  26.             pic.Invalidate();
  27.          
  28.         }
  29.  
  30.         private void Form1_MouseClick(object sender, MouseEventArgs e)
  31.         {
  32.             ;
  33.         }
  34.  
  35.         private void Form1x_Paint(object sender, PaintEventArgs e)
  36.         {
  37.             ;
  38.         }
  39.  
  40.         private void pic_Click(object sender, EventArgs e)
  41.         {
  42.             ;
  43.         }
  44.  
  45.         private void pic_MouseClick(object sender, MouseEventArgs e)
  46.         {
  47.             //int j = 0;
  48.             if (e.Button == System.Windows.Forms.MouseButtons.Right)
  49.             {
  50.                 int j = 0;
  51.                 for (; j < UsuallyStorage.Counter; j++)
  52.                 {
  53.                     if (UsuallyStorage.GetCCircle(j) != null)
  54.                         if ((e.X - UsuallyStorage.GetCCircle(j).X) * (e.X - UsuallyStorage.GetCCircle(j).X) + (e.Y - UsuallyStorage.GetCCircle(j).Y) * (e.Y - UsuallyStorage.GetCCircle(j).Y) < UsuallyStorage.GetCCircle(j).Radius * UsuallyStorage.GetCCircle(j).Radius)
  55.                         {
  56.                             foreach(int Neigh in UsuallyStorage.GetCCircle(j).Neighbours)
  57.                             {
  58.                                 UsuallyStorage.GetCCircle(Neigh).Neighbours.Remove(j);
  59.                             }
  60.                             if (UsuallyStorage.GetCCircle(j).Selected)
  61.                                 Connect--;
  62.                        
  63.                                button1.Visible = false;
  64.                                 Vision = false;
  65.                                 label1.Visible = false;
  66.                                 label2.Visible = false;
  67.                                 label1.Text = "";
  68.                            
  69.                            
  70.                             UsuallyStorage.DeleteCCircle(j);
  71.                             pic.Invalidate();
  72.                             break;
  73.                         }
  74.                 }
  75.                
  76.             }
  77.             else
  78.             {
  79.                 timer1.Enabled = true;
  80.                 int j = 0;
  81.                 // bool flag = true;
  82.                 for (; j < UsuallyStorage.Counter; j++)
  83.                 {
  84.                     if (UsuallyStorage.GetCCircle(j) != null)
  85.                     {
  86.                         if ((e.X - UsuallyStorage.GetCCircle(j).X) * (e.X - UsuallyStorage.GetCCircle(j).X) + (e.Y - UsuallyStorage.GetCCircle(j).Y) * (e.Y - UsuallyStorage.GetCCircle(j).Y) < UsuallyStorage.GetCCircle(j).Radius * UsuallyStorage.GetCCircle(j).Radius)
  87.                         {
  88.                             UsuallyStorage.GetCCircle(j).DoSelect();
  89.                             if (UsuallyStorage.GetCCircle(j).Selected)
  90.                                 Connect++;
  91.                             else
  92.                                 Connect--;
  93.                             if (Connect == 1)
  94.                             {
  95.                                 button1.Visible = true;
  96.                                 label1.Visible = true;
  97.                                 label2.Visible = true;
  98.                                 //Vision = true;
  99.                             }
  100.                             else
  101.                             {
  102.                                 button1.Visible = false;
  103.                                 Vision = false;
  104.                                 label1.Visible = false;
  105.                                 label2.Visible = false;
  106.                                 label1.Text = "";
  107.                             }
  108.                             if (Connect == 2)
  109.                             {
  110.                                 Connect = 0;
  111.                                 int k = 0;
  112.                                 for (; k < UsuallyStorage.Counter; k++)
  113.                                 {
  114.                                     if (UsuallyStorage.GetCCircle(k) != null)
  115.                                         if (UsuallyStorage.GetCCircle(k).Selected)
  116.                                         {
  117.                                             UsuallyStorage.GetCCircle(k).DoSelect();
  118.                                             break;
  119.                                         }
  120.                                 }
  121.                                 int l = UsuallyStorage.Counter;
  122.                                 for (; l > 0; l--)
  123.                                 {
  124.                                     if (UsuallyStorage.GetCCircle(l) != null)
  125.                                         if (UsuallyStorage.GetCCircle(l).Selected)
  126.                                         {
  127.                                             UsuallyStorage.GetCCircle(k).DoSelect();
  128.                                             break;
  129.                                         }
  130.                                 }
  131.                                 if (!UsuallyStorage.GetCCircle(k).Neighbours.Contains(l))
  132.                                 {
  133.                                     UsuallyStorage.GetCCircle(k).AddNeighbour(l);
  134.                                     UsuallyStorage.GetCCircle(k).Neighbours.Sort();
  135.                                 }
  136.                                 UsuallyStorage.GetCCircle(k).DoSelect();
  137.                                 if (!UsuallyStorage.GetCCircle(l).Neighbours.Contains(k))
  138.                                 {
  139.                                     UsuallyStorage.GetCCircle(l).AddNeighbour(k);
  140.                                     UsuallyStorage.GetCCircle(l).Neighbours.Sort();
  141.                                 }
  142.                                 UsuallyStorage.GetCCircle(l).DoSelect();
  143.                             }
  144.                             //flag = false;
  145.                             break;
  146.                         }
  147.                     }
  148.                 }/*
  149.             if (flag)
  150.             {
  151.                 CCircle tmp = new CCircle(e.X, e.Y);
  152.                 int index = UsuallyStorage.AddToStorage(tmp);
  153.             }*/
  154.             }
  155.  
  156.  
  157.         }
  158.  
  159.         private void pic_Paint(object sender, PaintEventArgs e)
  160.         {
  161.             e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
  162.            
  163.                 for (int i = 0; i < UsuallyStorage.Counter; i++)
  164.                 {
  165.                     CCircle a = UsuallyStorage.GetCCircle(i);
  166.                     if (a != null)
  167.                     {
  168.                         Pen myPen;
  169.                         myPen = new System.Drawing.Pen(System.Drawing.Color.Black);
  170.                         foreach (int neighbour in a.Neighbours)
  171.                         {
  172.                             CCircle tmp=UsuallyStorage.GetCCircle(neighbour);
  173.                             if (tmp!=null)
  174.                             e.Graphics.DrawLine(myPen, new Point((int)a.X, (int)a.Y), new Point((int)tmp.X, (int)tmp.Y));
  175.                         }
  176.                        // a.Paint(e.Graphics);
  177.                     }
  178.                        
  179.                 }
  180.                 for (int i = 0; i < UsuallyStorage.Counter; i++)
  181.                     if (UsuallyStorage.GetCCircle(i) != null)
  182.                     {
  183.                         UsuallyStorage.GetCCircle(i).Paint(e.Graphics, i, Vision);    
  184.                     }
  185.        
  186.         }
  187.        
  188.        
  189.  
  190.         private void pic_MouseDown(object sender, MouseEventArgs e)
  191.         {
  192.            
  193.             int j = 0;
  194.             for (; j < UsuallyStorage.Counter; j++)
  195.             {
  196.                 if (UsuallyStorage.GetCCircle(j) != null)
  197.                 {
  198.                     if ((e.X - UsuallyStorage.GetCCircle(j).X) * (e.X - UsuallyStorage.GetCCircle(j).X) + (e.Y - UsuallyStorage.GetCCircle(j).Y) * (e.Y - UsuallyStorage.GetCCircle(j).Y) < UsuallyStorage.GetCCircle(j).Radius * UsuallyStorage.GetCCircle(j).Radius)
  199.                     {
  200.                         UsuallyStorage.GetCCircle(j).DoMove();
  201.                        
  202.                         isClicked = true;
  203.                         DeltaX=e.X-UsuallyStorage.GetCCircle(j).X;
  204.                         DeltaY = e.Y - UsuallyStorage.GetCCircle(j).Y;
  205.                         IndMov = j;
  206.                         break;
  207.                     }
  208.                 }
  209.             }
  210.         }
  211.  
  212.         private void pic_MouseUp(object sender, MouseEventArgs e)
  213.         {
  214.             if (UsuallyStorage.GetCCircle(IndMov) != null)
  215.             {
  216.                 UsuallyStorage.GetCCircle(IndMov).DoMove();
  217.                 isClicked = false;
  218.                
  219.                // UsuallyStorage.GetCCircle(IndMov).DoSelect();
  220.             }
  221.         }
  222.  
  223.         private void pic_MouseMove(object sender, MouseEventArgs e)
  224.         {
  225.             if (isClicked && UsuallyStorage.GetCCircle(IndMov)!=null)
  226.             {
  227.                 UsuallyStorage.GetCCircle(IndMov).ChangeCenter(e.X - DeltaX, e.Y - DeltaY);
  228.                 pic.Refresh();
  229.             }
  230.         }
  231.  
  232.         private void timer1_Tick(object sender, EventArgs e)
  233.         {
  234.             pic.Invalidate();
  235.         }
  236.         private bool MasCheck (int[] array, int k)
  237.         {
  238.             for (int i = 0; i < array.Length; i++)
  239.                 if (array[i] == k)
  240.                     return true;
  241.             return false;
  242.         }
  243.         private void pic_MouseDoubleClick(object sender, MouseEventArgs e)
  244.         {
  245.             CCircle tmp = new CCircle(e.X, e.Y);
  246.             int index = UsuallyStorage.AddToStorage(tmp);
  247.         }
  248.  
  249.         private void Deep(int i, SortedSet<int> resArray)// ______________________________________________________________________________________________________________________________________________________________
  250.         {
  251.             Stack<int> myStack = new Stack<int>();
  252.             myStack.Push(i);
  253.             int walkN = 0;
  254.             while (myStack.Count != 0)
  255.             {
  256.                 int tmp = myStack.Pop();
  257.                 if (!resArray.Contains(tmp))
  258.                 {
  259.                     resArray.Add(tmp);
  260.                     UsuallyStorage.GetCCircle(tmp).ChangeNumero(walkN + 1);
  261.                     label1.Text += (tmp + 1).ToString() + "  ";
  262.                     foreach (int nei in UsuallyStorage.GetCCircle(tmp).Neighbours)
  263.                     {
  264.                         if (!resArray.Contains(nei))
  265.                         {
  266.                             myStack.Push(nei);
  267.                         }
  268.                     }
  269.                     walkN++;
  270.                 }
  271.             }
  272.         }
  273.  
  274.         private void button1_Click(object sender, EventArgs e)
  275.         {
  276.             Vision = true;
  277.             for (int q=0; q < UsuallyStorage.Counter; q++)
  278.                 if (UsuallyStorage.GetCCircle(q) != null)
  279.                     UsuallyStorage.GetCCircle(q).ChangeNumero(-1);
  280.             int i = 0;
  281.             for (; i < UsuallyStorage.Counter; i++)
  282.                 if (UsuallyStorage.GetCCircle(i) != null)
  283.                     if (UsuallyStorage.GetCCircle(i).Selected)
  284.                         break;
  285.            /* int[] resArray = new int[UsuallyStorage.Counter];
  286.             for (int c = 0; c < UsuallyStorage.Counter; c++)
  287.             {
  288.                 resArray[c] = -1;
  289.                
  290.             }*/
  291.             SortedSet<int> resArray=new SortedSet<int>();
  292.             Deep(i, resArray);
  293.         }
  294.     }
  295.     class CCircle
  296.     {
  297.         protected float x;
  298.         protected float y;
  299.         protected float radius;
  300.         protected bool selected;
  301.         protected bool moving;
  302.         protected List<int> neighbours;
  303.         protected String numero;
  304.         public CCircle() { this.x = 0; this.y = 0; this.radius = 30; this.selected = false; moving = false; neighbours = new List<int>(); numero = ""; }
  305.         public CCircle(float x, float y, float radius = 30)
  306.         {
  307.             this.x = x;
  308.             this.y = y;
  309.             this.radius = radius;
  310.             this.selected = false;
  311.             moving = false;
  312.             neighbours = new List<int>();
  313.             numero = "";
  314.         }
  315.         public CCircle(CCircle a)
  316.         {
  317.             this.x = a.x;
  318.             this.y = a.y;
  319.             this.radius = a.radius;
  320.             neighbours = new List<int>();
  321.             numero = "";
  322.         }
  323.         public void DoSelect()
  324.         {
  325.             this.selected = !this.selected;
  326.         }
  327.         public void DoMove()
  328.         {
  329.             this.moving = !this.moving;
  330.         }
  331.         virtual public void Paint(Graphics formGraphics, int numb, bool flag=false)
  332.         {
  333.             string drawString = (numb+1).ToString();
  334.             System.Drawing.Font drawFont = new System.Drawing.Font("Arial", 16);
  335.             System.Drawing.Font numeroFont = new System.Drawing.Font("Arial", 12);
  336.             System.Drawing.SolidBrush textBrush = new System.Drawing.SolidBrush(System.Drawing.Color.White);
  337.             System.Drawing.SolidBrush numeroBrush = new System.Drawing.SolidBrush(System.Drawing.Color.White);
  338.             System.Drawing.SolidBrush indexBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Violet);
  339.             if (!Selected)
  340.             {
  341.                 System.Drawing.Brush myBrush;
  342.                 myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Black);
  343.                 formGraphics.FillEllipse(myBrush, x - radius, y - radius, radius * 2, radius * 2);
  344.                 formGraphics.DrawString(drawString, drawFont, textBrush, x-10, y-10);
  345.                 if (flag)
  346.                 {
  347.                     formGraphics.FillEllipse(indexBrush,x - 40, y + 10, 40, 40);
  348.                     formGraphics.DrawString(numero, numeroFont, numeroBrush, x - 28, y+22);
  349.                 }
  350.             }
  351.             else
  352.             {
  353.                 System.Drawing.Brush myBrush;
  354.                 myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Violet);
  355.                 formGraphics.FillEllipse(myBrush, x - radius, y - radius, radius * 2, radius * 2);
  356.                 formGraphics.DrawString(drawString, drawFont, textBrush, x-10, y-10);
  357.                 if (flag)
  358.                 {
  359.                     formGraphics.FillEllipse(indexBrush, x - 40, y + 10, 40, 40);
  360.                     formGraphics.DrawString(numero, numeroFont, numeroBrush, x - 28, y+22);
  361.                 }
  362.             }
  363.  
  364.         }
  365.         public float X { get { return this.x; } }
  366.         public float Y { get { return this.y; } }
  367.         public float Radius { get { return this.radius; } }
  368.  
  369.         public bool Selected { get { return this.selected; } }
  370.         public bool Moving { get { return this.moving; } }
  371.  
  372.         public List<int> Neighbours { get { return this.neighbours; } }
  373.         public void ChangeCenter (float NX, float NY)
  374.         {
  375.             this.x = NX;
  376.             this.y = NY;
  377.         }
  378.         public void ChangeNumero(int a)
  379.         {
  380.             String tmp = a.ToString();
  381.             this.numero = tmp;
  382.         }
  383.         public void AddNeighbour (int x)
  384.         {
  385.             neighbours.Add(x);
  386.         }
  387.     }
  388.  
  389.    
  390.     class CCircleStorage
  391.     {
  392.         private CCircle[] a;
  393.         private int counter { get; set; }
  394.  
  395.         public CCircleStorage()
  396.         {
  397.             a = new CCircle[10];
  398.             counter = 10;
  399.             for (int i = 0; i < 10; i++)
  400.                 a[i] = null;
  401.         }
  402.         public CCircleStorage(int b)
  403.         {
  404.             this.a = new CCircle[b];
  405.             counter = b;
  406.             for (int i = 0; i < 10; i++)
  407.                 a[i] = null;
  408.         }
  409.         public int Counter { get { return this.counter; } }
  410.         public void AddToPosition(CCircle v1, int pos)
  411.         {
  412.             if (pos > counter - 1)
  413.                 return;
  414.             a[pos] = v1;
  415.         }
  416.  
  417.         public bool FreeCheck()
  418.         {
  419.             for (int i = 0; i < counter; i++)
  420.             {
  421.                 if (a[i] == null)
  422.                     return true;
  423.             }
  424.             return false;
  425.         }
  426.         public void AddPlaces(int k)
  427.         {
  428.             CCircle[] new_a = new CCircle[counter + k];
  429.             int i = 0;
  430.             for (; i < counter; i++)
  431.                 new_a[i] = a[i];
  432.             for (; i < k; i++)
  433.                 new_a[i] = null;
  434.             a = new_a;
  435.             counter += k;
  436.             return;
  437.         }
  438.         public CCircle GetCCircle(int k)
  439.         {
  440.             if (!(k > counter - 1))
  441.                 return a[k];
  442.             else
  443.                 return null;
  444.         }
  445.         public void DeleteCCircle(int k)
  446.         {
  447.             if (!(k > counter - 1))
  448.                 a[k] = null;
  449.             else
  450.                 return;
  451.         }
  452.         public int AddToStorage(CCircle v1)
  453.         {
  454.             for (int i = 0; i < counter; i++)
  455.             {
  456.                 if (a[i] == null)
  457.                 {
  458.                     a[i] = v1;
  459.                     return i;
  460.                 }
  461.             }
  462.             AddPlaces(1);
  463.             AddToPosition(v1, counter - 1);
  464.             return counter - 1;
  465.         }
  466.  
  467.     }
  468.  
  469.    
  470.  
  471. }
Advertisement
Add Comment
Please, Sign In to add comment