Sin and Cosine functions are straightforward to graph in autocad vba. The Tan, Cot, Csc, and Sec functions present a challenge with their asymptotes and holes in the graph, values for x where y is undefined. the following code is a first (incomplete) draft for these 4 functions.

Sub draw_trig() Call init_scale Dim a As Double, b As Double Dim fnx As String a = frm_trig1.txt_a1.Value b = frm_trig1.txt_b1.Value fnx = frm_trig1.cbo_sin.Value Select Case fnx Case "SIN" Call fnf_sin(a, b) Case "COS" Call fnf_cos(a, b) Case "TAN" Call fnf_tan(a, b) Case "COT" Call fnf_cot(a, b) Case "SEC" Call fnf_sec(a, b) Case "CSC" Call fnf_csc(a, b) End Select End Sub Sub fnf_sin(a As Double, b As Double) 'Xmin, Xmax, X_inc already set 'Y = a * Sin(b * X) Dim X As Double, Y As Double Dim plineobj As AcadLWPolyline Dim pt() As Double Dim i As Integer, numpts As Integer numpts = (Xmax - Xmin) / X_inc numpts = numpts + 1 ReDim pt(1 To numpts * 2) For i = 1 To numpts X = Xmin + ((i - 1) * X_inc) Y = a * Sin(b * X) pt(i * 2 - 1) = X: pt(i * 2) = Y Next i Set plineobj = acadDoc.ModelSpace.AddLightWeightPolyline(pt) Update strLabel = "Y= " & a & "* Sin " & b & "X" End Sub Sub fnf_cos(a As Double, b As Double) 'Xmin, Xmax, X_inc already set 'Y = a * Cos(b * X) Dim X As Double, Y As Double Dim plineobj As AcadLWPolyline Dim pt() As Double Dim i As Integer, numpts As Integer numpts = (Xmax - Xmin) / X_inc numpts = numpts + 1 ReDim pt(1 To numpts * 2) For i = 1 To numpts X = Xmin + ((i - 1) * X_inc) Y = a * Cos(b * X) pt(i * 2 - 1) = X: pt(i * 2) = Y Next i Set plineobj = acadDoc.ModelSpace.AddLightWeightPolyline(pt) Update strLabel = "Y= " & a & "* Cos " & b & "X" End Sub Sub fnf_tan(a As Double, b As Double) 'Xmin, Xmax, X_inc already set 'Y = a * Tan(b * X) Dim X As Double, Y As Double Dim plineobj As AcadLWPolyline Dim pt() As Double Dim i As Integer, numpts As Integer numpts = (Xmax - Xmin) / X_inc numpts = numpts + 1 ReDim pt(1 To numpts * 2) For i = 1 To numpts X = Xmin + ((i - 1) * X_inc) Y = a * Tan(b * X) pt(i * 2 - 1) = X: pt(i * 2) = Y Next i Set plineobj = acadDoc.ModelSpace.AddLightWeightPolyline(pt) Update strLabel = "Y= " & a & "* Tan " & b & "X" End Sub Sub fnf_cot(a As Double, b As Double) 'Xmin, Xmax, X_inc already set 'Y = a * (1 / Tan(b * X)) Dim X As Double, Y As Double Dim plineobj As AcadLWPolyline Dim pt() As Double Dim i As Integer, numpts As Integer numpts = (Xmax - Xmin) / X_inc numpts = numpts + 1 ReDim pt(1 To numpts * 2) For i = 1 To numpts X = Xmin + ((i - 1) * X_inc) Y = a * (1 / Tan(b * X)) pt(i * 2 - 1) = X: pt(i * 2) = Y Next i Set plineobj = acadDoc.ModelSpace.AddLightWeightPolyline(pt) Update strLabel = "Y= " & a & "* Cot " & b & "X" End Sub Sub fnf_sec(a As Double, b As Double) 'Xmin, Xmax, X_inc already set 'Y = a * (1 / Cos(b * X)) Dim X As Double, Y As Double Dim plineobj As AcadLWPolyline Dim pt() As Double Dim i As Integer, numpts As Integer numpts = (Xmax - Xmin) / X_inc numpts = numpts + 1 ReDim pt(1 To numpts * 2) For i = 1 To numpts X = Xmin + ((i - 1) * X_inc) Y = a * (1 / (Cos(b * X))) pt(i * 2 - 1) = X: pt(i * 2) = Y Next i Set plineobj = acadDoc.ModelSpace.AddLightWeightPolyline(pt) Update strLabel = "Y= " & a & "* Sec " & b & "X" End Sub Sub fnf_csc(a As Double, b As Double) 'Xmin, Xmax, X_inc already set 'Y = a * (1 / Sin(b * X)) Dim X As Double, Y As Double Dim plineobj As AcadLWPolyline Dim pt() As Double Dim i As Integer, numpts As Integer numpts = (Xmax - Xmin) / X_inc numpts = numpts + 1 ReDim pt(1 To numpts * 2) For i = 1 To numpts X = Xmin + ((i - 1) * X_inc) Y = a * (1 / (Sin(b * X))) pt(i * 2 - 1) = X: pt(i * 2) = Y Next i Set plineobj = acadDoc.ModelSpace.AddLightWeightPolyline(pt) Update strLabel = "Y= " & a & "* Csc " & b & "X" End Sub

Advertisements