- 积分
- 30299
- 明经币
- 个
- 注册时间
- 2016-9-16
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
本帖最后由 fangmin723 于 2024-6-25 09:19 编辑
关键方法:
- /// <summary>
- /// 判断给定角度是否在匹配角度的容差范围内
- /// </summary>
- /// <param name="angle">给定角度</param>
- /// <param name="matchangle">匹配角度</param>
- /// <param name="tolerance">容差</param>
- /// <returns>在匹配角度的容差范围内返回:true,否则返回:false</returns>
- public static bool IsWithInTo(this double angle, double matchangle, double tolerance = 1e-8)
- {
- var minang = (matchangle - tolerance).AngTo0To360();
- var maxang = (matchangle + tolerance).AngTo0To360();
- return minang > maxang
- ? (minang < angle && angle <= 360) || (0 <= angle && angle < maxang)
- : (minang < angle && angle < maxang);
- }
- /// <summary>
- /// 判断给定角度在匹配的集合中是否存在容差范围内的匹配项
- /// </summary>
- /// <param name="angle">给定角度</param>
- /// <param name="angles">匹配的集合</param>
- /// <param name="tolerance">容差</param>
- /// <param name="matchedAngle">匹配项</param>
- /// <returns>存在返回:true,否则返回:false</returns>
- public static bool IsWithInSet(this double angle, List<double> angles, double tolerance, out double matchedAngle)
- {
- // 先对角度集合进行排序
- angles.Sort();
- // 用默认值表示未找到匹配项
- matchedAngle = double.NaN;
- // 检查相邻角度之间是否有重叠
- for (int i = 1; i < angles.Count; i++)
- {
- if (angles - angles[i - 1] < 2 * tolerance)
- {
- //Console.WriteLine("给定集合和容差范围存在重叠。");
- return false; // 存在重叠,直接返回
- }
- }
- // 没有重叠,检查ang是否在集合中的角度的容差范围内
- foreach (var ang in angles)
- {
- if (angle.IsWithInTo(ang, tolerance))
- {
- //Console.WriteLine($"角度{ang}在集合中的角度{angle}的容差范围内。");
- matchedAngle = ang; // 找到匹配的角度
- return true;
- }
- }
- //没有找到匹配的角度
- matchedAngle = double.NaN;
- return false;
- }
测试及辅助方法:
- /// <summary>
- /// 角度转换为[0,360]的角度值
- /// </summary>
- /// <param name="degree">角度值</param>
- /// <returns>[0,360]的角度值</returns>
- public static double AngTo0To360(this double degree)
- {
- if (degree == 0) return 0;
- else if (degree % 360 == 0) return 360;
- else
- {
- // 调整至0到360区间
- degree %= 360;
- return degree < 0 ? degree += 360 : degree;
- }
- }
- /// <summary>
- /// 测试命令Tt
- /// </summary>
- [CommandMethod(nameof(Tt), CommandFlags.Session | CommandFlags.UsePickSet)]
- public void Tt()
- {
- var db = HostApplicationServices.WorkingDatabase;
- db.TrStart((tr, ed) =>
- {
- var ppr = ed.GetPointEx("\n指定起始点");
- if (ppr.Status != PromptStatus.OK) return;
- var jigBls = new JigBls("\n指定结束点", ppr.Value,
- (jppr, entities) =>
- {
- var point = jppr.Value;
- var ang = ppr.Value.GetAngle(point);
- if (ang.IsWithInSet([0, 45, 90, 135, 180, 225, 270, 315], 15, out double matchang))
- {
- $"当前角度:{ang};匹配到:{matchang}".PrintLine();
- }
- //entities.Enqueue();
- });
- var jigPr = ed.Drag(jigBls);
- });
- }
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?注册
x
|