How to get y coordinate on line at x value (VBA)

How to get y coordinate on line at x value (VBA)

Anonymous
Not applicable
2,012 Views
6 Replies
Message 1 of 7

How to get y coordinate on line at x value (VBA)

Anonymous
Not applicable

How to get the Y-Value on the line when X= 50

Getpoint.jpg

0 Likes
Accepted solutions (1)
2,013 Views
6 Replies
Replies (6)
Message 2 of 7

Alfred.NESWADBA
Consultant
Consultant

Hi,

 

simple mathematics for gradient formulas >>>click<<<

 

- alfred -

------------------------------------------------------------------------------------
Alfred NESWADBA
ISH-Solutions GmbH / Ingenieur Studio HOLLAUS
www.ish-solutions.at ... blog.ish-solutions.at ... LinkedIn ... CDay 2025
------------------------------------------------------------------------------------

(not an Autodesk consultant)
Message 3 of 7

leeminardi
Mentor
Mentor

It’s not clear from your question but I will assume that you know the location of the end points of the line.  Let’s call them PA (point A) and PBPA and PB represent the x,y  or x,y,z coordinates of a point. The equation of a line used in CAD is:

P = PA + t (PBPA)   (1)

Where t is an independent variable.  If we know PA, PB and t we can determine where the location of a point on the line.  If t = 0 then P = PA. If t = 1 then P = PB. If t is greater than 0 and less than 1 then the point P is between PA and PB.  This parametric definition of a line is used because it will locate a point on a line regardless of the slope of the line.  The traditional equation of a line, y = mx + b, is undefined for vertical lines.

 

For your problem PA, PB and dx are known and you wish to determine dy.  I assume this is a 2D problem. Note that if PAx (the x coordinate of PA) = PBx then dy cannot be calculated as it could be any value up or down the line.

 

Equation (1) is in vector form.  It can be rewritten in scalar form as 2 (or 3) equations.

Px = PAx + t (PBx –PAx)   (2)

Py = PAy + t (PBy –PAy)   (3)

 

We can combine (2) and (3) and solve for Py to get:

Py = PAy + (Px – PAx)/(PBx – PAx)     (4)

 

Since we know dx we want to include that in the expression.  We know that:

Px = PAx  + dx   (5)

 

Substituting (5) into (4) yields:

Py = PAy + ((PAx  + dx) – PAx)/(PBx – PAx)     (6)

 

We know that:

dy = Py – PAy  (7)     

 

Substituting (6) into (7) yields:

dy = ((PAx  + dx) – PAx)/(PBx – PAx)   if PAx not equal to  PBx    (8)     

 line1.PNG

lee.minardi
Message 4 of 7

Anonymous
Not applicable

I have to calculate? > no problem!

 

? So there is nothing like:

 

with myline '(or polyline)

      coor.y = getpoint (where coor.x = 50)

end with

 

0 Likes
Message 5 of 7

Kent1Cooper
Consultant
Consultant

It's a pretty simple trigonometry problem -- Y divided by 50 is the tangent of the angle.  If you really mean Line [i.e. a Line entity, not a Polyline line segment or something], in simplest terms:

(vl-load-com)
(setq lin (car (entsel "\nSelect Line: ")) ang (angle (vlax-curve-getStartPoint lin) (vlax-curve-getEndPoint lin)) Y (* 50 (/ (sin ang) (cos ang))) )

 

That will give an answer whether the Line extends that far or not.  If the direction of the Line is into the 2nd or 4th quadrant rather than the 1st or 3rd, the answer will be negative.

Kent Cooper, AIA
Message 6 of 7

leeminardi
Mentor
Mentor

@Kent1CooperThe OP implied he was working with VBA not VLISP. It's been several years since I used AutoCAD's VBA (I cannot get it to install with AutoCAD 2019) and wasn't sure what VBA functions are available so I used only x and y coordinates.  Using a get-angle function is surely more concise but  a check if the line is vertical still needs to be performed.  E.g., your code gave an answer of  8.16562e+17 for a sample vertical line.

lee.minardi
Message 7 of 7

SEANT61
Advisor
Advisor
Accepted solution

Here's another way to go about it with VBA.  The method also takes into account the more complicated LWPoly entity.

 Basic code sample.  Refinement still required.

Sub CompX() 'Main sub

Dim entBaseLine As AcadXline
Dim ent As AcadEntity
Dim varpt(2) As Double
Dim varpt2(2) As Double
Dim dblOffset As Double
Dim varIntersect As Variant


    With ThisDrawing
        On Error Resume Next
        .Utility.GetEntity ent, varpt, "Select subject Line/Polyline: "
        If Err <> 0 Then
           .Utility.Prompt "Selection operation aborted!" & vbLf
           Exit Sub
        End If
        On Error GoTo 0
        If Not TypeOf ent Is AcadLine And Not TypeOf ent Is AcadLWPolyline Then
           .Utility.Prompt "Improper entity, operation aborted!" & vbLf
           Exit Sub
        End If
       
    
        dblOffset = .Utility.GetReal("Enter X offset amount: ")
        varpt(0) = dblOffset
        varpt2(0) = dblOffset
        varpt2(1) = 1#
        Set entBaseLine = .ModelSpace.AddXline(varpt, varpt2)
        varIntersect = ent.IntersectWith(entBaseLine, acExtendNone)
        entBaseLine.Delete
        .Utility.Prompt "Y value at chosen X is: " & varIntersect(1) & vbLf
    End With
    
End Sub

 

 


************************************************************
May your cursor always snap to the location intended.