当前位置: 首页 > news >正文

C# 截取两个点之间的线段,等距分割线

  //取线段上两点之间的沿线线段//line 线//startDist:距离线第一个点的起点位置//stopDist:距离线第一个点的终点位置public static List<double[]> lineSliceAlong(List<double[]> line, double startDist, double stopDist){double travelled = 0;double overshot = 0;int origCoordsLength = line.Count;List<double[]> slice = new List<double[]>();//计算for (int i = 0; i < line.Count; i++){//如果起始距离大于,起始点位于的线段,且是最后一段儿则停止。if (startDist >= travelled && i == line.Count - 1) break;//如果 起始点位于的line分段段小于线段长度,且不是line的第一个点else if (travelled > startDist && slice.Count == 0){overshot = startDist - travelled;if (overshot == 0){slice.Add(line[i]);return slice;}//将点沿着向量方向移动距离Vector3 point1 = new Vector3((float)line[i][0], (float)line[i][1], (float)line[i][2]);Vector3 point2 = new Vector3((float)line[i - 1][0], (float)line[i - 1][1], (float)line[i - 1][2]);Vector3 direction = Vector3.Normalize(point1 - point2);Vector3 moveP = point1 + direction * (float)overshot;slice.Add(new double[] { moveP.X, moveP.Y, moveP.Z });slice.Add(new double[] { moveP.X, moveP.Y, moveP.Z });}if (travelled >= stopDist){overshot = stopDist - travelled;if (overshot == 0){slice.Add(line[i]);return slice;}Vector3 point1 = new Vector3((float)line[i][0], (float)line[i][1], (float)line[i][2]);Vector3 point2 = new Vector3((float)line[i - 1][0], (float)line[i - 1][1], (float)line[i - 1][2]);Vector3 direction = Vector3.Normalize(point1 - point2);Vector3 moveP = point1 + direction * (float)overshot;slice.Add(new double[] { moveP.X, moveP.Y, moveP.Z });return slice;}//线段起点if (travelled >= startDist){slice.Add(line[i]);}if (i == line.Count - 1){return slice;}List<double[]> tempLine = new List<double[]> { line[i], line[i + 1] };travelled += LineLength(tempLine);}if (travelled < startDist && line.Count == origCoordsLength){throw new Exception("Start position is beyond line");}double[] last = line[line.Count - 1];var temp = new List<double[]> { last, last };return temp;}//等距分割线public static List<double[]> lineChunck(List<double[]> line, double segmentLength, bool onlySegementPoint = false){List<double[]> result = new List<double[]>();double lineLength = LineLength(line);if (segmentLength <= 0){throw new Exception("segmentLength must be greater than 0");}//如果线比分段短,则返回原始数据if (lineLength <= segmentLength) return line;double numberOfSegments = lineLength / segmentLength;//如果分段不是整数则加1if ((int)numberOfSegments != numberOfSegments){numberOfSegments = Math.Floor(numberOfSegments) + 1;}for (int i = 0; i < numberOfSegments; i++){var outLine = lineSliceAlong(line, segmentLength * i, segmentLength * (i + 1));if (onlySegementPoint){result.Add(outLine.First());result.Add(outLine.Last());}else{for (int j = 0; j < outLine.Count; j++){var v = outLine[j];result.Add(v);}}}return distinctPoints(result);}//点去重public static List<double[]> distinctPoints(List<double[]> triplets){return triplets.Distinct(new TripletComparer()).ToList();}private class TripletComparer : IEqualityComparer<double[]>{public bool Equals(double[] x, double[] y){if (x.Length != y.Length) return false;for (int i = 0; i < x.Length; i++){if (x[i] != y[i]) return false;}return true;}public int GetHashCode(double[] obj){unchecked // Overflow is fine, just wrap{int hash = 17;foreach (var val in obj){hash = hash * 23 + val.GetHashCode();}return hash;}}}
http://www.lryc.cn/news/423678.html

相关文章:

  • 打造聊天流式回复效果:Spring Boot+WebSocket + JS实战
  • 202年版最新Python下载安装+PyCharm下载安装激活和使用教程!(附安装包+激活码)
  • 【面试宝典】spring常见面试题总结[上]
  • NC单链表的排序
  • 阿里云部署open-webui实现openai代理服务(持续更新)
  • Vue3简介和快速体验
  • LeetCode98 验证二叉搜索树
  • llama的神经网络结构;llama的神经网络结构中没有MLP吗;nanogpt的神经网络结构;残差是什么;残差连接:主要梯度消失
  • 函数的常量引用入参const saclass sdf,可否传入一个指向saclass对象的指针 shared_ptr<saclass>
  • 数据库:SQL——数据库操作的核心语言
  • Unity + HybridCLR 从零开始
  • C++小总结
  • 从快到慢学习Git指令
  • 传奇游戏发布渠道
  • 如何有效阅读科研论文【方法论】
  • 【揭秘】层层加码,竟能加速渠道营销数字化?-eBest
  • 基于WAMP环境的简单用户登录系统实现(v3版)(持续迭代)
  • 大语言模型与多模态大模型loss计算
  • 线上研讨会 | CATIA助力AI提升汽车造型设计
  • Unity新输入系统 之 InputAction(输入配置文件最基本的单位)
  • 【3】MySQL的安装即启动
  • 变“金点子”为“好应用”,合合信息智能文档处理技术助力大学生探索AI创新边界
  • 央行重提P2P存量业务化解,非吸案开始翻旧账?
  • 8B 端侧小模型 | 能力全面对标GPT-4V!单图、多图、视频理解端侧三冠王,这个国产AI开源项目火爆全网
  • 汽车免拆诊断案例 | DAF(达富)汽油尾气处理液故障警示
  • 图论算法
  • 手抖跟饮食有关系吗?
  • 59. 螺旋矩阵 II
  • shiro注解不起作用:shiro进行权限校验时,@RequireRoles(“admin“)注解不起作用的解决方法
  • ZABBIX邮件监控发送信息