I'm learning calculus on my own pretty much, and even made a sample app on finding the easiest way to find a derivative of a polynomial. My program graphs the polynomial (which is a parabola), and the derivative (which is a straight line representing slope). When the line intersects the parabola, I have it draw green end points. But I want to be able to only draw the line within the 2 end points. Any ideas on how to do this? Here's my code:
Code:Option Explicit
Private Type Point_2D
X As Single
Y As Single
End Type
Private Center As Point_2D
Private X As Single, Y As Single
Private X2 As Single, Y2 As Single
Private Current_X As Single
Private Initial As Point_2D
Private Pos As Point_2D, Pos2 As Point_2D
Private Scalar As Point_2D
Private Sub Form_Activate()
'Easiest way to find the derivative of a polynomial:
'---------------------------------------------------
'A derivative is the slope of a tangent line
'
'Formula for finding a derivative of X^n: n(x^(n - 1))
'
'Example:
'
'd/dx(x^2)
'= 2(X^(2-1))
'= 2X^1
'= 2X
'So the derivative of X^2 is 2X.
ScaleMode = 3
AutoRedraw = True
BackColor = RGB(0, 0, 0)
Center.X = ScaleWidth / 2
Center.Y = ScaleHeight / 2
Initial.X = Center.X
Initial.Y = Center.Y
Scalar.X = 25
Scalar.Y = 5
For Current_X = -100 To 100 Step 0.01
X = Current_X
Y = X ^ 2
Pos.X = Initial.X + X * Scalar.X
Pos.Y = Initial.Y - Y * Scalar.Y
PSet (Pos.X, Pos.Y), RGB(255, 0, 0)
'------------------------------------
X2 = Current_X
Y2 = 2 * X2
Pos2.X = Initial.X + X2 * Scalar.X
Pos2.Y = Initial.Y - Y2 * Scalar.Y
If Int(Pos.X) = Int(Pos2.X) And Int(Pos.Y) = Int(Pos2.Y) Then
'Draw end points for secant line.
PSet (Pos2.X, Pos2.Y), RGB(0, 255, 0)
Else
PSet (Pos2.X, Pos2.Y), RGB(255, 0, 0)
End If
Next Current_X
End Sub
