VB.NET螺旋计程序

时间:2016-07-26 04:43:22

标签: vb.net

我试图创建一个可以绘制hypotrochoids(spirograph)的程序。下面的程序编译得很好。但是当我运行它时,我只得到了一部分图画......我不确定我做错了什么。我对VB很新。任何帮助都表示赞赏。感谢。

以下是屏幕截图http://imgur.com/a/KxFWk

Public Class Form1

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
End Sub

Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint

Dim x As Integer
Dim y As Integer
Dim p As Integer
Dim x1 As Integer
Dim y1 As Integer
Dim x2 As Integer
Dim y2 As Integer

x = 75
y = 15
p = 15

x1 = (x + y) * Math.Cos(0) + p * Math.Cos(0)
y1 = (x + y) * Math.Sin(0) + p * Math.Sin(0)
For t = 0 To 500 Step 0.1
    x2 = (x + y) * Math.Cos(t) + p * Math.Cos((x + y) * t / y)
    y2 = (x + y) * Math.Sin(t) + p * Math.Sin((x + y) * t / y)
    e.Graphics.DrawLine(Pens.Blue, x1, y1, x2, y2)
    x1 = x2
    y1 = y2
Next
End Sub
End Class

1 个答案:

答案 0 :(得分:0)

Sin和Cos计算的结果导致负数,其中cos的参数大于90且sin的参数大于180。

要查看整个图像,您需要更改x2和y2的偏移量 - 请参阅下面的代码。将四行中的每一行中的数字200更改为适合您的图片框的值

    x1 = 200 + CInt((x + y) * Math.Cos(0) + p * Math.Cos(0))
    y1 = 200 + CInt((x + y) * Math.Sin(0) + p * Math.Sin(0))

    For t As Double = 0 To 500 Step 0.1
        x2 = 200 + CInt((x + y) * Math.Cos(t) + p * Math.Cos((x + y) * t / y))
        y2 = 200 + CInt((x + y) * Math.Sin(t) + p * Math.Sin((x + y) * t / y))
        e.Graphics.DrawLine(Pens.Blue, x1, y1, x2, y2)
        x1 = x2
        y1 = y2
    Next