示例#1
0
        /// <summary>
        /// 设置位置
        /// </summary>
        public void SetPosition()
        {
            #region 弧线
            lineElement.Point2 = new Point(EndPointX, EndPointY);
            LinePathFigureElement.StartPoint = new Point(StartPointX, StartPointY);

            #endregion

            #region 箭头
            Point  roundCenter = new Point();
            Point  textCenterPoint;
            double k1;
            //求圆心
            try
            {
                roundCenter = MathExtension.GetBezierCenter(LinePathFigureElement.StartPoint, lineElement.Point2, RadiusRadio * Length, this.LineSweepDirection);
                var cp = new Point()
                {
                    X = (StartPointX + EndPointX) / 2,
                    Y = (StartPointY + EndPointY) / 2,
                };

                textCenterPoint = new Point()
                {
                    X = (cp.X + roundCenter.X) / 2,
                    Y = (cp.Y + roundCenter.Y) / 2,
                };
                //圆心与箭头点连线的斜率
                k1 = (roundCenter.Y - this.EndPointY) / (roundCenter.X - this.EndPointX);
            }
            catch (Exception ex)
            {
                if (ex is RadiusException)
                {
                    k1 = (-1) * (this.StartPointX - this.EndPointX) / (this.StartPointY - this.EndPointY);
                    textCenterPoint = roundCenter = new Point()
                    {
                        X = (this.StartPointX + this.EndPointX) / 2,
                        Y = (this.StartPointY + this.EndPointY) / 2
                    };

                    SetLinePosition();
                    textBoxElement.Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity));

                    Canvas.SetLeft(textBoxElement, textCenterPoint.X - textBoxElement.DesiredSize.Width / 2);
                    Canvas.SetTop(textBoxElement, textCenterPoint.Y - textBoxElement.DesiredSize.Height / 2);
                    return;
                }
                else
                {
                    return;
                }
            }
            this.lineElement.Point1 = roundCenter;
            Point centerPoint1, centerPoint2, endPoint11, endPoint12, endPoint21, endPoint22;


            //切线斜率
            double k2 = 0;

            {
                k2 = k1;

                double xOffset = this.ArrowSize.Height / (Math.Sqrt(1 + k2 * k2));

                double xTemp, yTemp;

                #region 第一个点
                xTemp        = this.EndPointX + xOffset;
                yTemp        = k2 * (xTemp - this.EndPointX) + this.EndPointY;
                centerPoint1 = new Point(Math.Round(xTemp, 4), Math.Round(yTemp, 4));
                #endregion

                #region 第二个点
                xTemp        = this.EndPointX - xOffset;
                yTemp        = k2 * (xTemp - this.EndPointX) + this.EndPointY;
                centerPoint2 = new Point(Math.Round(xTemp, 4), Math.Round(yTemp, 4));
                #endregion

                k2 = (-1) / k1;

                xOffset = this.ArrowSize.Width / (Math.Sqrt(1 + k2 * k2));

                #region 第一个点对应的两个箭头端点
                xTemp      = centerPoint1.X + xOffset;
                yTemp      = k2 * (xTemp - centerPoint1.X) + centerPoint1.Y;
                endPoint11 = new Point(Math.Round(xTemp, 4), Math.Round(yTemp, 4));

                xTemp      = centerPoint1.X - xOffset;
                yTemp      = k2 * (xTemp - centerPoint1.X) + centerPoint1.Y;
                endPoint12 = new Point(Math.Round(xTemp, 4), Math.Round(yTemp, 4));
                #endregion

                #region 第二个点对应的两个箭头端点
                xTemp      = centerPoint2.X + xOffset;
                yTemp      = k2 * (xTemp - centerPoint2.X) + centerPoint2.Y;
                endPoint21 = new Point(Math.Round(xTemp, 4), Math.Round(yTemp, 4));

                xTemp      = centerPoint2.X - xOffset;
                yTemp      = k2 * (xTemp - centerPoint2.X) + centerPoint2.Y;
                endPoint22 = new Point(Math.Round(xTemp, 4), Math.Round(yTemp, 4));
                #endregion

                //小弧

                #region 永远找和圆心距离近的那组点

                {
                    double d1 = (endPoint11.X - roundCenter.X) * (endPoint11.X - roundCenter.X) + (endPoint11.Y - roundCenter.Y) * (endPoint11.Y - roundCenter.Y);
                    double d2 = (endPoint21.X - roundCenter.X) * (endPoint21.X - roundCenter.X) + (endPoint21.Y - roundCenter.Y) * (endPoint21.Y - roundCenter.Y);


                    //if (this.LineSweepDirection== SweepDirection.Clockwise)
                    if (d1 < d2)
                    {
                        this.arrowStartElement.StartPoint = endPoint11;
                        this.arrowElement.Points.Clear();
                        this.arrowElement.Points.Add(lineElement.Point2);
                        this.arrowElement.Points.Add(endPoint12);
                    }
                    else
                    {
                        this.arrowStartElement.StartPoint = endPoint21;
                        this.arrowElement.Points.Clear();
                        this.arrowElement.Points.Add(lineElement.Point2);
                        this.arrowElement.Points.Add(endPoint22);
                    }
                }
                #endregion
            }
            #endregion

            textBoxElement.Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity));

            Canvas.SetLeft(textBoxElement, textCenterPoint.X - textBoxElement.DesiredSize.Width / 2);
            Canvas.SetTop(textBoxElement, textCenterPoint.Y - textBoxElement.DesiredSize.Height / 2);
        }