# Thread: Tangent to a Curve

1. ## Tangent to a Curve

this example can be used for demonstrations in the calculus lessons in the secondary schools.
this is the tangent and the normal

look for explanation:

to find a tangent is to find a derivative at that point (one of the Calculus lessons), the best place to find the derivative is wolfram alpha, such as to find the derivative of y = Sin(x) go to
http://www.wolframalpha.com/input/?i...tive+of+sin(x)
and it will give you Cos(x)
the derivative of x^2 is 2*x
look at the folowing curve and its tangent, at the newTurn of the curve it seems to slow down because there is a lot of points there (condensed).

i have used a box to represent the tangent, because i find tbgl_Cylinder rotate around one of its sides and not from its center, (useful for tree branches generation).
if i use tbgl_cylinder instead of the tbgl_box like this way:
```TBGL_Translate x,y,0
'TBGL_Rotate 0,0,slope
TBGL_Rotate slope,0,0
TBGL_Rotate -90,0,0
TBGL_Cylinder 0.1,0.1,1
```
then at the newTurn of the curve it will not stay like the classical tangent.

curve y = x^2 press Space to move the tangent over the curve
```Uses "TBGL", "Math"
Begin Const
%listPoints = 1
End Const

Dim FrameRate  As Double
Local hWnd As DWord

hWnd = TBGL_CreateWindowEx("curve tangent .... press 'Space' to move the Tangent line over the curve", 800, 600, 32, %TBGL_WS_WINDOWED Or %TBGL_WS_CLOSEBOX)
TBGL_ShowWindow

TBGL_BackColor(255,255,255)

TBGL_ResetKeyState()

Global  x,y As Single
' catch the data and store it to %listPoints
TBGL_NewList %listPoints
TBGL_Color(0,0,0)
TBGL_PointSize 3
For x = -2.5 To 2.5 Step 0.01
y = x^2 - 3  ' just to shift down the curve

TBGL_BeginPoly(%GL_POINTS)
TBGL_Color(150, 150, 255 )
TBGL_Vertex(x,y)
TBGL_EndPoly

Next

TBGL_EndList

TBGL_ResetKeyState()
x = -2.5: y = 0
Global slope, slopeAngle As Single

While TBGL_IsWindow(hWnd)

FrameRate = TBGL_GetFrameRate

TBGL_ClearFrame

TBGL_RenderMatrix2D( -5, -5, 5, 5 )

TBGL_CallList %listPoints

If TBGL_GetWindowKeyState( hWnd, %VK_SPACE) Then
x + 0.01
y = x^2 - 3 ' just to shift down the curve

slope = 2*x ' This is the derivative of f(y)=x^2
slopeAngle = RadToDeg(Atn(slope)) 'find the angle correspend to slope and convert it to degrees
'slopeAngle = (Atn(slope))*180/Pi 'find the angle correspend to slope and convert it to degrees
End If
TBGL_Translate x,y,0
TBGL_Rotate slopeAngle,0,0
TBGL_Color(255, 0, 0 )
TBGL_Box 1, 0.05, 0.2
TBGL_Rotate 180,0,0
TBGL_Cylinder 0.02,0.02,0.5

TBGL_DrawFrame

If TBGL_GetWindowKeyState(hWnd, %VK_ESCAPE) Then Exit While

Wend

TBGL_DestroyWindow
```
Curve y = Sin(x)
```Uses "TBGL", "Math"
Begin Const
%listPoints = 1
End Const

Dim FrameRate  As Double
' Handle for our window
Local hWnd As DWord

hWnd = TBGL_CreateWindowEx("curve tangent .... press 'Space' to move the Tangent line over the curve", 800, 600, 32, %TBGL_WS_WINDOWED Or %TBGL_WS_CLOSEBOX)
TBGL_ShowWindow

' Set background  to white
TBGL_BackColor(255,255,255)

TBGL_ResetKeyState()

Global  x,y As Single
' catch the data and store it to %listPoints
TBGL_NewList %listPoints
TBGL_Color(0,0,0)
TBGL_PointSize 3
For x = -5 To 5 Step 0.01
y = Sin(x)

TBGL_BeginPoly(%GL_POINTS)
TBGL_Color(150, 150, 255 )
TBGL_Vertex(x,y)
TBGL_EndPoly

Next

TBGL_EndList

TBGL_ResetKeyState()
x = -5: y = 0
Global slope, slopeAngle As Single

While TBGL_IsWindow(hWnd)

FrameRate = TBGL_GetFrameRate

TBGL_ClearFrame

TBGL_RenderMatrix2D( -5, -5, 5, 5 )

TBGL_CallList %listPoints

If TBGL_GetWindowKeyState( hWnd, %VK_SPACE) Then
x + 0.01
y = Sin(x)

slope = Cos(x) ' This is the derivative of f(y)=Sine(x)
slopeAngle = RadToDeg(Atn(slope)) 'find the angle correspend to slope and convert it to degrees
'slopeAngle = (Atn(slope))*180/Pi 'find the angle correspend to slope and convert it to degrees
End If
TBGL_Translate x,y,0
TBGL_Rotate slopeAngle,0,0
TBGL_Color(255, 0, 0 )
TBGL_Box 1, 0.05, 0.2
TBGL_Cylinder 0.02,0.02,0.5
'TBGL_Box 0.1, 1, 0.1

TBGL_DrawFrame

If TBGL_GetWindowKeyState(hWnd, %VK_ESCAPE) Then Exit While

Wend

TBGL_DestroyWindow
```

#### Posting Permissions

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