[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;
        }

Parents Reply Children
  • I am sorry that I confused you. There is nothing wrong.

    Last time the study case of learning apparent intersections were: when you switch to top / font / side views, the intersection points were the different answers. If you were designing a interactive dynamic drawing tool, that was a nightmare. There might be something wrong at that time and codes now works.

    I am sorry that you are confused.

  • Hi,

    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)

    I think think there is nothing "to be fixed". Anything in CAD geometry is based on vector / matrix operation, so to use rotation and transformation matrices is a standard part of CAD coding, regardless of used language or API.

    Anyway, just use it if you want to keep codes clean

    To use as much from API functionality as possible, and to do not duplicate what is available already, is the way to keep the code clean.

    Actually, there were only Point3d belong to VBA COM.

    That's the problem of VBA: It's very limited. Even though it offers quite complete set of vector and math operations, C++ and NET (C# or VB.NET) offer, as Jon wrote, much more. It does not mean the code is simpler ;-), but the abstraction is better, so the code, when implemented right, is more universal.

    Regards,

      Jan