График линейного уравнения - Visual Basic .NET
Формулировка задачи:
Помогите пожалуйста Сделать работающий график а то этот график не правильный
График так должен выглядеть помагите пожалуйста очень надо
Решение задачи: «График линейного уравнения»
textual
Листинг программы
Public Class Form5 Private pp As fPlot2 Private isLoad As Boolean Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click Dim a1 As Double = CDbl(TextBox1.Text) Dim a2 As Double = CDbl(TextBox2.Text) Dim c1 As Double = CDbl(TextBox3.Text) Dim b1 As Double = CDbl(TextBox6.Text) Dim b2 As Double = CDbl(TextBox5.Text) Dim c2 As Double = CDbl(TextBox4.Text) Dim ff1 = Function(x) -a1 / a2 * x + c1 / a2 Dim ff2 = Function(x) -b1 / b2 * x + c2 / b2 Dim xx As Double = (c2 / b2 - c1 / a2) / (b1 / b2 - a1 / a2) Label6.Text = "x = " & xx.ToString Label7.Text = "y = " & ff1(xx).ToString pp = New fPlot2(ff1, ff2, -5, 5, 0.2) pp.isAxDivision = True pp.isGrid = True pp.divisionNumberToX = 10 pp.divisionNumberToY = 10 pp.isAxesLetter = True isLoad = True PictureBox1.Invalidate() End Sub Private Sub PictureBox1_Paint(sender As System.Object, e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint If Not isLoad Then Exit Sub pp.plot(PictureBox1.Width, PictureBox1.Height, e) End Sub End Class '… Public Class fPlot2 Private Xmn, Xmx, Ymn, Ymx, stX, divX, divY As Double Private xy1(), xy2() As PointF Private pnAxes, pnCurve1, pnCurve2, pnGrid As Pen Public Sub New(ff1 As Func(Of Double, Double), ff2 As Func(Of Double, Double), ByVal Xmin As Double, ByVal Xmax As Double, ByVal stepX As Double) Xmn = Xmin Xmx = Xmax stX = stepX ReDim xy1((Xmx - Xmn) / stX) ReDim xy2((Xmx - Xmn) / stX) Ymn = Double.MaxValue Ymx = Double.MinValue Dim xx As Double = Xmn Dim ffx1 As Double For i = 0 To xy1.Length - 1 ffx1 = ff1(xx) xy1(i) = New PointF(xx, ffx1) xy2(i) = New PointF(xx, ff2(xx)) Ymn = Math.Min(Ymn, ffx1) Ymx = Math.Max(Ymx, ffx1) xx += stX Next pnAxes = New Pen(Brushes.Black, -1) pnCurve1 = New Pen(Brushes.Red, -1) pnCurve2 = New Pen(Brushes.Blue, -1) pnGrid = New Pen(Brushes.LightGray, -1) _pnAxesColor = Color.Black _pnCurveColor1 = Color.Red _pnCurveColor2 = Color.Blue _isGrid = False isAxDivision = False _isAxesLetter = False _divisionNumberToX = 2 _divisionNumberToY = 2 End Sub Public Property pnAxesColor As Color 'цвет осей Public Property pnCurveColor1 As Color 'цвет кривой 1 Public Property pnCurveColor2 As Color 'цвет кривой 2 Public Property isGrid As Boolean 'сетка Public Property isAxDivision As Boolean 'шкала Public Property isAxesLetter As Boolean 'подписывать Public Property divisionNumberToX As Integer 'число делений шкалы X Public Property divisionNumberToY As Integer 'число делений шкалы Y Public Sub plot(ByVal w As Integer, ByVal h As Integer, ByVal e As System.Windows.Forms.PaintEventArgs) pnAxes.Color = _pnAxesColor pnCurve1.Color = _pnCurveColor1 pnCurve2.Color = _pnCurveColor2 Dim g As Graphics = e.Graphics g.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias Dim xxmx As Double = Xmx Dim xxmn As Double = Xmn Dim yymx As Double = Ymx Dim yymn As Double = Ymn Try If isAxDivision Then Dim axX() As Double = divisionAxes(Xmx, Xmn, divisionNumberToX) Dim axY() As Double = divisionAxes(Ymx, Ymn, divisionNumberToY) xxmx = axX(1) xxmn = axX(0) yymx = axY(1) yymn = axY(0) divX = axX(2) divY = axY(2) End If Dim scX As Single = w / (xxmx - xxmn) Dim scY As Single = h / (yymx - yymn) g.ScaleTransform(scX, -scY) g.TranslateTransform(-xxmn, -yymx) g.Clear(Color.White) If isAxDivision AndAlso isGrid Then For xx As Double = xxmn To xxmx Step divX g.DrawLine(pnGrid, New PointF(xx, yymn), New PointF(xx, yymx)) g.DrawLine(pnAxes, New PointF(xx, 0), New PointF(xx, -divY * 0.1)) Next For yy As Double = yymn To yymx Step divY g.DrawLine(pnGrid, New PointF(xxmn, yy), New PointF(xxmx, yy)) g.DrawLine(pnAxes, New PointF(0, yy), New PointF(divX * 0.1, yy)) Next End If g.DrawLine(pnAxes, New PointF(0, yymn), New PointF(0, yymx)) g.DrawLine(pnAxes, New PointF(xxmn, 0), New PointF(xxmx, 0)) g.DrawLines(pnCurve1, xy1) g.DrawLines(pnCurve2, xy2) If isAxesLetter Then g.ResetTransform() g.ScaleTransform(1, 1) g.TranslateTransform(-xxmn * scX, yymx * scY) Dim txtFont As New Font(FontFamily.GenericMonospace, 9, FontStyle.Regular) For xx = xxmn To xxmx Step divX g.DrawString(Format(xx, "0.0"), txtFont, Brushes.Black, xx * scX, divY * 0.2 * scY) Next For yy = yymn To yymx Step divY If yy = 0 Then Continue For g.DrawString(Format(yy, "0.0"), txtFont, Brushes.Black, divX * 0.2 * scX, -yy * scY) Next End If Catch ex As Exception MsgBox(ex.Message) End Try End Sub Private Function divisionAxes(ByVal vmx As Double, ByVal vmn As Double, ByVal div As Double) As Double() Dim ax(2) As Double Dim divSize As Double = (vmx - vmn) / div Dim tdSize As Double = 10 ^ Int(Math.Log10(divSize)) Dim mfc As Double = 1 Do While (vmx > (tdSize * Int(vmn / tdSize) + div * tdSize)) mfc += 1 If (mfc Mod 4) > 0 Then tdSize = 8 * tdSize / 5 tdSize = 5 * tdSize / 4 Loop ax(0) = tdSize * Int(vmn / tdSize) 'min шкалы ax(1) = ax(0) + div * tdSize 'max шкалы ax(2) = (ax(1) - ax(0)) / div 'деление шкалы Return ax End Function End Class
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д