From 28e1c9f3b620567acced4730cc36368090a6abe5 Mon Sep 17 00:00:00 2001 From: cloudflight <cloudflight@126.com> Date: 星期六, 15 六月 2024 12:56:50 +0800 Subject: [PATCH] feat:添加右上角视角辅助栏 --- Hydraulic/Hydro.MapUI/Map/MapViewer.Draw.cs | 126 +++++++++++++++++++++++++++++++++++++++++ 1 files changed, 125 insertions(+), 1 deletions(-) diff --git a/Hydraulic/Hydro.MapUI/Map/MapViewer.Draw.cs b/Hydraulic/Hydro.MapUI/Map/MapViewer.Draw.cs index a8af967..aa41280 100644 --- a/Hydraulic/Hydro.MapUI/Map/MapViewer.Draw.cs +++ b/Hydraulic/Hydro.MapUI/Map/MapViewer.Draw.cs @@ -1,4 +1,5 @@ 锘縰sing Hydro.MapView; +using Hydro.MapView.Base; using Hydro.MapView.Common; using System; using System.Collections.Generic; @@ -130,7 +131,7 @@ if (template == null) return; var _Nodes = template.network.Nodes.ViewNodes; var _Links = template.network.Links.ViewLinks; - + var _Areas = template.network.Areas; var Cpoints = getCurclePoints(64).ToList(); var r = 1.73f / zoom; @@ -161,6 +162,52 @@ Pen penClosed = new Pen(Color.OrangeRed, 2 * r); Pen penHovered = new Pen(Color.DeepSkyBlue, 5 * r); + + //缁樺埗闈� + using (Pen pen0 = new Pen(Color.FromArgb(0, 0, 255), 2 * r)) + { + foreach (var area in _Areas) + { + + if (!area.Visible) continue; + if (!IsFaceVisibleToCamera(area, GetCameraPosition())) continue; + if (area.Elev < minElve || area.Elev >= maxElve) continue; + + var p = new List<PointF>(); + foreach (var node in area.InnerNodes) + { + p.Add(WorldPointToMapPoint(node, template.OffSet)); + } + if (p.Count < 3) continue; + pen0.Color = penClosed.Color = area.color; + Pen pen = pen0; + if (area.Hovered) pen = penHovered; + bufferG.FillPolygon(pen.Brush, p.ToArray()); + bufferG.DrawPolygon(pen, p.ToArray()); + //鏄剧ずarea鐨勫悕绉� + var c = new PointF(p.Average(p0 => p0.X), p.Average(p0 => p0.Y)); + var brush=new SolidBrush(Color.White); + + + + + var gs = bufferG.Save(); + // 搴旂敤鐭╅樀鍙樻崲浠ユ姷娑堜箣鍓嶇殑缈昏浆鏁堟灉 + bufferG.ScaleTransform(1 / Zoom.X, 1 / Zoom.Y); + + Font font = new Font(FontFamily.GenericSansSerif, 10*10*zoom); + + SizeF textSize = bufferG.MeasureString(area.Name, font); + var center = new PointF(c.X * Zoom.X, c.Y * Zoom.Y); + float textLeft = center.X - textSize.Width / 2; + float textTop = center.Y - textSize.Height / 2; + PointF pd = new PointF(textLeft, textTop); + + bufferG.DrawString(area.Name, font, brush, pd); + // 鎭㈠涔嬪墠淇濆瓨鐨勭粯鍥剧姸鎬� + bufferG.Restore(gs); + } + } // 缁樺埗绾� @@ -963,7 +1010,84 @@ #endregion + #region 璁$畻涓夌淮鐩告満 + public PointF3D GetCameraPosition( float distance=1) + { + // 灏嗚搴﹁浆鎹负寮у害 + float rotationRadians = (float)Rotation * (float)Math.PI / 180; + float rotationFRadians = (float)RotationF * (float)Math.PI / 180; + // 璁$畻鐩告満鐨勭悆鍧愭爣绯讳綅缃� + float x = distance * (float)Math.Sin(rotationFRadians) * (float)Math.Cos(rotationRadians); + float y = distance * (float)Math.Sin(rotationFRadians) * (float)Math.Sin(rotationRadians); + float z = distance * (float)Math.Cos(rotationFRadians); + + return new PointF3D(x, y, z); + } + public bool IsFaceVisibleToCamera(AreaViewModel area, PointF3D cameraPosition) + { + + //姝i潰鏄�2锛屽彸闈㈡槸1锛屽乏闈㈡槸3锛岃儗闈㈡槸4 + //Rotation涓�0鏃讹紝鍙樉绀烘闈紝Rotation涓�90鏃讹紝鍙樉绀哄彸闈紝Rotation涓�180鏃讹紝鍙樉绀鸿儗闈紝Rotation涓�270鏃讹紝鍙樉绀哄乏闈� + int delta = 5; + bool flag = false; + switch (area.Name) + { + case "宸�": + //Rotation涓�0~180鏃舵樉绀� + if (RotationF<=90-delta && Rotation >= 0 + delta && Rotation <= 180 - delta) + flag = true; + break; + case "鍓�": + //Rotation涓�0~180鏃舵樉绀� + if (RotationF <= 90 - delta && Rotation >= -90 + delta && Rotation <= 90 - delta) + flag = true; + break; + case "鍙�": + //Rotation涓�0~180鏃舵樉绀� + if (RotationF <= 90 - delta && Rotation >= -180 + delta && Rotation <= 0 - delta) + flag = true; + break; + case "鍚�": + //Rotation涓�0~180鏃舵樉绀� + if (RotationF <= 90 - delta && ((Rotation >= 90 + delta && Rotation<=180)||( Rotation>=-180 && Rotation <= -90 - delta))) + flag = true; + break; + case "涓�": + if (RotationF >= 0 + delta) + flag = true; + break; + case "涓�": + if (RotationF <= 0 - delta) + flag = true; + break; + } + return flag; + + var faceVertices = area.InnerNodes; + if (faceVertices.Count < 3) return false; + PointF3D faceVertexA = faceVertices[0].Position3D; + PointF3D faceVertexB = faceVertices[1].Position3D; + PointF3D faceVertexC = faceVertices[2].Position3D; + + // 璁$畻娉曞悜閲� + PointF3D AB = faceVertexB - faceVertexA; + PointF3D AC = faceVertexC - faceVertexA; + PointF3D normal = AB ^ AC; + + // 璁$畻浠庣浉鏈轰綅缃埌闈㈢殑鍚戦噺 + PointF3D PD = faceVertexA - cameraPosition; + + // 璁$畻鐐圭Н + float dotProduct = normal * PD; + + // 鍒ゆ柇闈㈡槸鍚︽湞鍚戠浉鏈� + bool isFacingCamera = dotProduct > 0; + return isFacingCamera; + } + + + #endregion #region 鍒ゆ柇鍙鎬� private float Get_dist(PointF A, PointF B) -- Gitblit v1.9.3