Как создать оси координат и рисовать на них? - Visual Basic .NET
Формулировка задачи:
Ситуация такая: нужно как - то построить оси координат (-6 до 6), по y (-2 до 2), как то нарисовать любой график, считать точки Y в данной точке X и потом считать интегралл с этих точек. Может кто сталкивался с подобным? Я сижу в тупике, голова кружится, листаю литературу и сайты не могу найти. Спасибо за помощь! Это все на языке VB.Net.
Решение задачи: «Как создать оси координат и рисовать на них?»
textual
Листинг программы
Public Class Form1 Private xmin, xmax, ymin, ymax, w, h As Integer Private bmp As Bitmap Private g As Graphics Private pnAxis, pnGraphics As Pen Private pnt As List(Of PointF), pp As PointF Private isDraw As Boolean Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load xmin = -6 : xmax = 6 : ymin = -2 : ymax = 2 w = PictureBox1.Width h = PictureBox1.Height bmp = New Bitmap(w, h, PictureBox1.CreateGraphics()) PictureBox1.Enabled = False pnAxis = New Pen(Brushes.Blue, -1) pnGraphics = New Pen(Brushes.Red, 2) Button1.Enabled = False Label2.Text = "min = " & xmin.ToString Label3.Text = "max = " & xmax.ToString Label5.Text = "min = " & ymin.ToString Label4.Text = "max = " & ymax.ToString Label8.Text = "" End Sub Private Sub CreateGraphics_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click PictureBox1.Enabled = True gettingStartedCanvas() Button1.Enabled = True Label8.Text = "" End Sub Private Sub GetIntegral_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click Dim kx As Double = w / 2 / xmax Dim ky As Double = h / 2 / ymax Dim p As PointF For i = 0 To pnt.Count - 1 p = pnt(i) p.X -= w / 2 p.Y -= h / 2 p.Y = -p.Y p.X /= kx p.Y /= ky pnt(i) = p Next 'вычисляем приближенно по методу трапеций Dim sum As Double = 0.0 For i = 1 To pnt.Count - 1 sum += (pnt(i - 1).Y + pnt(i).Y) / 2 * (pnt(i).X - pnt(i - 1).X) Next Label8.Text = sum.ToString Button1.Enabled = False PictureBox1.Enabled = False End Sub Private Sub gettingStartedCanvas() g = Graphics.FromImage(bmp) g.Clear(PictureBox1.BackColor) g.DrawLine(pnAxis, New Point(w / 2, 0), New Point(w / 2, h)) g.DrawLine(pnAxis, New Point(0, h / 2), New Point(w, h / 2)) PictureBox1.Image = bmp g.Dispose() End Sub Private Sub PictureBox1_MouseDown(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown Dim x As Integer = e.Location.X Dim y As Integer = e.Location.Y If x <> 0 Then x = 0 pnt = New List(Of PointF) pp = New PointF(x, y) pnt.Add(pp) isDraw = True g = Graphics.FromImage(bmp) End Sub Private Sub PictureBox1_MouseUp(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp If Not isDraw Then Exit Sub Dim x As Integer = e.Location.X Dim y As Integer = e.Location.Y If x <> w Then x = w pnt.Add(New PointF(x, y)) g.DrawLine(pnGraphics, pp, pnt(pnt.Count - 1)) isDraw = False g.Dispose() PictureBox1.Image = bmp End Sub Private Sub PictureBox1_MouseMove(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove If isDraw Then Dim x As Integer = e.Location.X Dim y As Integer = e.Location.Y If x <= pp.X Then Exit Sub Dim p As PointF = New PointF(x, y) g.DrawLine(pnGraphics, pp, p) pnt.Add(p) pp = p PictureBox1.Image = bmp End If End Sub End Class
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д