using OpenTK.Graphics.OpenGL;
|
using OpenTK.Mathematics;
|
using OpenTK.Windowing.Common;
|
using System.Drawing;
|
|
namespace Yw.WinFrmUI.Hydro
|
{
|
/// <summary>
|
/// 绘制水泵2d辅助类
|
/// </summary>
|
internal static class DrawPump2dExtensions
|
{
|
/// <summary>
|
/// 绘制水源
|
/// </summary>
|
public static void Draw2d(this PumpL3d pump, float zoom)
|
{
|
var width = pump.Style2d.Normal.Width;
|
var color = pump.Style2d.Normal.Color;
|
if (pump.IsSelected)
|
{
|
width = pump.Style2d.Selected.Width;
|
color = pump.Style2d.Selected.Color;
|
}
|
if (pump.IsHovered)
|
{
|
width = pump.Style2d.Hovered.Width;
|
color = pump.Style2d.Hovered.Color;
|
}
|
|
Draw2dHelper.DrawLine(width, color, pump.StartPosition, pump.EndPosition);
|
|
var start = new Vector3(pump.StartPosition.X, pump.StartPosition.Y, pump.StartPosition.Z);
|
var end = new Vector3(pump.EndPosition.X, pump.EndPosition.Y, pump.EndPosition.Z);
|
var middle = (start + end) / 2f;
|
var direction = (start - middle).Normalized();
|
var circle = middle + direction * (width * zoom * 2f);
|
Draw2dHelper.DrawPoint(width * 4f, color, circle);
|
|
|
direction = (end - circle).Normalized();
|
|
var rect = circle + direction * (width * zoom * 4f);
|
|
// 选择一个辅助向量(这里选择 Y 轴正方向)
|
Vector3 up = Vector3.UnitY;
|
if (MathHelper.Abs(Vector3.Dot(direction, up)) > 0.99f)
|
{
|
up = Vector3.UnitZ;
|
}
|
|
// 计算垂直于线段方向的向量(即左侧方向)
|
Vector3 leftDirection = Vector3.Cross(direction, up).Normalized();
|
|
// 计算矩形的四个顶点
|
Vector3 rectTopLeft = circle + leftDirection * (width * zoom);
|
Vector3 rectTopRight = rect + leftDirection * (width * zoom);
|
Vector3 rectBottomLeft = circle;
|
Vector3 rectBottomRight = rect;
|
|
// 绘制矩形
|
Draw2dHelper.DrawRectangle(rectTopLeft, rectTopRight, rectBottomRight, rectBottomLeft, color);
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
}
|