70002792 I wish to implement a method to calculate closest point on a specific angle on a figure from a given point

Article 70002792
Type HowTo
Product Engine
Date Added 12/1/2025 12:00:00 AM
Fixed 11.5.03 (12/1/2025 12:00:00 AM)
Submitted by fla

Summary

I wish to implement a method to calculate closest point on a specific angle on a figure from a given point

Solution

The following code will ask the user to pick an entity and then a point and will create vdPoint objects from the calculated found points on 5 given angles (0 , 90 , 180 , 270 , 37 degrees)

                //Ask the user for a figure
                vdFigure fig = null;
                gPoint pt = null;
                StatusCode s1 = doc.ActionUtility.getUserEntityOneClick(out fig, out pt, vdDocument.LockLayerMethodEnum.Default, true);
                if (s1 == StatusCode.Success)
                {
                    //Ask the user for a point
                    gPoint SelectedPoint = null;
                    StatusCode s = doc.ActionUtility.getUserPoint(out SelectedPoint);
                    if (s == StatusCode.Success)
                    {
                        //90 degrees
                        gPoint result1 = getClosestPointOnAngle(fig, SelectedPoint, Globals.HALF_PI);
                        if (result1 != null)
                        {
                            vdPoint resultpoint = new vdPoint(doc, result1);
                            resultpoint.PenColor.ColorIndex = 0;
                            doc.ActiveLayOut.Entities.AddItem(resultpoint);
                        }
                        //180 degrees
                        gPoint result2 = getClosestPointOnAngle(fig, SelectedPoint, Globals.PI);
                        if (result2 != null)
                        {
                            vdPoint resultpoint = new vdPoint(doc, result2);
                            resultpoint.PenColor.ColorIndex = 1;
                            doc.ActiveLayOut.Entities.AddItem(resultpoint);
                        }
                        //270 degrees
                        gPoint result3 = getClosestPointOnAngle(fig, SelectedPoint, Globals.VD_270PI);
                        if (result3 != null)
                        {
                            vdPoint resultpoint = new vdPoint(doc, result3);
                            resultpoint.PenColor.ColorIndex = 2;
                            doc.ActiveLayOut.Entities.AddItem(resultpoint);
                        }
                        //360 - 0 degrees
                        gPoint result4 = getClosestPointOnAngle(fig, SelectedPoint, Globals.VD_TWOPI);
                        if (result4 != null)
                        {
                            vdPoint resultpoint = new vdPoint(doc, result4);
                            resultpoint.PenColor.ColorIndex = 3;
                            doc.ActiveLayOut.Entities.AddItem(resultpoint);
                        }
                        //37 degrees
                        gPoint result5 = getClosestPointOnAngle(fig, SelectedPoint, Globals.DegreesToRadians (37));
                        if (result5 != null)
                        {
                            vdPoint resultpoint = new vdPoint(doc, result5);
                            resultpoint.PenColor.ColorIndex = 4;
                            doc.ActiveLayOut.Entities.AddItem(resultpoint);
                        }
                        doc.Redraw(true);
                    }
                }


And the calculation method is the following
        summary Calculates and returns the closest point from a given point at a specific angle
        param name="figure" The figure to get the closest points from.
        param name="SelectedPoint" The selected point.
        param name="angle" The angle in radians.
        returns Null or a gPoint which is the closest point on the passed figure from the passed SelectedPoint at the specified angle.
        private gPoint getClosestPointOnAngle(vdFigure figure, gPoint SelectedPoint, double angle)
        {
            gPoint ret = null;
            Box bbox = figure.BoundingBox;
            double max = Math.Max(bbox.Width, bbox.Height);
            gPoint SecondPoint = SelectedPoint.Polar(angle, max * 2.0);
            vdLine tmpline = new vdLine(SelectedPoint, SecondPoint);

            gPoints intersections = new gPoints();
            if (figure.IntersectWith(tmpline, VdConstInters.VdIntOnBothOperands, intersections))
            {
                if (intersections.Count > 0)
                {
                    intersections.RemoveEqualPoints();
                    intersections.shortByPointOrigin(SelectedPoint);
                    return intersections[0];
                }
            }
            return ret;
        }

Send comments on this topic.