
13/09/2004, 09:48
|
 | | | Fecha de Ingreso: diciembre-2001 Ubicación: Monterrey, Nuevo León
Mensajes: 433
Antigüedad: 23 años, 2 meses Puntos: 7 | |
Malexandrapc:
Hola. Supongo que el ejemplo con la API que te envié no te sirvió de mucho... Tenía unas horas libres el fin de semana, así que hice este programita, que tal vez te pueda servir. Grafica ecuaciones matemáticas, siempre que las pongas con la sintaxis que usarías en VB para programar dicha ecuación. Si te sientes más cómoda con JS, entonces, le cambias el modo al control que utiliza y usas la sintaxis de JS. El control es Microsoft Script Control (msscript.ocx). Yo prefiero incluirlo como referencia, pero si lo deseas simplemente inserta el control y modifica el código para que así funcione. Lo que tienes que hacer:
1.- En un nuevo proyecto, agrega una referencia a Microsoft Script Control 1.0, con el menú Proyecto/Referencias. Debe de estar en tu sistema, pero si no, descárgalo desde el sitio de Microsoft, es gratuito y es muy pequeño.
2.- En el formulario del proyecto, agrega:
i) Un Botón (command1)
ii) Una etiqueta (label1)
iii) Un cuadro de texto (Text1)
iv) Un Timer (Timer1) y
v) Un ComboBox (Combo1)
3.- Cambia la propiedad index de Label1 y Text1 a cero.
Y ahora, copia y pega el siguiente código.
Código:
Option Explicit
Private Enum EnumErrorSolve
ESOK
ESInfinit
ESArgInvalid
ESOverFlow
End Enum
Private Const ErrorSolve As Long = &HFFFFFFF
Private Const FormPad As Long = 10
Private m_lXValReal As Double
Private m_lYValReal As Double
Private m_lXOrigin As Long
Private m_lYOrigin As Long
Private m_lArrTxtVal(0 To 13) As String
Private m_bAllowChange As Boolean
Private Sub Combo1_Click()
Dim sValues As Variant
Dim i As Long
m_bAllowChange = False
sValues = Split(m_lArrTxtVal(Combo1.ListIndex), ",")
'Combo1.Text = "Combo1"
For i = 0 To 5
Text1(i).Text = sValues(i)
Next i
m_bAllowChange = True
Timer1.Enabled = True
End Sub
Private Sub Command1_Click()
Dim i As Long
Dim j As Long
Dim lCurrentY As Long
Dim lErResult As EnumErrorSolve
Dim sEquations() As String
Dim lColor As Variant
'Por si se hemos olvidado contemplar algo
On Error Resume Next
'Si no hay nada que hacer...
If Not Combo1.Text <> "" Then Exit Sub
'Se prodrían hacer verificaciones más complejas, para
'asegurarnos de que realmente se tiene una(s) funcion(es)
'pero eso se quedará de tarea...
'Usaremos un color aleatorio (de entre la siguiente lista)
lColor = Array(&HC0C0&, vbRed, &HC000&, vbBlue, vbMagenta, &HC0C000)
Randomize (Timer)
Me.Cls
'Separamos las ecuaciones
sEquations = Split(Combo1.Text, ";")
For j = LBound(sEquations) To UBound(sEquations)
Me.ForeColor = lColor(Int(Rnd() * 7))
For i = 0 To Me.ScaleWidth
'Solicitamos f(x)
lErResult = Solve(sEquations(j), CDbl(i - m_lXOrigin) * m_lXValReal, lCurrentY)
If lErResult = ESOK Then 'No hubo errores
If i = 0 Then
Me.CurrentX = 0
Me.CurrentY = lCurrentY
Else
Me.Line -(i, lCurrentY)
End If
Else 'Si la expresión causa un error
Do
If i <= Me.ScaleHeight Then
i = i + 1
Else
Exit Do
End If
Loop Until Solve(sEquations(j), CDbl(i - m_lXOrigin) * m_lXValReal, lCurrentY) = ESOK
Me.CurrentX = i
Me.CurrentY = lCurrentY
End If
Next i
Next j
End Sub
Private Sub Form_Load()
'Usamos pixels en lugar de twips
Me.ScaleMode = vbPixels
'Me.BackColor = vbWindowBackground
Me.Caption = "Hypatia"
'Maximizamos la ventana
Me.WindowState = vbMaximized
'Mostramos la ventana. Esto es para poder calcular las
'distancias de las lineas de la cuadrícula. Si no lo
'hacemos, los valores de las dimensiones del form,
'no reflejan el valor actual (maximizado), sino los valores del form
'en el diseño.
Me.Show
Timer1.Enabled = False
Timer1.Interval = 100
With Command1
.Left = 440: .Top = 8
.Height = 25: .Width = 120
.Caption = "Graficar Ecuación"
End With
With Text1(0)
.Alignment = vbRightJustify
.Width = 60: .Left = 80
.Height = 21: .Top = 50
.Text = "-10"
End With
Load Text1(1)
With Text1(1)
.Top = Text1(0).Top + Text1(0).Height + 4
.Text = -8: .Visible = True
End With
Load Text1(2)
With Text1(2)
.Top = Text1(1).Top + Text1(1).Height + 4
.Text = "1": .Visible = True
End With
Load Text1(3)
With Text1(3)
.Top = Text1(2).Top + Text1(2).Height + 4
.Text = "1": .Visible = True
End With
Load Text1(4)
With Text1(4)
.Top = Text1(3).Top + Text1(3).Height + 4
.Text = "20": .Visible = True
End With
Load Text1(5)
With Text1(5)
.Top = Text1(4).Top + Text1(4).Height + 4
.Text = "16": .Visible = True
End With
With Label1(0)
.AutoSize = True
.BackStyle = vbTransparent
.Alignment = vbRightJustify
.Top = 54
.Left = 76 - .Width
.Caption = "Inicio X:"
End With
Load Label1(1)
With Label1(1)
.Top = Text1(1).Top + 4
.Caption = "Inicio Y:"
.Visible = True
End With
Load Label1(2)
With Label1(2)
.Top = Text1(2).Top + 4
.Caption = "Escala X:"
.Visible = True
End With
Load Label1(3)
With Label1(3)
.Top = Text1(3).Top + 4
.Caption = "Escala Y:"
.Visible = True
End With
Load Label1(4)
With Label1(4)
.Top = Text1(4).Top + 4
.Caption = "Divisiones X:"
.Visible = True
End With
Load Label1(5)
With Label1(5)
.Top = Text1(5).Top + 4
.Caption = "Divisiones Y:"
.Visible = True
End With
LoadUnloadLabels
DrawGrid
'Estas son algunas ecuaciones de ejemplo, que tienen propiedades interesantes.
With Combo1
.Left = 8: .Top = 8
.Width = 420
.AddItem "X; 2 * X; 3 * X; X / 2; X / 3"
m_lArrTxtVal(0) = "-11,-8,1,1,22,16"
.AddItem "Sqr(x)"
m_lArrTxtVal(1) = "0,-5,250,5,20,16"
.AddItem "Log(x)"
m_lArrTxtVal(2) = "0,-4,50,1,20,13"
.AddItem "(2 * X) ^ 2"
m_lArrTxtVal(3) = "-11,0,1,10,22,16"
.AddItem "((2 * X) ^ 2) / (X - 1)"
m_lArrTxtVal(4) = "-9,-70,1,10,22,16"
.AddItem "(2 * (X ^ 3)) - X"
m_lArrTxtVal(5) = "-3,-1,0.3,0.1,20,20"
.AddItem "(X + 2) / ((X ^2) - (2 * X))"
m_lArrTxtVal(6) = "-3,-16,0.5,2,20,16"
.AddItem "(2 * X) / ((X + 3) ^ 2)"
m_lArrTxtVal(7) = "-8,-60,0.5,5,20,16"
.AddItem "((X ^ 2) - X + 1) / ((X ^ 2) + 1)"
m_lArrTxtVal(8) = "-32,0,4,0.2,16,10"
.AddItem "Abs(X * (X + 2) * (X - 2))"
m_lArrTxtVal(9) = "-4,-1,0.4,0.5,20,16"
.AddItem "Sin(x)"
m_lArrTxtVal(10) = "-7,-1,1,0.1,14,20"
.AddItem "Atn(x)"
m_lArrTxtVal(11) = "-10,-2,1,0.2,20,20"
.AddItem "Sqr(((4 * (X ^ 2)) + (8 * X) + 16) / 3); -Sqr(((4 * (X ^ 2)) + (8 * X) + 16) / 3); (2 * (X + 1)) / Sqr(3); -(2 * (X + 1)) / Sqr(3)"
m_lArrTxtVal(12) = "-26,-20,2,2,26,20"
.AddItem "Sqr(1 - (X ^ 2)); -Sqr(1 - (X ^ 2));Sqr((2/3) - (2 *(X ^ 2)) / 3); -Sqr((2/3) - (2 *(X ^ 2)) / 3)"
m_lArrTxtVal(13) = "-2,-1,0.1,0.1,30,20"
.Text = ""
End With
m_bAllowChange = True
End Sub
Abajo continúa el código... |