简体   繁体   English

C#画线平行于折线

[英]C# Drawing line parallel to a Polyline

I'm creating a program in wpf that draws polyline as well as a line an offset away which is parallel. 我正在wpf中创建一个程序,该程序绘制折线以及一条偏移量为平行的线。 It works perfectly for the first set of parallel lines, but on each following line the right line is off angled(Shown in red) . 它非常适合第一组平行线,但在随后的每条平行线上,右行都是倾斜的(以红色显示)。

在此处输入图片说明

Code so far: 到目前为止的代码:

        private void DrawingCanvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) {
        if (polylineLeft != null) {
            var canvas = (Canvas)sender;
            leftSegment.Points[1] = e.GetPosition(canvas);

            var distance = (leftSegment.Points[0] - leftSegment.Points[1]).Length;

            if (distance >= 20) {

                polylineLeft.Points.Add(leftSegment.Points[1]);
                //calculate second line
                var L = Math.Sqrt((leftSegment.Points[0].X - leftSegment.Points[1].X) * 
                    (leftSegment.Points[0].X - leftSegment.Points[1].X) + 
                    (leftSegment.Points[0].Y - leftSegment.Points[1].Y) * 
                    (leftSegment.Points[0].Y - leftSegment.Points[1].Y));
                var x1p = leftSegment.Points[0].X + width * (leftSegment.Points[1].Y-leftSegment.Points[0].Y) / L;
                var x2p = leftSegment.Points[1].X + width * (leftSegment.Points[1].Y-leftSegment.Points[0].Y) / L;
                var y1p = leftSegment.Points[0].Y + width * (leftSegment.Points[0].X-leftSegment.Points[1].X) / L;
                var y2p = leftSegment.Points[1].Y + width * (leftSegment.Points[0].X-leftSegment.Points[1].X) / L;
                if (!initialLeftPoint) {
                    polylineRight.Points.Clear();
                    polylineRight.Points.Add(new Point(x1p, y1p));
                    initialLeftPoint = true;
                }
                polylineRight.Points.Add(new Point(x2p, y2p));
                leftSegment.Points[0] = leftSegment.Points[1];
                rightSegment.Points[0] = rightSegment.Points[1];
            } else {
                if (polylineLeft.Points.Count < 2) {
                    canvas.Children.Remove(polylineLeft);
                }

                polylineLeft = null;
                polylineRight = null;
                leftSegment.Points.Clear();
                rightSegment.Points.Clear();
                canvas.Children.Remove(leftSegment);
                canvas.Children.Remove(rightSegment);
            }
        }
    }

How do I ensure that on my second line, (Red) it is parallel with the main line (Green)? 如何确保第二行(红色)与主行(绿色)平行?

One part of the problem is quite easy to solve with the help of the WPF Vector structure. 问题的一部分很容易借助WPF Vector结构来解决。 Given a line segment between the two Points p1 and p2 , you could calculate the normal vector like this: 给定两个点p1p2之间的线段,您可以像这样计算法线向量:

Point p1 = ...
Point p2 = ...
var v = p2 - p1;
var n = new Vector(v.Y, -v.X);
n.Normalize();
// now n is a Vector of length 1, perpendicular to the line p1-p2

You could now create a parallel line segment (given by Points p3 and p4 ) like this: 现在,您可以像这样创建一个平行线段(由Points p3p4给定):

var distance = 20d;
var p3 = p1 + n * distance;
var p4 = p3 + v;

However, the above code creates a parallel line segment of the same length as the original one. 但是,以上代码创建的平行线段的长度与原始长度相同。 This may not be exactly what you want, as I guess you want to create a "parallel polyline". 这可能不完全是您想要的,因为我想您想创建“平行多段线”。 If that is the case, things get a bit more complicated because you would also have to calculate the intersections between adjacent segments of the parallel polyline. 在这种情况下,事情会变得更加复杂,因为您还必须计算平行多段线的相邻线段之间的交点。 It may even happen that some of these segments disappear during these calculations. 这些段中的某些段甚至可能在这些计算过程中消失。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM