vb.net绘制平滑曲线 vba画曲线

vb如何把已知点绘制成曲线这是画点vb.net绘制平滑曲线的vb.net绘制平滑曲线:
Private Sub Form_Click()
Dim X(7) As Integer, Y(7) As Integer, i%
Form1.Scale (-15, 15)-(15, -15)
Form1.Line (0, 0)-(15, 0)
Form1.Line (0, 15)-(0, 0)
For i = 0 To 12 'Step 1.2'X轴坐标
Form1.Line (i, 0.1)-(i, 0)
CurrentX = i
CurrentY = -1
Form1.Print i
Next i
For i = 0 To 12 'Step 1.2'Y轴坐标
Form1.Line (0, i)-(0.3, i)
CurrentX = -0.5
CurrentY = i
Form1.Print i
Next i
X(0) = 0: X(1) = 2: X(2) = 4: X(3) = 6: X(4) = 8: X(5) = 10: X(6) = 12: X(7) = 14
Y(0) = 0: Y(1) = 1: Y(2) = 3: Y(3) = 4: Y(4) = 3: Y(5) = 2: Y(6) = 1: Y(7) = 0
For i = 0 To 7
Form1.PSet (X(i), Y(i)), vbRed
Next i
End Sub
VB如何实现平滑曲线VERSION 5.00Begin VB.Form FrmMainAutoRedraw = -1 'TrueCaption = "光滑直线"ClientHeight = 6780ClientLeft = 60ClientTop = 450ClientWidth = 9795LinkTopic = "Form1"ScaleHeight = 452ScaleMode = 3 'PixelScaleWidth = 653StartUpPosition = 3 '窗口缺省Begin VB.CommandButton CmdDrawCaption = "绘制"Height = 375Left = 120TabIndex = 0Top = 120Width = 1215EndBegin VB.Line Line1Visible = 0 'FalseX1 = 48X2 = 272Y1 = 128Y2 = 88EndEndAttribute VB_Name = "FrmMain"Attribute VB_GlobalNameSpace = FalseAttribute VB_Creatable = FalseAttribute VB_PredeclaredId = TrueAttribute VB_Exposed = FalsePrivate Declare Function SetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal crColor As Long) As LongFunction Min(s1 As Long, s2 As Long) As LongIf s1s2 Then Min = s1 Else Min = s2End FunctionFunction Max(s1 As Long, s2 As Long) As LongIf s1s2 Then Max = s1 Else Max = s2End FunctionPrivate Sub CmdDraw_Click()Dim Scal As Single, Gray As Long, MinX As Long, MaxX As Long, MinY As Long, MaxY As LongDim X1 As Long, X2 As Long, Y1 As Long, Y2 As Long, X3 As Single, Y3 As SingleLine1.Visible = FalseMinX = Min(Line1.X1, Line1.X2): MaxX = Max(Line1.X1, Line1.X2)MinY = Min(Line1.Y1, Line1.Y2): MaxY = Max(Line1.Y1, Line1.Y2)If (Line1.Y1Line1.Y2 And Line1.X1Line1.X2) Or (Line1.Y1Line1.Y2 And Line1.X1Line1.X2) ThenIf Abs(Line1.X1 - Line1.X2)Abs(Line1.Y1 - Line1.Y2) ThenScal = Abs(Line1.Y2 - Line1.Y1) / Abs(Line1.X2 - Line1.X1)For i = 0 To MaxX - MinXY3 = i * ScalMinY: Y1 = Int(Y3): Y2 = Y11Gray = (Y2 - Y3) * 255SetPixel Me.hdc, iMinX, Y2, RGB(Gray, Gray, Gray)Gray = (Y3 - Y1) * 255SetPixel Me.hdc, iMinX, Y1, RGB(Gray, Gray, Gray)Next iElseScal = Abs(Line1.X2 - Line1.X1) / Abs(Line1.Y2 - Line1.Y1)For i = 0 To MaxY - MinYX3 = i * ScalMinX: X1 = Int(X3): X2 = X11Gray = (X2 - X3) * 255SetPixel Me.hdc, X2, iMinY, RGB(Gray, Gray, Gray)Gray = (X3 - X1) * 255SetPixel Me.hdc, X1, iMinY, RGB(Gray, Gray, Gray)Next iEnd IfElseIf Abs(Line1.X1 - Line1.X2)Abs(Line1.Y1 - Line1.Y2) ThenScal = -Abs(Line1.Y2 - Line1.Y1) / Abs(Line1.X2 - Line1.X1)For i = 0 To MaxX - MinXY3 = i * ScalMaxY: Y1 = Int(Y3): Y2 = Y11Gray = (Y2 - Y3) * 255SetPixel Me.hdc, iMinX, Y2, RGB(Gray, Gray, Gray)Gray = (Y3 - Y1) * 255SetPixel Me.hdc, iMinX, Y1, RGB(Gray, Gray, Gray)Next iElseScal = -Abs(Line1.X2 - Line1.X1) / Abs(Line1.Y2 - Line1.Y1)For i = 0 To MaxY - MinYX3 = i * ScalMaxX: X1 = Int(X3): X2 = X11Gray = (X2 - X3) * 255SetPixel Me.hdc, X2, iMinY, RGB(Gray, Gray, Gray)Gray = (X3 - X1) * 255SetPixel Me.hdc, X1, iMinY, RGB(Gray, Gray, Gray)Next iEnd IfEnd IfMe.RefreshEnd SubPrivate Sub Form_Load()Me.ScaleMode = vbPixelsMe.AutoRedraw = TrueEnd SubPrivate Sub Form_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)Line1.Visible = TrueLine1.X1 = x: Line1.Y1 = yEnd SubPrivate Sub Form_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)If Button = vbLeftButton Then Line1.X2 = x: Line1.Y2 = yEnd SubPrivate Sub Form_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)Line1.X2 = x: Line1.Y2 = yEnd Sub
vb中如何画曲线图?我先给你写一段如果不明白加我QQ:905906
在工控制软件中,实时曲线的绘制用途非常的广泛,它可以很直观的显示出监控数据的变化值和变化趋势 。在VB中实现曲线的绘制有很多种方法,本文介绍一种非常简单的方法来实现实时曲线的绘制 。
在VB中实现实时曲线的绘制 , 要利用VB的PictureBox(图像)控件 , 和画线函数line(x1,y1)-(x2,y2) 。PictureBox控件,可以作为一个“容器”,在它的里面可以包含很多的对象 。也可以执行很多VB的内部函数 。
要实现实时曲线的绘制,肯定要有外部实时数据的输入,这里假设是有一个数据从计算机的串口输入 定义该数据变量为DataFromCom 。实时曲线反映的就是该数据 。
打开VB6.0中文版,新建一个项目和窗体,修改窗体的属性 , 将“Heigh”修改为:8000,“Width”修改为在窗体中放如一个PictureBox控件 。然后重新定义PictureBox控件的一些基本属性,在VB中选中PictureBox控件,直接在它的属性框中,修改一些属性参数 。“名称”改为Pic 。“AutoRedraw”改为:True 。“BackColor”改为:H00004000(墨绿色背景颜色) 。”Heigh”改为:5000 。“Width”改为:8000 。如图1所示:
然后要重新定义PictureBox控件的坐标系 。图像框的默认坐标系,是从左上角开始的,不符合我们的画线要求 。修改坐标系的目的是让曲线从图像框的左边正中间,开始画线 。修改图像框的坐标系,这里定义一个过程PicScale(),代码如下:
Private Sub PicScale(picX As PictureBox)
【vb.net绘制平滑曲线 vba画曲线】picX.Scale (0, PicX.ScaleHeight)-(picX.ScaleWidth, -PicX.ScaleHeight)
End Sub
一般的实时曲线显示的时候在屏幕的正中间有一条基准线,这里也要画出这条基准线,用一个过程PicMidleLine()来实现,代码如下:
Private Sub PicMidleLine( picX As PictureBox)
picX.Line (0, 0)-(picX.ScaleWidth, 0), vbGreen '画出中线
End Sub
要画一条实时曲线,坐标轴的设定很重要,在这里把X轴设定为时间轴 , Y轴设定为数据轴 。对应X轴我们定义一个时间变量TimeCount,TimeCount会随着时间逐渐递增,每次递增 , 对应着一个从串口读过来的数据DataFromCom,这样图像框中的(x,y)坐标点实际上就对应着(TimeCount,DataFromCom)如果只是当TimeCount发生变化时就在图像框上画一个点,就只需调用VB中的画像素的函数point(x,y),这里x,y为所画的点的坐标 。单这样画出来的是一个个不连续的点 。我们想要的是实时的连续的曲线,所以要调用VB中的画线的函数line(x1,y1)-(x2,y2),这里(x1,y1)(x2,y2)为所要画的线的起点和终点的坐标 。只要把上次串口读过来的数据(这里把它定义为变量DataFromComLast)和现在串口读过来的数据(DataFromCom)和TimeCount相对应,调用line(x1,y1)-(x2,y2)函数就可以在图像框中画出实时的曲线了 。把它写成一个过程如下面的代码:
Private Sub DrawRealLine(picX As PictureBox, TimeCountX As Integer, DataFromComX As Integer, DataFromComLastX As Integer)
If TimeCountX - 10 Then
picX.Line (TimeCountX - 1, DataFromComLastX)-(TimeCountX, DataFromComX), vbWhite
End If
End Sub
有了三个过程就可以在图像框中画出一条实时的曲线了 。
DrawRealLine()过程中的picX.Line (TimeCountX - 1, DataFromComLastX)-(TimeCountX, DataFromComX), vbWhite所画线的起始点和结束点都是以像素为单位的,这样以来如果不改变的话 , 画出来的线将是一个屏幕上像素相连的很密的曲线,通过调整line(x1,y1)-(x2,y2)
中的x的值 , 就可以画出分布密度不一样的曲线,这里为了在屏幕上能够看到不是很密的曲线我们把x乘以一个系数10,修改为:
picX.Line ((TimeCountX - 1) * 10, DataFromComLastX)-(TimeCountX * 10, DataFromComX), vbWhite
这比较容易在屏幕上看到稀疏的曲线 。
由于是仅仅讲解如何画出实时的曲线 , 读者的计算机上未必有和串口相连的设备,这里用一个定时器控件来模拟从串口读过来的数据 。在窗体上放入一个Timer控件,修改Timer控件的属性为:“Enable”该为True , “Interval”改为300 。双击Timer控件在它的过程中,添加代码后如下:
Private Sub Timer1_Timer()
DataFromComLast = DataFromCom
Randomize
DataFromCom = 3000 * Rnd
TimeCount = TimeCount1
DrawRealLine Pic, TimeCount, DataFromCom, DataFromComLast
End Sub
这样在运行后就可以看到我们想要的实时曲线了,如下图:
图 2
下面是完整的代码:
Option Explicit
Dim DataFromCom As Integer '从串口读过来的实时值
Dim DataFromComLast As Integer '上次的串口值
Dim TimeCount As Integer
Private Sub Form_Load()
PicScale Pic '调整图像框的坐标系
PicMidleLine Pic '在图像框中画一条中线
End Sub
Private Sub PicScale(picX As PictureBox) '调整图像框的坐标系
picX.Scale (0, picX.ScaleHeight)-(picX.ScaleWidth, -picX.ScaleHeight)
End Sub
Private Sub PicMidleLine(picX As PictureBox) '在图像框中画一条中线
picX.Line (0, 0)-(picX.ScaleWidth, 0), vbGreen '画出中线
End Sub
Private Sub DrawRealLine(picX As PictureBox, TimeCountX As Integer, DataFromComX As Integer, DataFromComLastX As Integer)
If TimeCountX - 10 Then
picX.Line ((TimeCountX - 1) * 10, DataFromComLastX)-(TimeCountX * 10, DataFromComX), vbWhite
End If
End Sub
Private Sub Timer1_Timer()
DataFromComLast = DataFromCom
Randomize
DataFromCom = 3000 * Rnd
TimeCount = TimeCount1
DrawRealLine Pic, TimeCount, DataFromCom, DataFromComLast '画出实时的曲线
End Sub
vb.net绘制曲线图 。net其实还是很好绘制图形的
你可以看下 Graphics类
Dim d As New Bitmap(Me.Width, Me.Height)‘一个图片吧
Dim g As Graphics = Graphics.FromImage(d)’绘制准备在这个图片是进行
然后就是你绘制的东西了
线 就是g.DrawLine()
圆 弧度就用g.DrawArc(Pens.Black, New Rectangle(0, 0, 400, 200), 0, 360)
复杂的就是g.DrawBezier()
等如果你用的是 VS的编译上面都有详细的参数说明
Dim d As New Bitmap(Me.Width, Me.Height)
Dim g As Graphics = Graphics.FromImage(d)
g.DrawArc(Pens.Black, New Rectangle(0, 0, 200, 200), 0, 360)
g.DrawLine(Pens.Red, New Point(0, 0), New Point(200, 200))
g.DrawLines(Pens.Green, New Point() {New Point(0, 0), New Point(50, 40), New Point(50, 80), New Point(90, 70), New Point(100, 400)})
g.DrawBezier(Pens.Yellow, New Point(0, 100), New Point(0, 0), New Point(200, 0),New Point(200, 200))
g.Dispose()
Me.BackgroundImage = d
用VB如何画曲线,并求代码.Option Explicit
Private Const X_Axe = 0.5'X轴原点位于窗口的水平位置比例
Private Const Y_Axe = 0.6'Y轴原点位于窗口的垂直位置比例
Private Const ScaleRate = 30'多少像素代表一个单位长度
Private Const DrawColor = vbRed'曲线颜色
Private ErrExp As Boolean'当表达式发生错误时,会置True(比如函数在此点无有效值)
Private Sub Form_Load()
Me.Show
Me.Cls
Call DrawAxe
Call DrawCoordinate
End Sub
Private Function Expression(ByVal X As Double) As Double'公式函数可以是任意内容
ErrExp = False'进入时 , 必须重置此标志为False
If X0 Then'这里代码任意,也可以是Expression = 2 * X ^ 22 * X1之类的
Expression = Log(X)
Else
Expression = 0
ErrExp = True
End If
End Function
Private Sub DrawCoordinate()'绘制曲线过程
Dim i As Long
Dim Last(1 To 2) As Long, This(1 To 2) As Long
Dim X_offset As Long
Dim Y_offset As Long
Me.ScaleMode = vbPixels
X_offset = Me.ScaleWidth * X_Axe
Y_offset = Me.ScaleHeight * Y_Axe
'设置一个初始值
Last(1) = 0
Last(2) = -Expression((0 - X_offset) / ScaleRate) * ScaleRate
i = 0
While i = Me.ScaleWidth
'取下一点的值
This(1) = i
This(2) = -Expression((i - X_offset) / ScaleRate) * ScaleRate
'判断表达式是否出错
If ErrExp = True Then
'出错的情况下,循环直到没有错误或者超出范围为止
While ErrExp = True And i = Me.ScaleWidth
i = i1
This(1) = i
This(2) = -Expression((i - X_offset) / ScaleRate) * ScaleRate
Wend
'重置起点
Last(1) = This(1)
Last(2) = This(2)
End If
'画线
Me.Line (Last(1), Last(2)Y_offset)-(This(1), This(2)Y_offset), DrawColor
Last(1) = This(1)
Last(2) = This(2)
i = i1
Wend
End Sub
Private Sub DrawAxe()'绘制坐标的过程
Dim X_offset As Long
Dim Y_offset As Long
Dim i As Long
Me.ScaleMode = vbPixels'取单位长度为像素
X_offset = Me.ScaleWidth * X_Axe'计算坐标轴轴位置
Y_offset = Me.ScaleHeight * Y_Axe
'绘制坐标轴
Me.Line (X_offset, 0)-(X_offset, Me.ScaleHeight)
Me.Line (0, Y_offset)-(Me.ScaleWidth, Y_offset)
'绘制坐标线
For i = X_offsetScaleRate To Me.ScaleWidth Step ScaleRate
Me.Line (i, 0)-(i, Me.ScaleHeight), vbWhite
Next i
For i = X_offset - ScaleRate To 0 Step -ScaleRate
Me.Line (i, 0)-(i, Me.ScaleHeight), vbWhite
Next i
For i = Y_offsetScaleRate To Me.ScaleHeight Step ScaleRate
Me.Line (0, i)-(Me.ScaleWidth, i), vbWhite
Next i
For i = Y_offset - ScaleRate To 0 Step -ScaleRate
Me.Line (0, i)-(Me.ScaleWidth, i), vbWhite
Next i
End Sub
Private Sub Form_Resize()
Me.Cls
Call DrawAxe
Call DrawCoordinate
End Sub
vb怎么将多个点用光滑曲线连起来 , 并画出这条曲线的平均线用贝塞尔曲线画就可以了
Private Type POINTAPI
xAs Long
yAs Long
End Type
Private Declare Function PolyBezierTo Lib "gdi32.dll " (ByVal hdc As Long, lppt As POINTAPI, ByVal cCount As Long) As Long
Private Sub Form_Paint()
Dim pts(0 To 6)As POINTAPI
'settheco?rdinates
pts(0).x = 22:pts(0).y = 33
pts(1).x = 66:pts(1).y = 55
pts(2).x = 177:pts(2).y = 88
pts(3).x = 199:pts(3).y = 111
pts(4).x = 299:pts(4).y = 222
pts(5).x = 80:pts(5).y = 333
PolyBezierTo Me.hdc, pts(0), 6
End Sub
平均线 ??是个什么线????求出平均值后话直线吗?
那就lineto好了
Private Type POINTAPI
xAs Long
yAs Long
End Type
Private Declare Function PolyBezierTo Lib "gdi32.dll " (ByVal hdc As Long, lppt As POINTAPI, ByVal cCount As Long) As Long
Private Sub Form_Paint()
Dim pts(0 To 6)As POINTAPI
Dim pt(0 To 6)As POINTAPI
'settheco?rdinates
pts(0).x = 22:pts(0).y = 33
pts(1).x = 66:pts(1).y = 55
pts(2).x = 177:pts(2).y = 88
pts(3).x = 199:pts(3).y = 111
pts(4).x = 299:pts(4).y = 222
pts(5).x = 80:pts(5).y = 333
For n = 1 To 6
pt(n).x = (pts(n).xpts(n - 1).x) / 2
pt(n).y = (pts(n).ypts(n - 1).y) / 2
Next n
PolyBezierTo Me.hdc, pt(0), 6
PolyBezierTo Me.hdc, pts(0), 6
End Sub
关于vb.net绘制平滑曲线和vba画曲线的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站 。

    推荐阅读