Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace autopilot_win
- {
- public partial class Form1 : Form
- {
- // ------------------------------------------------------------------------------------------
- //
- // воспомогательные классы и переменные
- //
- // ------------------------------------------------------------------------------------------
- public struct Coordinate
- {
- public double x;
- public double y;
- public double dist(Coordinate other)
- {
- return Math.Sqrt(Math.Pow(x - other.x, 2) + Math.Pow(y - other.y, 2));
- }
- public double distX(Coordinate other)
- {
- return (x - other.x);
- }
- public double distY(Coordinate other)
- {
- return (y - other.y);
- }
- }
- //public struct Neighbour { public double n; public double len; }
- private const int HOUSE_DENSITY = 1;
- private const double TRESHOLD = 1.0;
- //static Node[] nodes;
- static List<node_item> Nodes = new List<node_item>();
- static List<graph_item> Graph = new List<graph_item>();
- public struct node_item
- {
- public node_item(int _street, Coordinate _pos)
- {
- street = _street;
- pos = _pos;
- con = new Dictionary<int, double>();
- }
- public int street { get; }
- public Coordinate pos { get; }
- public double dist(node_item other)
- {
- return Math.Sqrt(Math.Pow(pos.x - other.pos.x, 2) + Math.Pow(pos.y - other.pos.y, 2));
- }
- public Dictionary<int, double> con;
- }
- public struct graph_item
- {
- public graph_item(int _start, int _end, int _type, List<node_item> Nodes)
- {
- start = _start;
- end = _end;
- type = _type;
- len = Nodes[end].dist(Nodes[start]);
- }
- public int start { get; }
- public int end { get; }
- public double len { get; }
- public int type { get; }
- }
- static Coordinate str_to_coord(string s)
- {
- string[] xy = s.Split(',');
- Coordinate c;
- Double.TryParse(xy[0], out c.x);
- Double.TryParse(xy[1], out c.y);
- return c;
- }
- public void load_text_data(string filename)
- {
- // ------------------------------------------------------------------------------------------
- //
- // читаем сегменты улиц, разбиваем на "дома", создаем из них узлы и графы
- //
- // ------------------------------------------------------------------------------------------
- int street_index = 0;
- int nodes_count = 0;
- Coordinate segment_start, segment_end, coord_insert;
- char[] charsToTrim = { '\n', '\r', ' ' };
- foreach (string line in System.IO.File.ReadLines(filename))
- {
- string street_line = line.Trim(charsToTrim);
- string[] segments = street_line.Split('\t');
- if (segments.Length > 2)
- {
- segment_start = str_to_coord(segments[1]);
- // add first node of street
- Nodes.Add(new node_item(street_index, segment_start));
- nodes_count++;
- // for each segment add nodes
- for (int i = 2; i < segments.Length; i++)
- {
- segment_end = str_to_coord(segments[i]);
- double segment_len = segment_end.dist(segment_start);
- int house_per_segment = (int)(segment_len / HOUSE_DENSITY);
- double dx = segment_end.distX(segment_start) / house_per_segment;
- double dy = segment_end.distY(segment_start) / house_per_segment;
- for (int h = 1; h < house_per_segment + 1; h++)
- {
- // добавляем очередную точку
- coord_insert.x = segment_start.x + h * dx;
- coord_insert.y = segment_start.y + h * dy;
- Nodes.Add(new node_item(street_index, coord_insert));
- // создаем граф из 2 последних добавленных точек
- Graph.Add(new graph_item(nodes_count - 1, nodes_count, 0, Nodes));
- nodes_count++;
- }
- segment_start = segment_end;
- }
- }
- else
- {
- Console.WriteLine(String.Format("Строка {0} содержит меньше координат, ожидается 2 или больше, есть {1}",
- street_index, segments.Length));
- }
- street_index++;
- }
- // ------------------------------------------------------------------------------------------
- //
- // добавляем для каждого узла его соседей
- //
- // ------------------------------------------------------------------------------------------
- node_item n;
- for (int g = 0; g < Graph.Count; g++)
- {
- n = Nodes[Graph[g].start];
- if (!n.con.ContainsKey(Graph[g].end))
- {
- n.con.Add(Graph[g].end, n.dist(Nodes[Graph[g].end]));
- }
- n = Nodes[Graph[g].end];
- if (!n.con.ContainsKey(Graph[g].start))
- {
- n.con.Add(Graph[g].start, n.dist(Nodes[Graph[g].start]));
- }
- }
- // ------------------------------------------------------------------------------------------
- //
- // добавляем стыки улиц
- //
- // ------------------------------------------------------------------------------------------
- bool graph_exists(int node1, int node2)
- {
- for (int g = 0; g < Graph.Count; g++)
- {
- if (((Graph[g].start == node1) && (Graph[g].end == node2)) || ((Graph[g].start == node2) && (Graph[g].end == node1)))
- return true;
- }
- return false;
- }
- int graph_count = Graph.Count;
- double l;
- for (int ns = 0; ns < Nodes.Count; ns++)
- for (int nd = 0; nd < Nodes.Count; nd++)
- {
- if (Nodes[ns].street != Nodes[nd].street)
- {
- l = Nodes[nd].dist(Nodes[ns]);
- if (l <= TRESHOLD)
- {
- if (!Nodes[ns].con.ContainsKey(nd))
- Nodes[ns].con.Add(nd, l);
- if (!Nodes[nd].con.ContainsKey(ns))
- Nodes[nd].con.Add(ns, l);
- if (!graph_exists(ns, nd))
- Graph.Add(new graph_item(ns, nd, 1, Nodes));
- }
- }
- }
- /*
- Form1.textBox1.Lines = string.Format("Прочитано улиц: {0}\r\n", street_index);
- Console.WriteLine(String.Format("Созданно узлов: {0}", Nodes.Count));
- Console.WriteLine(String.Format("Графов: {0} (добавлено: {1})", Graph.Count, Graph.Count - graph_count));
- Console.WriteLine("");
- Console.WriteLine("Нажмите любую кнопку для выхода...");
- Console.ReadKey();
- */
- }
- private PictureBox pictureBox1 = new PictureBox();
- private Font fnt = new Font("Arial", 10);
- public Form1()
- {
- InitializeComponent();
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- // Dock the PictureBox to the form and set its background to white.
- pictureBox1.Dock = DockStyle.Fill;
- pictureBox1.BackColor = Color.White;
- // Connect the Paint event of the PictureBox to the event handler method.
- pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox1_Paint);
- // Add the PictureBox control to the Form.
- this.Controls.Add(pictureBox1);
- textBox1.Text = string.Format("Прочитано улиц: {0}\r\n",1);
- //load_text_data(@"f:\tusur2\map1.txt");
- }
- private void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
- {
- // Create a local version of the graphics object for the PictureBox.
- Graphics g = e.Graphics;
- // Draw a string on the PictureBox.
- g.DrawString("This is a diagonal line drawn on the control",
- fnt, System.Drawing.Brushes.Blue, new Point(30, 30));
- // Draw a line in the PictureBox.
- g.DrawLine(System.Drawing.Pens.Red, pictureBox1.Left, pictureBox1.Top,
- pictureBox1.Right, pictureBox1.Bottom);
- }
- private void textBox1_TextChanged(object sender, EventArgs e)
- {
- }
- private void Form1_MouseDown(object sender, MouseEventArgs e)
- {
- }
- private void Form1_Resize(object sender, EventArgs e)
- {
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement