实例105 绘制函数曲线

本文探讨如何利用Graphics.DrawCurve方法在.NET中绘制函数曲线,重点介绍该方法的重载形式,并讨论屏幕坐标系与数学坐标系之间的转换问题。

Graphics.DrawCurve 方法

https://docs.microsoft.com/zh-cn/dotnet/api/system.drawing.graphics.drawcurve?view=dotnet-plat-ext-3.1#System_Drawing_Graphics_DrawCurve_System_Drawing_Pen_System_Drawing_Point___

重载

表 1
DrawCurve(Pen, Point[])

绘制经过一组指定的 Point 结构的基数样条。

DrawCurve(Pen, PointF[])

绘制经过一组指定的 PointF 结构的基数样条。

DrawCurve(Pen, Point[], Single)

使用指定的张力绘制经过一组指定的 Point 结构的基数样条。

DrawCurve(Pen, PointF[], Single)

使用指定的张力绘制经过一组指定的 PointF 结构的基数样条。

DrawCurve(Pen, PointF[], Int32, Int32)

绘制经过一组指定的 PointF 结构的基数样条。 从相对于数组开始位置的偏移量开始绘制。

DrawCurve(Pen, Point[], Int32, Int32, Single)

使用指定的张力绘制经过一组指定的 Point 结构的基数样条。

DrawCurve(Pen, PointF[], Int32, Int32, Single)

使用指定的张力绘制经过一组指定的 PointF 结构的基数样条。 从相对于数组开始位置的偏移量开

屏幕坐标系与常用数学的坐标系的转换有点难搞

Imports System.Math
Public Class Form1
    Dim myGraph As Graphics
    Dim myPen As New Pen(Color.Black, 3)
    Private Sub Paintline()
        myGraph = PictureBox1.CreateGraphics
        myGraph.Clear(Me.BackColor)
        myPen.Color = Color.Black
        myPen.Width = 3
        myPen.EndCap = Drawing2D.LineCap.ArrowAnchor

        Dim Ox As Integer = Int((PictureBox1.Left + PictureBox1.Right) / 2)
        Dim Oy As Integer = Int((PictureBox1.Top + PictureBox1.Bottom) / 2)
        Dim Xleft As Integer = Int(PictureBox1.Left + 5)
        Dim Xright As Integer = Int(PictureBox1.Right - 5)
        myGraph.DrawLine(myPen, Xleft, Oy, Xright, Oy)

        Dim Ytop As Integer = Int(PictureBox1.Top + 5)
        Dim Ybottom As Integer = Int(PictureBox1.Bottom - 5)
        myGraph.DrawLine(myPen, Ox, Ybottom, Ox, Ytop)


        Dim LinePoints() As PointF
        Dim SinPoints(Xright - Xleft) As PointF
        Dim CurvePoints(Xright - Xleft) As PointF

        If CheckBox1.Checked Then
            ReDim LinePoints(1)
            LinePoints(0) = New PointF(Ox + 150, Oy - 150)
            LinePoints(1) = New PointF(Ox - 150, Oy + 150)
            myPen.Width = 3
            myPen.Color = Color.Red
            myPen.EndCap = Drawing2D.LineCap.Custom
            myGraph.DrawLine(myPen, LinePoints(0), LinePoints(1))
        End If

        If CheckBox2.Checked Then
            Dim x As Integer
            Dim x1 As Single
            Dim y1 As Single
            For x = 0 To Xright - Xleft
                x1 = x
                y1 = Oy - 80 * Sin((x - Ox) / 90 * PI)
                SinPoints(x) = New PointF(x1, y1)
            Next
            myPen.Width = 3
            myPen.Color = Color.Blue
            myPen.EndCap = Drawing2D.LineCap.Custom
            myGraph.DrawCurve(myPen, SinPoints)
        End If

        If CheckBox3.Checked Then
            Dim x As Integer
            Dim x1 As Single
            Dim y1 As Single
            For x = 0 To Xright - Xleft
                x1 = x
                y1 = Oy - (x - Ox) * (x - Ox) / 100
                CurvePoints(x) = New PointF(x1, y1)
            Next
            myPen.Width = 3
            myPen.Color = Color.Green
            myPen.EndCap = Drawing2D.LineCap.Custom
            myGraph.DrawCurve(myPen, CurvePoints)
        End If
    End Sub

    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        Paintline()
    End Sub

    Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
        Paintline()
    End Sub

    Private Sub CheckBox2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox2.CheckedChanged
        Paintline()
    End Sub

    Private Sub CheckBox3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox3.CheckedChanged
        Paintline()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.Close()
    End Sub
End Class

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ngbshzhn

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值