C# CAD2016 多边形顶点按方向重新排序

  多边形顶点按方向重新排序

  1. 初始化多边形顶点集合 outerPoints

    • 创建一个名为 outerPoints 的 List<Point2d>,用于存储多边形的所有顶点坐标。
  2. 计算多边形顶点集合的边界框(BoundingBox)

    • 使用LINQ的Aggregate方法遍历整个outerPoints列表,并逐个更新最小X、最大X、最小Y和最大Y值。
    • 初始化匿名对象,其中包含四个属性:MinX、MaxX、MinY、MaxY,分别设置为double的最大值和最小值。
    • 对于列表中的每个顶点,将当前顶点的X和Y坐标与匿名对象中对应的最小值或最大值进行比较并更新。
  3. 找到左上角(候选西北角)

    • 根据计算得到的边界框信息,创建一个新的Point2d对象topLeftCorner,其坐标分别为边界框的最小X值和最大Y值。这个点位于多边形顶点集合的左上角,作为候选的西北方向起始点。
  4. 查找最接近左上角(西北方向)的顶点索引

    • outerPoints中的所有顶点按照它们到左上角顶点的距离平方进行排序,距离越近排在越前面。
    • 调用IndexOf方法找出排序后序列的第一个元素(即最接近左上角的顶点)在原列表中的索引位置,赋值给变量startVertexIndex

这样,通过以上步骤,我们找到了一个多边形的一个候选起始顶点(可能位于西北方向),并且已经获取了该多边形的边界框信息,这些信息对于后续判断顶点排列顺序及处理其他相关问题具有重要意义。

示例代码一 

假设您已经有了一个outerPolyline对象(或类似的多边形表示方式),其中包含了多边形的顶点。以下是一个完整的示例代码片段:

 

using System;
using System.Collections.Generic;
using Autodesk.AutoCAD.Geometry; // 假设使用AutoCAD Geometry库

// 初始化多边形顶点集合
List<Point2d> outerPoints = new List<Point2d>();
for (int i = 0; i < outerPolyline.NumberOfVertices; i++)
{
    Point2d point = outerPolyline.GetPoint2dAt(i);
    outerPoints.Add(point);
}

// 计算多边形顶点集合的边界框
var boundingBox = outerPoints.Aggregate(
    new { MinX = double.MaxValue, MaxX = double.MinValue, MinY = double.MaxValue, MaxY = double.MinValue },
    (a, b) =>
    {
        return new
        {
            MinX = Math.Min(a.MinX, b.X),
            MaxX = Math.Max(a.MaxX, b.X),
            MinY = Math.Min(a.MinY, b.Y),
            MaxY = Math.Max(a.MaxY, b.Y)
        };
    });

// 找到左上角的顶点作为候选西北角
Point2d topLeftCorner = new Point2d(boundingBox.MinX, boundingBox.MaxY);

// 找到最接近左上角(西北方向)的顶点索引
int startVertexIndex = outerPoints.IndexOf(outerPoints.OrderBy(p => Math.Pow(p.X - topLeftCorner.X, 2) + Math.Pow(p.Y - topLeftCorner.Y, 2)).First());

// 示例:打印出找到的起始顶点和边界框信息
Console.WriteLine($"西北角候选顶点坐标: ({topLeftCorner.X}, {topLeftCorner.Y})");
Console.WriteLine($"最接近西北角的顶点索引: {startVertexIndex}");
Console.WriteLine($"多边形边界框信息: 最小X={boundingBox.MinX}, 最大X={boundingBox.MaxX}, 最小Y={boundingBox.MinY}, 最大Y={boundingBox.MaxY}");

示例代码二

完整地处理八个方向(东北、东南、西南、西北、北、南、东、西),我们可以创建一个方法来获取多边形相对于给定点的最近顶点及其对应的方向。以下是一个示例代码:

using System;
using System.Collections.Generic;
using Autodesk.AutoCAD.Geometry; // 假设使用AutoCAD Geometry库

public enum Direction
{
    North,
    South,
    East,
    West,
    Northeast,
    Northwest,
    Southeast,
    Southwest
}

public static (Point2d, Direction) GetNearestVertexAndDirection(Point2d referencePoint, List<Point2d> polygonVertices)
{
    double minDistanceSquared = double.MaxValue;
    Point2d nearestVertex = new Point2d();
    Direction direction = Direction.North;

    foreach (var vertex in polygonVertices)
    {
        double dx = vertex.X - referencePoint.X;
        double dy = vertex.Y - referencePoint.Y;
        double distanceSquared = dx * dx + dy * dy;

        if (distanceSquared < minDistanceSquared)
        {
            minDistanceSquared = distanceSquared;
            nearestVertex = vertex;

            // 根据坐标判断方向
            if (dx == 0 && dy > 0) direction = Direction.South;
            else if (dx == 0 && dy < 0) direction = Direction.North;
            else if (dy == 0 && dx > 0) direction = Direction.West;
            else if (dy == 0 && dx < 0) direction = Direction.East;
            else if (dx > 0 && dy > 0) direction = Direction.Northeast;
            else if (dx < 0 && dy > 0) direction = Direction.Southeast;
            else if (dx < 0 && dy < 0) direction = Direction.Southwest;
            else if (dx > 0 && dy < 0) direction = Direction.Northwest;
        }
    }

    return (nearestVertex, direction);
}

// 使用示例
List<Point2d> outerPoints = ... // 初始化或从外部获取多边形顶点集合
Point2d referencePoint = new Point2d(10.0, 10.0); // 假设参考点坐标为(10, 10)

var (nearestVertex, direction) = GetNearestVertexAndDirection(referencePoint, outerPoints);
Console.WriteLine($"最近顶点坐标: ({nearestVertex.X}, {nearestVertex.Y})");
Console.WriteLine($"最近顶点方向: {direction}");

//感谢大家的点赞,收藏,转发,关注 
//附送AI 图片无版权 随意用 龙年大吉大利
通义万相        阿里最新推出的A绘画创作模型

  • 16
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C# CAD二次开发中,获取多边形的角度可以使用尺寸标注类中的转角标注(rotateddimension)。通过选择多边形的边界线,然后使用代码中的GetSelection方法获取选中的尺寸标注对象。在循环中,可以通过RotatedDimension对象的Measurement属性获取每个角度的数值。以下是一个获取多边形角度的示例代码: ``` public void GetPolygonAngles() { Database db = HostApplicationServices.WorkingDatabase; Document doc = Application.DocumentManager.MdiActiveDocument; Editor ed = doc.Editor; using (Transaction trans = db.TransactionManager.StartTransaction()) { var selectedDimensions = from dim in db.GetSelection<RotatedDimension>() where dim.ColorIndex >= 0 select dim; foreach (RotatedDimension dimension in selectedDimensions) { double angle = dimension.Measurement; Application.ShowAlertDialog("角度: " + angle.ToString()); } trans.Commit(); } } ``` 这段代码将获取选中的转角标注对象,并循环输出每个转角标注的角度。请注意,你需要在你的项目中引用相应的命名空间和CAD开发库。123 #### 引用[.reference_title] - *1* *2* [cad二次开发c#学习记录4——导出图纸标注的尺寸](https://blog.csdn.net/weixin_48897477/article/details/127777400)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}} ] [.reference_item] - *3* [cad二次开发C#学习记录5——导出属性块信息](https://blog.csdn.net/weixin_48897477/article/details/128601490)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值