LinkZY3471 2023-01-23 12:02 采纳率: 50%
浏览 105
已结题

C# 道格拉斯线压缩算法 求解

目的是输入一段曲线的首尾点,然后利用垂距法得到特征点,以此重新产生两端曲线进行递归,请问下面代码哪里出错了呢

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Collections;

namespace test1
{
    public class DG
    {
        List<Point> PList;
        public List<Point> keep = new List<Point>();
        public int index = 0;
        ArrayList d = new ArrayList();
        ArrayList dtemp = new ArrayList();
        double D = 10.0;

        public DG()
        { }

        public DG(List<Point> List)
        {
            PList = List;
        }

        public void getID(int bid,int eid)
        {
            Vector temp = new Vector();
            
            for (int i = 0; i < eid - bid + 1; i++)
            { 
                Vector iTOb = new Vector(PList[i],PList[bid]);
                Vector iTOe = new Vector(PList[i],PList[eid]);
                Vector bTOe = new Vector(PList[bid], PList[eid]);
                double length = temp.Length(bTOe);
                double cross = Math.Abs(temp.VectorX(iTOb, iTOe)) / 2.0;
                double t = cross / length;

                d.Add(t);
                dtemp.Add(t);
            }
            if (eid <= bid + 1)
            {
                return;
            }
            dtemp.Sort();
            dtemp.Reverse();
            int maxindex = d.IndexOf(dtemp[0]);

            if (((double)dtemp[0]) > D)
            {
                keep.Add(PList[maxindex]);
                d.Clear();
                dtemp.Clear();
                getID(bid, maxindex);
                getID(maxindex, eid);
                index++;
            }
            else return;

        }
    }
}

  • 写回答

4条回答 默认 最新

  • heart_6662 2023-01-23 16:23
    关注

    我看见你的程序看起来没有明显错误,但是有一些可以改进的地方。

    我发现你使用了ArrayList来存储d和dtemp,这是不必要的,因为List可以做到同样的事情。

    在getID方法中,你使用了一个循环来遍历eid-bid+1个点,这样会导致重复计算,影响程序的效率。可以改进为用一个变量i来遍历这些点。

    在if (eid <= bid + 1)语句块中,你返回了,但是你没有清空d和dtemp这两个数组,这会导致累加。

    如果你不能找到问题的根源,可以使用调试器来跟踪程序的运行过程,检查变量和数组的值是否正确。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 1月24日
  • 已采纳回答 1月24日
  • 赞助了问题酬金15元 1月23日
  • 创建了问题 1月23日

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么