public static Vector2D GetBottomRightUnitPoint(this ICanvasScreenConvertable canvasProxy) { if (canvasProxy == null) { throw new ArgumentNullException(nameof(canvasProxy)); } return(canvasProxy.ToUnit(new Vector2D(canvasProxy.ActualWidth, canvasProxy.ActualHeight))); }
public static Vector2D GetTopLeftUnitPoint(this ICanvasScreenConvertable canvasProxy) { if (canvasProxy == null) { throw new ArgumentNullException(nameof(canvasProxy)); } return(canvasProxy.ToUnit(new Vector2D(0, 0))); }
/// <summary> /// 将视图坐标转化为工程数学坐标; /// </summary> /// <param name="screenPoint"></param> /// <returns></returns> public static Vector2D ToUnit(this ICanvasScreenConvertable canvasProxy, Vector2D screenPoint) { if (canvasProxy == null) { throw new ArgumentNullException(nameof(canvasProxy)); } if (screenPoint == null) { throw new ArgumentNullException(nameof(screenPoint)); } var unitPoint = new Vector2D(); canvasProxy.ToUnit(screenPoint, unitPoint); return(unitPoint); }
/// <summary> /// 在未完成的线段的中点附近的位置绘制线段的长度; /// </summary> /// <param name="canvas"></param> /// <param name="canvasProxy"></param> /// <param name="editingLine"></param> public static void DrawEditingLineLengthString(ICanvas canvas, ICanvasScreenConvertable canvasProxy, Line2D editingLine) { var lengthString = editingLine.Length.ToString("F2"); var direction = editingLine.Direction; if (direction == null) { return; } //通过法向量确定其它提示信息的位置; var verticalDir = new Vector2D(-direction.Y, direction.X); //将视图距离转化为工程数学距离; var unitDistance = canvasProxy.ToUnit( ScreenDistanceLineEditingWithLine ); var paraLinesDistance = verticalDir * unitDistance; #region 绘制中点,调试用; #if DEBUG //var middleScreenPoint = canvasProxy.ToScreen(editingLine.MiddlePoint); //var surroundRect = NativeGeometryUtil.GetNativeSuroundingScreenRect(middleScreenPoint, 8, 8); //canvas.NativeDrawRectangle( // surroundRect, // Tida.Canvas.Shell.Constants.HighLightRectColorBrush, // Tida.Canvas.Shell.Constants.LinePen //); #endif #endregion //绘制提示长度; var editingTextPosition = editingLine.MiddlePoint + paraLinesDistance / 1.2; canvas.DrawText( lengthString, LineEditingSnappingLengthFontSize, LineEditingTipBrush, editingTextPosition ); }
/// <summary> /// 为未完成的线段绘制外围辅助线,这包括了一条平行与原线段的线段,以及垂直于原线段的两条侧边; /// </summary> /// <param name="canvas"></param> /// <param name="canvasProxy"></param> /// <param name="editingLine"></param> public static void DrawEditingLineOutLines(ICanvas canvas, ICanvasScreenConvertable canvasProxy, Line2D editingLine) { var direction = editingLine.Direction; if (direction == null) { return; } //通过法向量确定其它提示线的位置; var verticalDir = new Vector2D(-direction.Y, direction.X); //将视图距离转化为工程数学距离; var unitDistance = canvasProxy.ToUnit( ScreenDistanceLineEditingWithLine ); var paraLinesDistance = verticalDir * unitDistance; //绘制外围的平行线; var outLinePara = new Line2D( editingLine.Start + verticalDir * unitDistance, editingLine.End + verticalDir * unitDistance ); canvas.DrawLine(LineEditingTipPen, outLinePara); var verticalLineTime = 1.2; //绘制两侧垂直线; var verticalLine0 = new Line2D( editingLine.Start, editingLine.Start + verticalDir * unitDistance * verticalLineTime ); var verticalLine1 = new Line2D( editingLine.End, editingLine.End + verticalDir * unitDistance * verticalLineTime ); canvas.DrawLine(LineEditingTipPen, verticalLine0); canvas.DrawLine(LineEditingTipPen, verticalLine1); }
public override void Draw(ICanvas canvas, ICanvasScreenConvertable canvasProxy) { var line2D = GetPreviewLine2D(MousePositionTracker.CurrentHoverPosition); line2D = line2D ?? Line2D; var direction = line2D.Direction; if (direction == null) { return; } var vDir = direction.X > 0 ? new Vector2D(-direction.Y, direction.X) : new Vector2D(direction.Y, -direction.X); var screenDis = 36d; var unitDis = canvasProxy.ToUnit(screenDis); var measureLineDis = vDir * unitDis; MeasureLineP = new Line2D(line2D.Start + vDir * unitDis, line2D.End + vDir * unitDis); canvas.DrawLine(MeasurePenP, MeasureLineP); var verticalLineTimes = 1.2; MeasureLineV0 = new Line2D( line2D.Start, line2D.Start + vDir * unitDis * verticalLineTimes ); MeasureLineV1 = new Line2D( line2D.End, line2D.End + vDir * unitDis * verticalLineTimes ); canvas.DrawLine(MeasurePenV, MeasureLineV0); canvas.DrawLine(MeasurePenV, MeasureLineV1); if (IsSelected) { LineDrawExtensions.DrawSelectedLineState(MeasureLineP, canvas, canvasProxy, SelectionPen); LineDrawExtensions.DrawSelectedLineState(MeasureLineV0, canvas, canvasProxy, SelectionPen); LineDrawExtensions.DrawSelectedLineState(MeasureLineV1, canvas, canvasProxy, SelectionPen); } //line2D = outlineParaLine; var middlePoint = MeasureLineP.MiddlePoint; if (middlePoint == null) { return; } var text = $"{line2D.Length.ToString(LengthFormat)}"; double width = 0; var chars = text.ToCharArray(); for (int i = 0; i < chars.Length; i++) { width += canvasProxy.GetCharScreenSize(chars[i]).Width; } width = -canvasProxy.ToUnit(width * Constants.TipFontSize_LengthMeasurement); var height = canvasProxy.ToUnit(canvasProxy.GetCharScreenSize(chars[0]).Height *Constants.TipFontSize_LengthMeasurement * 1.15); var a = direction.AngleWith(Vector2D.BasisX); a = direction.X * direction.Y > 0 ? a : -a; var vec = new Vector2D(width / 2 * Math.Cos(-a) + height * Math.Sin(-a), -width / 2 * Math.Sin(-a) + height * Math.Cos(-a)); middlePoint = middlePoint + vec; canvas.DrawText( $"{line2D.Length.ToString(LengthFormat)}", Constants.TipFontSize_LengthMeasurement, TextForeground_LengthMeasurement, middlePoint, Math.PI / 2 - vDir.AngleFrom(Vector2D.BasisX) ); }
/// <summary> /// 线段的辅助判断; /// 依次判断以下情况:1.中点 2.端点 /// </summary> public static ISnapShape GetLine2DSnapShape(Line2D line2D, Vector2D position, ICanvasScreenConvertable canvasProxy) { if (line2D == null) { throw new ArgumentNullException(nameof(line2D)); } if (position == null) { throw new ArgumentNullException(nameof(position)); } if (canvasProxy == null) { throw new ArgumentNullException(nameof(canvasProxy)); } /*判断是否是三点中一个附近的点*/ var startPoint = line2D.Start; var endPoint = line2D.End; if (startPoint == null) { throw new ArgumentNullException(nameof(startPoint)); } if (endPoint == null) { throw new ArgumentNullException(nameof(endPoint)); } if (position == null) { throw new ArgumentNullException(nameof(position)); } if (canvasProxy == null) { throw new ArgumentNullException(nameof(canvasProxy)); } var middlePoint = (startPoint + endPoint) / 2; Vector2D[] points = { startPoint, endPoint, middlePoint }; var screenPoint = new Vector2D(); var screenStart = new Vector2D(); var screenEnd = new Vector2D(); var lineDir = (endPoint - startPoint).Normalize(); var verticalVector = new Vector2D(-lineDir.Y, lineDir.X); var screenPosition = canvasProxy.ToScreen(position); foreach (var point in points) { canvasProxy.ToScreen(point, screenPoint); if (!GetIsSurround(screenPosition, screenPoint)) { continue; } var unitLineLength = canvasProxy.ToUnit(4 * TolerantedScreenLength); var start = point - verticalVector * unitLineLength; var end = point + verticalVector * unitLineLength; //作垂直于原线段的垂线; canvasProxy.ToScreen(start, screenStart); canvasProxy.ToScreen(end, screenEnd); var lineSnapShape = new ScreenLineSnapShape(new Line2D(screenStart, screenEnd), point); //若命中点为中点,则该垂线为中垂线,则修改颜色; if (point == middlePoint) { lineSnapShape.LinePen = MiddleLinePen; } return(lineSnapShape); } return(null); }