用C#代码实现求两条线段的交点并判断各种情况

RT,简单地几何学知识。这个函数只考虑了二维空间的求交。

GetInterBetweenTwoLines函数即为求交点函数。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GeometryTestConsole
{
    class Program
    {

        public struct DPoint3d
        {
            public double X, Y, Z;
            public DPoint3d(double x, double y, double z) { X = x; Y = y; Z = z; }
        }

        /// <summary>
        /// 求一个线段与另一个线段的交点
        /// </summary>
        /// <param name="line1S"></param>
        /// <param name="line1E"></param>
        /// <param name="line2S"></param>
        /// <param name="line2E"></param>
        /// <param name="interPoint"></param>
        /// <returns>-1:不存在交点;1:存在一个交点;2:存在无穷多个交点(重合或部分重合)</returns>
        private static int GetInterBetweenTwoLines(DPoint3d line1S, DPoint3d line1E,
            DPoint3d line2S, DPoint3d line2E, ref DPoint3d interPoint)
        {
            int status = -1;
            // 判断两条直线各自的矩形包围盒的X与Y的值范围
            double line1Xmin = line1S.X < line1E.X ? line1S.X : line1E.X,
                line1Xmax = line1S.X > line1E.X ? line1S.X : line1E.X,
                line1Ymin = line1S.Y < line1E.Y ? line1S.Y : line1E.Y,
                line1Ymax = line1S.Y > line1E.Y ? line1S.Y : line1E.Y,
                line2Xmin = line2S.X < line2E.X ? line2S.X : line2E.X,
                line2Xmax = line2S.X > line2E.X ? line2S.X : line2E.X,
                line2Ymin = line2S.Y < line2E.Y ? line2S.Y : line2E.Y,
                line2Ymax = line2S.Y > line2E.Y ? line2S.Y : line2E.Y;
            if (line1S.X - line1E.X == 0)
            {
                // 两条线都垂直于X轴
                if (line2S.X - line2E.X == 0)
                {
                    if (line1S.X != line2S.X) status = -1;
                    else if ((line1Ymin > line2Ymax) || (line1Ymax < line2Ymin)) status = -1;
                    else if (line1Ymin == line2Ymax)
                    {
                        interPoint = new DPoint3d(line1S.X, line1Ymin, line1S.Z); status = 1;
                    }
                    else if (line1Ymax == line2Ymin)
                    {
                        interPoint = new DPoint3d(line1S.X, line1Ymax, line1S.Z); status = 1;
                    }
                    else status = 2;
                }
                // line1垂直于X轴,line2不垂直于X轴
                else
                {
                    double slope = (line2S.Y - line2E.Y) / (line2S.X - line2E.X);
                    double offset = line2S.Y - slope * line2S.X;
                    double newX = line1S.X, newY = slope * newX + offset;
                    if (newX >= line2Xmin && newX <= line2Xmax && newY >= line1Ymin && newY <= line1Ymax
                        && newY >= line2Ymin && newY <= line2Ymax)
                    {
                        interPoint = new DPoint3d(newX, newY, line1S.Z); status = 1;
                    }
                }
            }
            else
            {
                // line1不垂直于X轴,line2垂直于X轴
                if (line2S.X - line2E.X == 0)
                {
                    double slope = (line1S.Y - line1E.Y) / (line1S.X - line1E.X);
                    double offset = line1S.Y - slope * line1S.X;
                    double newX = line2S.X, newY = slope * newX + offset;
                    if (newX >= line1Xmin && newX <= line1Xmax && newY >= line1Ymin && newY <= line1Ymax
                        && newY >= line2Ymin && newY <= line2Ymax)
                    {
                        interPoint = new DPoint3d(newX, newY, line2S.Z); status = 1;
                    }
                }
                // line1和line2都不垂直于X轴
                else
                {
                    double slope1 = (line1S.Y - line1E.Y) / (line1S.X - line1E.X);
                    double offset1 = line1S.Y - slope1 * line1S.X;
                    double slope2 = (line2S.Y - line2E.Y) / (line2S.X - line2E.X);
                    double offset2 = line2S.Y - slope2 * line2S.X;
                    // 如果两条直线平行
                    if (slope1 == slope2)
                    {
                        if (offset1 != offset2) status = -1;
                        else if (line1Xmax == line2Xmin)
                        {
                            interPoint = new DPoint3d(line1Xmax, line1Xmax * slope1 + offset1, line1S.Z); status = 1;
                        }
                        else if (line1Xmin == line2Xmax)
                        {
                            interPoint = new DPoint3d(line1Xmin, line1Xmin * slope1 + offset1, line1S.Z); status = 1;
                        }
                        else if (line1Xmax < line2Xmin || line1Xmin > line2Xmax) status = -1;
                        else status = 2;
                    }
                    else
                    {
                        double newX = (offset2 - offset1) / (slope1 - slope2), newY = newX * slope1 + offset1;
                        if (newX >= line1Xmin && newX <= line1Xmax && newX >= line2Xmin && newX <= line2Xmax)
                        {
                            interPoint = new DPoint3d(newX, newY, line1S.Z); status = 1;
                        }
                    }
                }
            }
            return status;
        }
        static void Main(string[] args)
        {
            DPoint3d point1S = new DPoint3d(1, 1, 0), point1E = new DPoint3d(3, 3, 0),
                point2S = new DPoint3d(4, 4, 0), point2E = new DPoint3d(5, 5, 0);
            DPoint3d interPoint = new DPoint3d();
            int type = GetInterBetweenTwoLines(point1S, point1E, point2S, point2E, ref interPoint);
        }
    }
}

 

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
可以使用以下的C#代码两条线段交点: ```csharp using System; class Program { static void Main(string[] args) { // 定义线段的起点和终点坐标 double x1 = 1, y1 = 1; double x2 = 4, y2 = 5; double x3 = 2, y3 = 3; double x4 = 5, y4 = 7; // 计算线段交点 double[] intersectionPoint = GetIntersectionPoint(x1, y1, x2, y2, x3, y3, x4, y4); // 输出交点坐标 Console.WriteLine("Intersection Point: ({0}, {1})", intersectionPoint[0], intersectionPoint[1]); Console.ReadLine(); } static double[] GetIntersectionPoint(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4) { double[] intersectionPoint = new double[2]; // 计算线段的参数 double dx1 = x2 - x1; double dy1 = y2 - y1; double dx2 = x4 - x3; double dy2 = y4 - y3; // 计算两条线段的斜率 double slope1 = dy1 / dx1; double slope2 = dy2 / dx2; // 计算两条线段的截距 double intercept1 = y1 - slope1 * x1; double intercept2 = y3 - slope2 * x3; // 若两条线段平行,则没有交点 if (slope1 == slope2) { Console.WriteLine("No intersection point"); return intersectionPoint; } // 计算交点的x坐标 double intersectionX = (intercept2 - intercept1) / (slope1 - slope2); // 计算交点的y坐标 double intersectionY = slope1 * intersectionX + intercept1; // 将交点坐标存储到数组中 intersectionPoint[0] = intersectionX; intersectionPoint[1] = intersectionY; return intersectionPoint; } } ``` 这段代码通过两条线段的斜率和截距,然后利用斜截式方程计算交点的坐标。如果两条线段平行,则没有交点。运行以上代码将输出交点的坐标。请根据实际情况修改线段的起点和终点坐标。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值