Advertisement
Skylighty

lab07

Apr 11th, 2019
471
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.02 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 lab07_live
  12. {
  13.     public partial class Form1 : Form
  14.     {
  15.         public List<MyNode> obiekty = new List<MyNode>();
  16.         public List<MyLine> linie = new List<MyLine>();
  17.         public Pen Pioro = new Pen(Color.Black, 50);
  18.         public Pen PioroLinie = new Pen(Color.Black, 2);
  19.         public int X;
  20.         public int Y;
  21.         public int ObjectCount = 1;
  22.         public Form1()
  23.         {
  24.             InitializeComponent();
  25.         }
  26.  
  27.         private void panel1_MouseDown(object sender, MouseEventArgs e)
  28.         {
  29.             X = e.X;
  30.             Y = e.Y;
  31.             MyNode Wezel = new MyNode(X, Y);
  32.             obiekty.Add(Wezel);
  33.             panel1.Refresh();
  34.             Wezel.Text = $"Obiekt {ObjectCount.ToString()}";
  35.             treeView1.Nodes.Add(Wezel);
  36.             ObjectCount++;
  37.             for (int i = 0; i < obiekty.Count-1; i++)
  38.             {
  39.                 double roznica1 = Math.Pow((Math.Abs(Wezel.NodeX - obiekty[i].NodeX)), 2);
  40.                 double roznica2 = Math.Pow((Math.Abs(Wezel.NodeY - obiekty[i].NodeY)), 2);
  41.                 if (Math.Sqrt(roznica1+roznica2) < 150)
  42.                 {
  43.                     obiekty[i].NCount++;
  44.                     obiekty[i].Nodes.Add(new MyNode(Wezel.NodeX,Wezel.NodeY,$"Sąsiad {obiekty[i].NCount.ToString()}"));
  45.                     MyLine Nowa = new MyLine(Wezel.NodeX,Wezel.NodeY,obiekty[i].NodeX,obiekty[i].NodeY);
  46.                     linie.Add(Nowa);
  47.                     panel1.Refresh();
  48.                 }
  49.             }    
  50.         }
  51.  
  52.         private void panel1_Paint(object sender, PaintEventArgs e)
  53.         {
  54.             Graphics g = e.Graphics;
  55.             for (int i = 0; i < obiekty.Count; i++)
  56.             {
  57.                 g.DrawEllipse(Pioro,obiekty[i].NodeX,obiekty[i].NodeY,5, 5);
  58.             }
  59.  
  60.             for (int i = 0; i < linie.Count; i++)
  61.             {
  62.                 g.DrawLine(PioroLinie, linie[i].sx, linie[i].sy, linie[i].ex, linie[i].ey);
  63.             }
  64.  
  65.         }
  66.  
  67.         private void panel1_MouseMove(object sender, MouseEventArgs e)
  68.         {
  69.             toolStripStatusLabel1.Text = $"{e.X}, {e.Y}";
  70.         }
  71.     }
  72.     public class MyNode : TreeNode
  73.     {
  74.         public int NodeX, NodeY;
  75.         public int NCount = 0;
  76.  
  77.         //TreeNode dziedziczy tez naturalnie liste sasiadow
  78.         public MyNode(int x, int y)
  79.         {
  80.             NodeX = x;
  81.             NodeY = y;
  82.         }
  83.         public MyNode(int x, int y, string n)
  84.         {
  85.             NodeX = x;
  86.             NodeY = y;
  87.             Text = n;
  88.         }
  89.     }
  90.  
  91.     public class MyLine
  92.     {
  93.         public int sx, sy, ex, ey;
  94.  
  95.         public MyLine(int x1, int y1, int x2, int y2)
  96.         {
  97.             sx = x1;
  98.             sy = y1;
  99.             ex = x2;
  100.             ey = y2;
  101.         }
  102.     }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement