Thread: Angle/Slope of Tangent to a curve vb.net

1. Angle/Slope of Tangent to a curve vb.net

Registered forum members do not see this ad.

Is anyone able to help

In vb.net how do i find the angle of the tangent line to a curve at a given point relative to the X axis?

Also would this have to be quadrant sensitive?

2. Don't know vb.net, but the function/expression you need is the "arc tangent", so look for something like that. In lisp it's "atan". And yes it's quadrant sensitive, positive tangent would give you a 0-90 degree result, negative is for a 90-180 degrees.

**edit** found this, might help:

This example uses the Atan method of the Math class to calculate the value of pi.

Code:
```Public Function GetPi() As Double
' Calculate the value of pi.
Return 4.0 * Math.Atan(1.0)
End Function```

3. The general methodology for 3d curves would be:

1. Select the curve
2. Pick a point(Point3d) on the curve with (DatabaseServices.Curve.GetClosestPointTo)
3. Get first derivative(Geometry.Vector3d) with(DatabaseServices.Curve.GetFirstDerivative (Geometry.Point3d))
4. Get angle with System.Math.Atan(Geometry.Vector3d.Y/Geometry.Vector3d.X)

4. ahh thank you very much

5. so how do i calculate what quadrant it is in so that i can calculate the 'Normal to the tangent'.?

6. Tan is positive for quadrants 1 and 3 and negative for quadrants 2 and 4, hence a positive result indicates a vector at 0-pi/2 to the x-axis, plus or minus pi; and negative pi/2 - pi, plus or minus pi.

I suppose you could add coding to correct the angle by pi radians so that you will only get an answer between 0 and pi, then the normal to the tangent will just be the arctan + pi/2.

Is this a purely 2D problem, or are you interested in the Binormal to the tangent also?

7. No its purely 2d.

Thanks for that - i changed the code around so it will work now.

This is what i have.

Code:
```Public Function CalcAngle(ByVal ObjCurve As AcadNetDbServices.Curve, ByVal Dist As Double)

Dim Radians As Double
Dim Degrees As Double
Dim Point1 As AcadNetGeometry.Point3d = ObjCurve.GetPointAtDist(Dist)
Dim firstderiv As AcadNetGeometry.Vector3d = ObjCurve.GetFirstDerivative(Point1)
Radians = Math.Atan2(Cdbl(firstderiv.Y) , Cdbl(firstderiv.X))
Degrees = Radians * (180 / Math.PI)

'If in positive quadrants then = 180 - abs.Degrees
'If in Negative quadrants then = Abs.Degrees - 180
If Degrees < 0 Then
Degrees = (Math.Abs(Degrees)) + 180
Else
Degrees = 180 - (Math.Abs(Degrees))
End If
CalcAngle = Degrees

End Function
```

8. Not sure why you would set Degrees = 180 - angle for any positive result. Say it was initially calculated at 45 degrees, wouldn't that be a good answer already? But then I don't know what you're using it for...

9. I don't know much about vb.net (in fact I know absolutely nothing about it), but when you find the first derivative, does that not return a vector? - Then you would know exactly the direction of the tangent from the vector.

10. Registered forum members do not see this ad.

derivative gives the slope of the tangent.

using atan2 gets the arc tangent to it.

also i had to use Degrees = 180 - Angle in the positive quadrant because i was getting answers like 175 & 115 when it should be 5 & 65 etc

Thanks for your help guys.

This works

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts