判断两线段相交,并求交点

直线段交点检测算法

原文
http://blog.youkuaiyun.com/rickliuxiao/article/details/6259322

#include "math.h"
#include "stdio.h"

double E = 0.0001;

struct Point  
{   
    double x, y;  
};   

double min(double x1, double x2)
{
    return x1 < x2 ? x1 : x2;
}

double max(double x1, double x2)
{
    return x1 > x2 ? x1 : x2;
}

bool between(double X, double X1, double X2)  
{  
    return X >= min(X1, X2) && X <= max(X1, X2);
}  


// 判断两条直线段是否有交点,有则计算交点的坐标  
// p1,p2是直线一的端点坐标  
// p3,p4是直线二的端点坐标  
// line_x, line_y; //交点
bool detectIntersect(Point p1, Point p2, Point p3, Point p4, double &line_x, double &line_y)  
{  
    if ( (fabs(p1.x-p2.x)<E) && (fabs(p3.x-p4.x)<E) )  
    {  
        return false;  
    }  
    else if ( (fabs(p1.x-p2.x)<E) ) //如果直线段p1p2垂直与y轴  
    {  
        if (between(p1.x, p3.x, p4.x))  
        {  
            double k = (p4.y-p3.y)/(p4.x-p3.x);  
            line_x = p1.x;  
            line_y = k*(line_x-p3.x) + p3.y;  

            if (between(line_y,p1.y,p2.y))  
            {  
                return true;  
            }  
            else  
            {  
                return false;  
            }  
        }  
        else   
        {  
            return false;  
        }  
    }  
    else if ( (fabs(p3.x-p4.x)<E) ) //如果直线段p3p4垂直于y轴  
    {  
        if (between(p3.x,p1.x,p2.x))  
        {  
            double k = (p2.y-p1.y)/(p2.x-p1.x);  
            line_x = p3.x;  
            line_y = k*(line_x-p2.x) + p2.y;  

            if (between(line_y, p3.y, p4.y))  
            {  
                return true;  
            }  
            else  
            {  
                return false;  
            }  
        }  
        else   
        {  
            return true;  
        }  
    }  
    else  
    {  
        double k1 = (p2.y-p1.y)/(p2.x-p1.x);   
        double k2 = (p4.y-p3.y)/(p4.x-p3.x);  

        if (fabs(k1-k2) < E)  
        {  
            return false;  
        }  
        else   
        {  
            line_x = ((p3.y - p1.y) - (k2*p3.x - k1*p1.x)) / (k1 - k2);  
            line_y = k1*(line_x-p1.x) + p1.y;  
        }  

        if (between(line_x, p1.x, p2.x) && between(line_x, p3.x, p4.x))  
        {  
            return true;  
        }  
        else   
        {  
            return false;  
        }  
    }  
}  

int main()
{
    Point p1, p2, p3, p4;  
    p1.x = 5.0;  
    p1.y = 0.0;  
    p2.x = 0.0;  
    p2.y = 5.0;  
    p3.x = 0.0;  
    p3.y = 0.0;  
    p4.x = 5.0;  
    p4.y = 5.0;  

    double x, y;
    int ok = detectIntersect(p1, p2, p3, p4, x, y); 
    if(ok)
    {
        printf("相交,交点为:(%.3f, %.3f)\n", x, y);
    }
    else
    {
        printf("不相交\n");
    }
    return 0;
}

这里写图片描述

在CAD软件中,判断两条线是否相交返回交点通常涉及几何计算。在C#中,这通常需要使用CAD软件提供的API或者SDK来实现。以AutoCAD为例,你可以使用AutoCAD的.NET API来进行这样的判断和计算。以下是一个简化的代码示例,展示了如何使用AutoCAD的.NET API来判断两条直线是否相交获取交点: ```csharp using Autodesk.AutoCAD.Runtime; using Autodesk.AutoCAD.ApplicationServices; using Autodesk.AutoCAD.DatabaseServices; using Autodesk.AutoCAD.Geometry; // 这是一个AutoCAD命令的示例 public class IntersectLineCommands { // 该方法用于判断两条直线是否相交 [CommandMethod("IntersectLines")] public void IntersectTwoLines() { Document acDoc = Application.DocumentManager.MdiActiveDocument; Database acCurDb = acDoc.Database; // 开始一个事务 using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction()) { // 打开块表记录用于读写 BlockTable acBlkTbl; acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable; // 打开块表记录模型空间用于写入 BlockTableRecord acBlkTblRec; acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord; // 创建两条直线作为示例 Line acLine1 = new Line(new Point3d(0, 0, 0), new Point3d(10, 10, 0)); Line acLine2 = new Line(new Point3d(0, 10, 0), new Point3d(10, 0, 0)); // 将新对象添加到块表记录和事务 acBlkTblRec.AppendEntity(acLine1); acTrans.AddNewlyCreatedDBObject(acLine1, true); acBlkTblRec.AppendEntity(acLine2); acTrans.AddNewlyCreatedDBObject(acLine2, true); // 判断两条线是否相交 Point3dCollection intersectionPoints = new Point3dCollection(); acLine1.IntersectWith(acLine2, Intersect.OnBothOperands, intersectionPoints, IntPtr.Zero, IntPtr.Zero); if (intersectionPoints.Count > 0) { // 如果有交点,则输出交点坐标 acDoc.Editor.WriteMessage("交点坐标: " + intersectionPoints[0].ToString() + "\n"); } else { // 如果没有交点,则输出提示信息 acDoc.Editor.WriteMessage("两条线相交。\n"); } // 提交事务 acTrans.Commit(); } } } ``` 需要注意的是,上述代码仅作为示例,未考虑所有可能的异常处理和用户交互逻辑。在实际使用中,你需要根据实际CAD软件的具体API文档来编写代码,确保处理好所有可能的异常情况。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值