根据指定距离分割折线

来源:互联网 发布:淘宝企业店铺如何申请 编辑:程序博客网 时间:2024/06/05 02:56

        /// <summary>
        /// 根据指定距离分割折线
        /// </summary>
        /// <param name="coords">折线坐标,长度为2*n</param>
        /// <param name="result">返回的分段节点坐标</param>
        /// <param name="length">指定分割长度</param>
        private void CalSplitPoints(double[] coords, ref double[] result, double length)
        {
            int size=coords.Length/2;
            double[] lengths = new double[size];
            double totalLength = 0;
            int i;
            lengths[0] = 0;
            for (i = 1; i < size; i++)
            {
                int tempI = 2 * (i-1);
                lengths[i] = Math.Sqrt((coords[tempI + 2] - coords[tempI]) * (coords[tempI + 2] - coords[tempI])
                    + (coords[tempI + 3] - coords[tempI + 1]) * (coords[tempI + 3] - coords[tempI + 1]));
                totalLength += lengths[i];
            }
            axis = new double[2 * (int)(totalLength / length) + 2];

            int count = axis.Length / 2;
            int currentI = -1;
            double currentL = 0;
            double tempLengths=0;
            axis[0] = coords[0]; axis[1] = coords[1];           

            for (i = 1; i < count; i++)
            {
                currentL = (i-1) * length;
                while ((currentL + length) > tempLengths)
                {
                    if (currentI == size - 1)
                    {
                        break;
                    }
                    currentI++;
                    tempLengths += lengths[currentI];
                }
                double lastLength = tempLengths - lengths[currentI];
                double nowLength = currentL + length - lastLength;
                //nowLength为点currentI-1和currentI两点间离点currentI-1的距离
                double ratio = nowLength / lengths[currentI];
                int tempI = 2 * (currentI-1);
                axis[2 * i] = ratio * (coords[tempI + 2] - coords[tempI]) + coords[tempI];
                axis[2 * i+1] = ratio * (coords[tempI + 3] - coords[tempI+1]) + coords[tempI+1];
            }
        }

原创粉丝点击