using Hydro.MapView;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Hydro.MapUI
{
///
/// 几何计算辅助类
///
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 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 nodes = new List();
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;
}
}
}