using System; using System.Windows; using System.Windows.Controls; using System.Windows.Media; using System.Windows.Shapes; namespace XP.ImageProcessing.RoiControl.Models { /// 一次点点距测量的所有视觉元素 public class MeasureGroup { // 保留原有圆点属性以兼容其他代码 public Ellipse Dot1 { get; set; } public Ellipse Dot2 { get; set; } // 新增:垂直线段(替代圆点的视觉表现) public Line PerpLine1 { get; set; } public Line PerpLine2 { get; set; } public Line Line { get; set; } public TextBlock Label { get; set; } public Point P1 { get; set; } public Point P2 { get; set; } /// 垂直线段长度(像素) public double PerpLineLength { get; set; } = 10.0; public double Distance { get { double dx = P2.X - P1.X, dy = P2.Y - P1.Y; return Math.Sqrt(dx * dx + dy * dy); } } public void UpdateLine() { Line.X1 = P1.X; Line.Y1 = P1.Y; Line.X2 = P2.X; Line.Y2 = P2.Y; Line.Visibility = Visibility.Visible; // 更新垂直线段位置 UpdatePerpLines(); } /// 更新两条垂直线段的位置 public void UpdatePerpLines() { if (PerpLine1 == null || PerpLine2 == null) return; // 计算两点连线的方向向量 double dx = P2.X - P1.X; double dy = P2.Y - P1.Y; double len = Math.Sqrt(dx * dx + dy * dy); if (len < 0.001) return; // 避免除零 // 归一化方向向量 double ux = dx / len; double uy = dy / len; // 垂直方向向量 (-uy, ux) double vx = -uy; double vy = ux; // P1处的垂直线段 double halfLen1 = PerpLineLength / 2; PerpLine1.X1 = P1.X + vx * halfLen1; PerpLine1.Y1 = P1.Y + vy * halfLen1; PerpLine1.X2 = P1.X - vx * halfLen1; PerpLine1.Y2 = P1.Y - vy * halfLen1; PerpLine1.Visibility = Visibility.Visible; // P2处的垂直线段 double halfLen2 = PerpLineLength / 2; PerpLine2.X1 = P2.X + vx * halfLen2; PerpLine2.Y1 = P2.Y + vy * halfLen2; PerpLine2.X2 = P2.X - vx * halfLen2; PerpLine2.Y2 = P2.Y - vy * halfLen2; PerpLine2.Visibility = Visibility.Visible; } public void UpdateLabel(string distanceText = null) { Label.Text = (Index > 0 ? $"#{Index} " : "") + (distanceText ?? $"{Distance:F2} px"); Canvas.SetLeft(Label, (P1.X + P2.X) / 2 + 8); Canvas.SetTop(Label, (P1.Y + P2.Y) / 2 - 18); Label.Visibility = Visibility.Visible; } public int Index { get; set; } } }