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 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题