基于折线生成平行线的C#算法实现

基于折线生成平行线的C#算法实现_第1张图片经过几天的研究,基于C#实现了一根折线生成平行线,解决了出现折点时的坐标变化。可应用于地图开发中的路段展现,也可以应用于管线设计应用。

下面是代码片段,如需要进一步沟通请加Q59135929


 public System.Drawing.Point[] OffsetPtsLine(System.Drawing.Point[] pts, double pixels)
        {
            //小于2个点不进行计算
            if (pts.Length < 2)
                return pts;
            Point[] tmpPoints = (Point[])pts.Clone();
            //角度变量
            double angle, angleA, angleB;
            //交接点变量
            int x = 0, y = 0;
            //计算点对象
            Point ptA = new Point();
            Point ptB = new Point();
            for (int i = 0; i < pts.Length - 1; i++)
            {
                Point pt1 = pts[i];
                Point pt2 = pts[i + 1];
                //计算斜率
                angleA = Math.Atan2((pt2.Y - pt1.Y), (pt2.X - pt1.X));
                if (i + 2 < pts.Length)
                {
                    //当线段超出二段时,以三个点为一组进行计算
                    Point pt3 = pts[i + 2];
                    angleB = Math.Atan2((pt3.Y - pt2.Y), (pt3.X - pt2.X));
                    angle = angleA;
                    double k = (angleB - angleA - Math.PI) / 2;
                    angle = angleA + k;
                    double pixels1 = pixels / Math.Sin(k);
                    x = (int)(pt2.X - pixels1 * Math.Cos(angle));
                    y = (int)(pt2.Y - pixels1 * Math.Sin(angle));
                }
                else
                {
                    //线段终点
                    Point tmpPt = this.Offset(pt2, pixels, angleA);
                    x = tmpPt.X;
                    y = tmpPt.Y;
                    //如果终点和起点一样,则是闭合状态
                    if (pt2.Equals(pts[0]) && pts.Length>2)
                    {
                       
                        pt1 = pts[pts.Length - 2];
                        Point pt3 = pts[1];
                        angleA = Math.Atan2((pt2.Y - pt1.Y), (pt2.X - pt1.X));
                        angleB = Math.Atan2((pt3.Y - pt2.Y), (pt3.X - pt2.X));
                        angle = angleA;
                        double k = (angleB - angleA - Math.PI) / 2;
                        angle = angleA + k;
                        double pixels1 = pixels / Math.Sin(k);
                        x = (int)(pt2.X - pixels1 * Math.Cos(angle));
                        y = (int)(pt2.Y - pixels1 * Math.Sin(angle));
                        tmpPoints[0] = new Point(x, y);
                    }
                }
                //线段起点
                if (i == 0)
                {                   
                    ptA = this.Offset(pt1, pixels, angleA);
                    tmpPoints[i] = ptA;
                }
               



你可能感兴趣的:(基于折线生成平行线的C#算法实现)