ningshuxia
2025-03-26 c44fcc54651c187d0883a041df60a3baf0164e9f
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
namespace HydroUI
{
    /// <summary>
    /// 几何计算辅助类
    /// </summary>
    internal 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);
            }
 
            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 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);
        }
 
    }
 
}