[VBA/C# CE] Calculate Intersection Points between two Lines in term of XY with VBA COM

This code was written few years ago, and nowaday it is still found very useful to VBA/C# because Bentley provides no function for that.

Function GetIntersectionPoints can return you a intersection point but when you rotate the view, the output will be changed. (unless you know how to fix the Matrix3d)

Function Ray3dRay3dIntersectXY will return you a Boolean, but from the VBA Help it is hard for us to understand how to get out the intersection point.

So I copied the code to here for easy reference. (not sure if there any bug for you guys)

GetIntersectionPoints

      public static Point3d GetLineIntersectXY1(Point3d Line1_Point1, Point3d Line1_Point2, Point3d Line2_Point1, Point3d Line2_Point2)
        {
            //Equation of Line : y = mx + c
            Application app = Bentley.MicroStation.InteropServices.Utilities.ComApp;
            Point3d result = app.Point3dFromXYZ(0, 0, 0);
            double m1 = 0;
            double m2 = 0;
            double c1 = 0;
            double c2 = 0;
            double Temp = 0;
            
            if ((Line1_Point2.X - Line1_Point1.X != 0) && (Line2_Point2.X - Line2_Point1.X != 0))
            {
                m1 = (Line1_Point2.Y - Line1_Point1.Y) / (Line1_Point2.X - Line1_Point1.X);
                m2 = (Line2_Point2.Y - Line2_Point1.Y) / (Line2_Point2.X - Line2_Point1.X);
                c1 = Line1_Point1.Y - (m1 * Line1_Point1.X);
                c2 = Line2_Point1.Y - (m2 * Line2_Point1.X);

                if (0 == m2) //Cannot divide a value by zero in Mathematics
                {
                    Temp = m1;
                    m1 = m2;
                    m2 = Temp;
                    Temp = c1;
                    c1 = c2;
                    c2 = Temp;
                }

                result.X = (c1 - c2) / (m2 - m1);
                result.Y = (m1 * c2 / m2 - c1) / (m1 / m2 - 1);
                result.Z = 0;
            }
            else if ((Line1_Point2.X - Line1_Point1.X == 0) && (Line2_Point2.X - Line2_Point1.X != 0))
            {
                m2 = (Line2_Point2.Y - Line2_Point1.Y) / (Line2_Point2.X - Line2_Point1.X);
                c2 = Line2_Point1.Y - (m2 * Line2_Point1.X);

                result.X = Line1_Point1.X;
                result.Y = (m2 * Line1_Point1.X) + c2;
                result.Z = 0;
            }
            else if ((Line1_Point2.X - Line1_Point1.X != 0) && (Line2_Point2.X - Line2_Point1.X == 0))
            {
                m1 = (Line1_Point2.Y - Line1_Point1.Y) / (Line1_Point2.X - Line1_Point1.X);
                c1 = Line1_Point1.Y - (m1 * Line1_Point1.X);

                result.X = Line2_Point1.X;
                result.Y = (m1 * Line2_Point1.X) + c1;
                result.Z = 0;
            }
            else
            {
                //System.Windows.Forms.MessageBox.Show("Error!");
            }
            return result;
        }

        public static Point3d GetLineIntersectXY2(bool IsVertical, double X, double m1, double c1, double m2 = 0, double c2 = 0)
        {
            //y = mx + c
            Application app = Bentley.MicroStation.InteropServices.Utilities.ComApp;
            Point3d result = app.Point3dFromXYZ(0, 0, 0);
            if (IsVertical == false)
            {
                if ((m2 - m1 != 0) && (m2 - c1 != 0) && (m1 / m2 - 1 != 0))
                {
                    result.X = (c1 - c2) / (m2 - m1);
                    if (m2 != 0)
                    {
                        result.Y = ((m1 * c2 / m2) - c1) / ((m1 / m2) - 1);
                    }
                    else
                    {
                        //  y = (c2 - (m2 / m1 * c1)) / (1 - (m2 / m1));
                        result.Y = c2; 
                    }
                }
            }
            else //Intersecting with a vertical line
            {
                result.X = X;
                result.Y = (m1 * X) + c1;
            }
            return result;
        }