Shuxia Ning
2024-11-25 d4898c5d7e1bbbbba384a0e29f29c066d6f502a7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
 
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
 
namespace Yw.WinFrmUI.Q3d
{
    /// <summary>
    /// 几何计算辅助类
    /// </summary>
    class GraphHelper
    {
        public static Color getNodeColor(Colour colour, NodeViewModel node)
        {
            double value = 0;
            Color color = Color.Gray;
 
            switch (colour.Type)
            {
                case ColourType.节点自由压力:
                    value = node.EN_PRESSURE;
                    break;
                case ColourType.节点绝对压力:
                    value = node.EN_HEAD;
                    break;
                case ColourType.节点需水量:
                    value = node.EN_DEMAND;
                    break;
            }
 
            for (int i = 0; i < colour.Items.Count; i++)
            {
                if (colour.Items[i].DRange.IsInside(value))
                {
                    color = colour.Items[i].value;
                    break;
                }
            }
            return color;
        }
        public static Color getLinkColor(Colour colour, LinkViewModel link)
        {
            double value = 0;
            Color color = Color.Gray;
            switch (colour.Type)
            {
                case ColourType.管线流量:
                    value = link.EN_FLOW;
                    break;
                case ColourType.管线流速:
                    value = link.EN_VELOCITY;
                    break;
            }
 
            for (int i = 0; i < colour.Items.Count; i++)
            {
                if (colour.Items[i].DRange.IsInside(value))
                {
                    color = colour.Items[i].value;
                    break;
                }
            }
            return color;
        }
        public static long GetUlongByPoint(PointF ps, float delta = 20, int MaxY = 2000)
        {
            return (long)((int)(ps.X / delta) + ((int)Math.Round(ps.Y / delta)) * MaxY / delta);
        }
 
        public static List<PointF> Get等分Nodes(PointF p1, PointF p2, int n)
        {
            // 计算线段长度
            float len = (float)Math.Sqrt(Math.Pow(p2.X - p1.X, 2) + Math.Pow(p2.Y - p1.Y, 2));
            if (len == 0) len = 0.00001f;
            // 计算单位向量
            PointF u = new PointF((p2.X - p1.X) / len, (p2.Y - p1.Y) / len);
 
            // 计算间距
            float d = len / (n + 1);
 
 
            // 计算n等分点
            List<PointF> nodes = new List<PointF>();
            for (int i = 1; i < n + 1; i++)
            {
                PointF node = new PointF(p1.X + i * d * u.X, p1.Y + i * d * u.Y);
                if (node.X == float.NaN || node.Y == float.NaN) node = p1;
                nodes.Add(node);
            }
            //nodes.RemoveAt(0);
            //nodes.RemoveAt(nodes.Count - 1);
 
            //// 绘制线段
            //graphics.DrawLine(Pens.Black, p1, p2);
 
            //// 绘制3等分点和n等分点
            //foreach (PointF node in node3)
            //{
            //    graphics.FillEllipse(Brushes.Red, node.X - 2, node.Y - 2, 4, 4);
            //}
            //foreach (PointF node in nodes)
            //{
            //    graphics.FillEllipse(Brushes.Blue, node.X - 2, node.Y - 2, 4, 4);
            //}
            return nodes;
        }
 
 
        public static PointF getRotatePoint(float px, float py, PointF center, PointF x, PointF y)
        {
            PointF p = new PointF(px, py);
 
            return getRotatePoint(p, center, x, y);
        }
        private static PointF[] getRotatePoints(PointF[] ps, PointF center, PointF x, PointF y)
        {
            return ps.ToList().Select(p => getRotatePoint(p, center, x, y)).ToArray();
        }
 
 
        private static PointF getRotatePoint(PointF p, PointF center, PointF x, PointF y)
        {
            float angle = (float)Math.Atan2(y.Y - x.Y, y.X - x.X);
            float distance = (float)Math.Sqrt(Math.Pow(p.X - center.X, 2) + Math.Pow(p.Y - center.Y, 2));
            float rotationAngle = (float)(Math.Atan2(p.Y - center.Y, p.X - center.X) + angle);
            float rotatedX = center.X + distance * (float)Math.Cos(rotationAngle);
            float rotatedY = center.Y + distance * (float)Math.Sin(rotationAngle);
            return new PointF(rotatedX, rotatedY);
        }
 
        public static PointF[] getCurclePoints(int num)
        {
            PointF[] points = new PointF[num + 1];
            float angle = 0;
            for (int i = 0; i < points.Length; i++)
            {
                float x = (float)Math.Cos(angle);
                float y = (float)Math.Sin(angle);
                points[i] = new PointF(x, y);
                angle += 2 * (float)Math.PI / num;
            }
            points[num] = points[0];
            return points;
        }
    }
 
 
    public class Prompt : Form
    {
        private static TextBox textBox;
        private static Button okButton;
 
        public static string ShowDialog(string text, string caption, string defaultTxt = "")
        {
            Form prompt = new Form()
            {
                Width = 200,
                Height = 150,
                FormBorderStyle = FormBorderStyle.FixedDialog,
                Text = caption,
                StartPosition = FormStartPosition.CenterScreen
            };
 
            Label textLabel = new Label() { Left = 20, Top = 20, Text = text };
            textBox = new TextBox() { Left = 20, Top = 50, Width = 150 };
            textBox.Text = defaultTxt;
            okButton = new Button() { Text = "确定", Left = 75, Width = 75, Top = 80 };
            okButton.Click += (sender, e) => { prompt.Close(); };
 
            prompt.Controls.Add(textBox);
            prompt.Controls.Add(textLabel);
            prompt.Controls.Add(okButton);
            prompt.ShowDialog();
 
            return textBox.Text;
        }
    }
 
}