Visual C#中随机数使用实例

关键词Visual    C#中随机数使用实例                                          

 

随机数的使用很普遍,可用它随机显示图片,用它防止无聊的人在论坛灌水还可以用来加密信息等等。本文讨论如何在一段数字区间内随机生成若干个互不相同的随机数,比如在从120间随机生成6个互不相同的整数,并通过此文介绍Visual c#中随机数的用法。

 

.net.Frameword中提供了一个专门产生随机数的类System.Random,此类默认情况下已被导入,编程过程中可以直接使用。我们知道,计算机并不能产生完全随机的数字,它生成的数字被称为伪随机数,它是以相同的概率从一组有限的数字中选取的,所选的数字并不具有完全的随机性,但就实用而言,其随机程度已经足够了。

 

我们可以用以下两种方法初始化一个随机数发生器;

 

第一种方法不指定随机种子,系统自动选取当前时前作随机种子:

 

Random ra=new Random();

 

第二种方法是指定一个int型的参数作为随机种子:

 

int iSeed=6;

 

Random ra=new Random(iSeed);

 

下面我们要用到Random.Next()方法产生随机数。

 

ra.Next();

 

它返回一个大于或等于零而小于2,147,483,647的数,这并不满足我们的需要,下面我们介绍它的重载函数和其它一些方法。

 

         public virtual int Next(int);

 

用法:ra.next(20)

 

返回一个小于所指定最大值(此处为20)的正随机数。

 

public virtual int Next(int minValue, int maxValue);

 

用法:ra.next(1,20)

 

返回一个指定范围内(此处为1-20之间)的随机数,我们在下面的实例中会用到此函数。

 

System.Random还有几个方法分别是:

 

公共方法:

 

NextBytes用随机数填充指定字节数组的元素。

 

NextDouble返回一个介于 0.0 1.0 之间的随机数。

 

受保护的方法:

 

Sample返回一个介于 0.0 1.0 之间的随机数,只允许子类对象访问。

 

以上介绍了随机数的基本用法,下面我们用一个实例来做更进一步的介绍。要在一段数字区间内随机生成若干个互不相同的随机数,比如在从120间随机生成6个互不相同的整数。

 

主要是下面两个函数getRandomNumgetNum:

 

public int[] getRandomNum(int num,int minValue,int maxValue)

 

{

 

Random ra=new Random(unchecked((int)DateTime.Now.Ticks));

 

int[] arrNum=new int[num];

 

int tmp=0;

 

for (int i=0;i<=num-1;i++){

 

tmp=ra.Next(minValue,maxValue); //随机取数

 

arrNum[i]=getNum(arrNum,tmp,minValue,maxValue,ra); //取出值赋到数组中

 

}

 

return arrNum;

 

}

 

getRandomNum即是在区间[minValue,maxValue]取出num个互不相同的随机数,返回的数组包含着结果。

 

其中随机数是这样创建的 Random ra=new Random(unchecked((int)DateTime.Now.Ticks));为什么不用Random ra=new Random();(系统自动选取当前时前作随机种子)呢?

 

用系统时间做随机种子并不保险,如果应用程序在一个较快的计算机上运行,则该计算机的系统时钟可能没有时间在此构造函数的调用之间进行更改,Random 的不同实例的种子值可能相同。这种情况下,我们就需要另外的算法来保证产生的数字的随机性。所以为了保证产生的随机数足够“随机”,我们不得不使用复杂一点的方法来获得随机种子。在上面的这段程序中,我们首先使用系统时间作为随机种子,然后将上一次产生的随机数跟循环变量和一个与系统时间有关的整型参数相乘,以之作为随机种子,从而得到了每次都不同的随机种子,保证了产生足够“随机”的随机数。

 

函数getNum是一递归,用它来检测生成的随机数是否有重复,如果取出来的数字和已取得的数字有重复就重新随机获取。值得注意的是要用一同一个随机数实例生成,所以ra要作为参数传入getNum中,否则生成的数字会有重复。

 

public int getNum(int[] arrNum,int tmp,int minValue,int maxValue,Random ra){

 

int n=0;

 

while (n<=arrNum.Length-1)

 

{

 

if (arrNum[n]==tmp) //利用循环判断是否有重复

 

{

 

tmp=ra.Next(minValue,maxValue); //重新随机获取。

 

getNum(arrNum,tmp,minValue,maxValue,ra);//递归:如果取出来的数字和已取得的数字有重复就         重新随机获取。

 

}

 

n++;

 

}

 

return tmp;

 

}

 

最后就是要显示出来,当点击一个button时取出的数字显示在一个label中。

 

private void button1_Click(object sender, System.EventArgs e)

 

{

 

int[] arr=getRandomNum(6,1,20); //120中取出6个互不相同的随机数

 

int i=0;

 

string temp="";

 

while (i<=arr.Length-1){

 

temp+=arr[i].ToString()+"/n";

 

i++;

 

}

 

label1.Text=temp; //显示在label1

 

}

 

随机数的作用不止如此,读者可用它进行游戏开发,安全验证等等,这有待读者去开发和实践。

 

原码如下:

 

using System;

 

using System.Drawing;

 

using System.Collections;

 

using System.ComponentModel;

 

using System.Windows.Forms;

 

using System.Data;

 

namespace random

 

{

 

///

 

/// Form1 的摘要说明。

 

///

 

public class Form1 : System.Windows.Forms.Form

 

{

 

private System.Windows.Forms.Button button1;

 

private System.Windows.Forms.Label label1;

 

///

 

/// 必需的设计器变量。

 

///

 

private System.ComponentModel.Container components = null;

 

public Form1()

 

{

 

//

 

// Windows 窗体设计器支持所必需的

 

//

 

InitializeComponent();

 

//

 

// TODO: InitializeComponent 调用后添加任何构造函数代码

 

//

 

}

 

///

 

/// 清理所有正在使用的资源。

 

///

 

protected override void Dispose( bool disposing )

 

{

 

if( disposing )

 

{

 

if (components != null)

 

{

 

components.Dispose();

 

}

 

}

 

base.Dispose( disposing );

 

}

 

#region Windows Form Designer generated code

 

///

 

/// 设计器支持所需的方法 - 不要使用代码编辑器修改

 

/// 此方法的内容。

 

///

 

private void InitializeComponent()

 

{

 

this.button1 = new System.Windows.Forms.Button();

 

this.label1 = new System.Windows.Forms.Label();

 

this.SuspendLayout();

 

//

 

// button1

 

//

 

this.button1.Location = new System.Drawing.Point(96, 32);

 

this.button1.Name = "button1";

 

this.button1.TabIndex = 0;

 

this.button1.Text = "随机数";

 

this.button1.Click += new System.EventHandler(this.button1_Click);

 

//

 

// label1

 

//

 

this.label1.BackColor = System.Drawing.SystemColors.Desktop;

 

this.label1.ForeColor = System.Drawing.SystemColors.ActiveCaptionText;

 

this.label1.Location = new System.Drawing.Point(49, 112);

 

this.label1.Name = "label1";

 

this.label1.Size = new System.Drawing.Size(168, 80);

 

this.label1.TabIndex = 1;

 

this.label1.Text = "label1";

 

//

 

// Form1

 

//

 

this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);

 

this.ClientSize = new System.Drawing.Size(292, 273);

 

this.Controls.AddRange(new System.Windows.Forms.Control[] {

 

this.label1,

 

this.button1});

 

this.Name = "Form1";

 

this.Text = "Form1";

 

this.ResumeLayout(false);

 

}

 

#endregion

 

///

 

/// 应用程序的主入口点。

 

///

 

[STAThread]

 

static void Main()

 

{

 

Application.Run(new Form1());

 

}

 

private void button1_Click(object sender, System.EventArgs e)

 

{

 

int[] arr=getRandomNum(6,1,20);

 

int i=0;

 

string temp="";

 

while (i<=arr.Length-1){

 

temp+=arr[i].ToString()+"/n";

 

i++;

 

}

 

label1.Text=temp;

 

}

 

public int[] getRandomNum(int num,int minValue,int maxValue){

 

Random ra=new Random(unchecked((int)DateTime.Now.Ticks));

 

int[] arrNum=new int[num];

 

int tmp=0;

 

for (int i=0;i<=num-1;i++){

 

tmp=ra.Next(minValue,maxValue);

 

arrNum[i]=getNum(arrNum,tmp,minValue,maxValue,ra);

 

}

 

return arrNum;

 

}

 

public int getNum(int[] arrNum,int tmp,int minValue,int maxValue,Random ra){

 

//Random ra=new Random(unchecked((int)DateTime.Now.Ticks));

 

int n=0;

 

while (n<=arrNum.Length-1)

 

{

 

if (arrNum[n]==tmp)

 

{

 

tmp=ra.Next(minValue,maxValue);

 

getNum(arrNum,tmp,minValue,maxValue,ra);

 

}

 

n++;

 

}

 

return tmp;

 

}

 

}

 

}

 

作者: terrysh

http://terrysh.bokee.com/4396429.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Option Strict OnImports System.MathPublic Class MandelbrotForm Inherits System.Windows.Forms.Form#Region " Windows Form Designer generated code " Public Sub New() MyBase.New() ‘This call is required by the Windows Form Designer. InitializeComponent() ‘Add any initialization after the InitializeComponent() call End Sub ‘Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub ‘Required by the Windows Form Designer Private components As System.ComponentModel.IContainer ‘NOTE: The following procedure is required by the Windows Form Designer ‘It can be modified using the Windows Form Designer. ‘Do not modify it using the code editor. Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox Friend WithEvents txtMin As System.Windows.Forms.TextBox Friend WithEvents txtMax As System.Windows.Forms.TextBox Friend WithEvents txtColorMin As System.Windows.Forms.TextBox Friend WithEvents txtColorMax As System.Windows.Forms.TextBox Friend WithEvents bttnMandelbrot As System.Windows.Forms.Button Friend WithEvents Label1 As System.Windows.Forms.Label Friend WithEvents Label2 As System.Windows.Forms.Label Friend WithEvents Label3 As System.Windows.Forms.Label Friend WithEvents Label4 As System.Windows.Forms.Label Friend WithEvents Label5 As System.Windows.Forms.Label Friend WithEvents Label6 As System.Windows.Forms.Label Friend WithEvents bttnViewPrevious As System.Windows.Forms.Button Friend WithEvents bttnViewNext As System.Windows.Forms.Button <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(MandelbrotForm)) Me.PictureBox1 = New System.Windows.Forms.PictureBox Me.bttnMandelbrot = New System.Windows.Forms.Button Me.bttnViewPrevious = New System.Windows.Forms.Button Me.bttnViewNext = New System.Windows.Forms.Button Me.txtMin = New System.Windows.Forms.TextBox Me.txtMax = New System.Windows.Forms.TextBox Me.txtColorMin = New System.Windows.Forms.TextBox Me.txtColorMax = New System.Windows.Forms.TextBox Me.Label1 = New System.Windows.Forms.Label Me.Label2 = New System.Windows.Forms.Label Me.Label3 = New System.Windows.Forms.Label Me.Label4 = New System.Windows.Forms.Label Me.Label5 = New System.Windows.Forms.Label Me.Label6 = New System.Windows.Forms.Label Me.SuspendLayout() ‘ ‘PictureBox1 ‘ Me.PictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle Me.PictureBox1.Location = New System.Drawing.Point(5, 97) Me.PictureBox1.Name = "PictureBox1" Me.PictureBox1.Size = New System.Drawing.Size(512, 512) Me.PictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage Me.PictureBox1.TabIndex = 0 Me.PictureBox1.TabStop = False ‘ ‘bttnMandelbrot ‘ Me.bttnMandelbrot.Font = New System.Drawing.Font("Verdana", 12.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.bttnMandelbrot.Location = New System.Drawing.Point(62, 4) Me.bttnMandelbrot.Name = "bttnMandelbrot" Me.bttnMandelbrot.Size = New System.Drawing.Size(399, 30) Me.bttnMandelbrot.TabIndex = 1 Me.bttnMandelbrot.Text = "New Fractal" ‘ ‘bttnViewPrevious ‘ Me.bttnViewPrevious.Image = CType(resources.GetObject("bttnViewPrevious.Image"), System.Drawing.Image) Me.bttnViewPrevious.Location = New System.Drawing.Point(3, 4) Me.bttnViewPrevious.Name = "bttnViewPrevious" Me.bttnViewPrevious.Size = New System.Drawing.Size(50, 30) Me.bttnViewPrevious.TabIndex = 3 ‘ ‘bttnViewNext ‘ Me.bttnViewNext.Image = CType(resources.GetObject("bttnViewNext.Image"), System.Drawing.Image) Me.bttnViewNext.Location = New System.Drawing.Point(470, 4) Me.bttnViewNext.Name = "bttnViewNext" Me.bttnViewNext.Size = New System.Drawing.Size(50, 30) Me.bttnViewNext.TabIndex = 4 ‘ ‘txtMin ‘ Me.txtMin.Font = New System.Drawing.Font("Verdana", 11.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(161, Byte)) Me.txtMin.Location = New System.Drawing.Point(61, 67) Me.txtMin.Name = "txtMin" Me.txtMin.Size = New System.Drawing.Size(46, 26) Me.txtMin.TabIndex = 6 Me.txtMin.Text = "0" ‘ ‘txtMax ‘ Me.txtMax.Font = New System.Drawing.Font("Verdana", 11.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(161, Byte)) Me.txtMax.Location = New System.Drawing.Point(165, 67) Me.txtMax.Name = "txtMax" Me.txtMax.Size = New System.Drawing.Size(46, 26) Me.txtMax.TabIndex = 7 Me.txtMax.Text = "128" ‘ ‘txtColorMin ‘ Me.txtColorMin.Font = New System.Drawing.Font("Verdana", 11.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(161, Byte)) Me.txtColorMin.Location = New System.Drawing.Point(336, 67) Me.txtColorMin.Name = "txtColorMin" Me.txtColorMin.Size = New System.Drawing.Size(46, 26) Me.txtColorMin.TabIndex = 9 Me.txtColorMin.Text = "0" ‘ ‘txtColorMax ‘ Me.txtColorMax.Font = New System.Drawing.Font("Verdana", 11.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(161, Byte)) Me.txtColorMax.Location = New System.Drawing.Point(451, 67) Me.txtColorMax.Name = "txtColorMax" Me.txtColorMax.Size = New System.Drawing.Size(46, 26) Me.txtColorMax.TabIndex = 10 Me.txtColorMax.Text = "512" ‘ ‘Label1 ‘ Me.Label1.Font = New System.Drawing.Font("Verdana", 14.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(161, Byte)) Me.Label1.Location = New System.Drawing.Point(11, 41) Me.Label1.Name = "Label1" Me.Label1.Size = New System.Drawing.Size(121, 22) Me.Label1.TabIndex = 11 Me.Label1.Text = "Iterations" ‘ ‘Label2 ‘ Me.Label2.Font = New System.Drawing.Font("Verdana", 11.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(161, Byte)) Me.Label2.Location = New System.Drawing.Point(16, 72) Me.Label2.Name = "Label2" Me.Label2.Size = New System.Drawing.Size(39, 16) Me.Label2.TabIndex = 12 Me.Label2.Text = "Min" ‘ ‘Label3 ‘ Me.Label3.Font = New System.Drawing.Font("Verdana", 11.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(161, Byte)) Me.Label3.Location = New System.Drawing.Point(120, 72) Me.Label3.Name = "Label3" Me.Label3.Size = New System.Drawing.Size(46, 16) Me.Label3.TabIndex = 13 Me.Label3.Text = "Max" ‘ ‘Label4 ‘ Me.Label4.Font = New System.Drawing.Font("Verdana", 14.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(161, Byte)) Me.Label4.Location = New System.Drawing.Point(285, 41) Me.Label4.Name = "Label4" Me.Label4.Size = New System.Drawing.Size(158, 22) Me.Label4.TabIndex = 14 Me.Label4.Text = "Color Range" ‘ ‘Label5 ‘ Me.Label5.Font = New System.Drawing.Font("Verdana", 11.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(161, Byte)) Me.Label5.Location = New System.Drawing.Point(293, 72) Me.Label5.Name = "Label5" Me.Label5.Size = New System.Drawing.Size(39, 16) Me.Label5.TabIndex = 15 Me.Label5.Text = "Min" ‘ ‘Label6 ‘ Me.Label6.Font = New System.Drawing.Font("Verdana", 11.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(161, Byte)) Me.Label6.Location = New System.Drawing.Point(402, 72) Me.Label6.Name = "Label6" Me.Label6.Size = New System.Drawing.Size(46, 16) Me.Label6.TabIndex = 16 Me.Label6.Text = "Max" ‘ ‘MandelbrotForm ‘ Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(524, 620) Me.Controls.Add(Me.Label6) Me.Controls.Add(Me.Label5) Me.Controls.Add(Me.Label4) Me.Controls.Add(Me.Label3) Me.Controls.Add(Me.Label2) Me.Controls.Add(Me.Label1) Me.Controls.Add(Me.txtColorMax) Me.Controls.Add(Me.txtColorMin) Me.Controls.Add(Me.txtMax) Me.Controls.Add(Me.txtMin) Me.Controls.Add(Me.bttnViewNext) Me.Controls.Add(Me.bttnViewPrevious) Me.Controls.Add(Me.bttnMandelbrot) Me.Controls.Add(Me.PictureBox1) Me.KeyPreview = True Me.Name = "MandelbrotForm" Me.Text = "Fractal Generator: The Mandelbrot Set" Me.ResumeLayout(False) End Sub#End Region Dim XMin As Double, XMax As Double Dim YMin As Double, YMax As Double Dim XStart, YStart As Integer Dim XEnd, YEnd As Integer Dim currentFractal As Integer Dim breaknow As Boolean Dim limits As New ArrayList Structure Bounding Dim XMin As Double Dim XMax As Double Dim YMin As Double Dim YMax As Double End Structure Private Sub bttnMandelbrot_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnMandelbrot.Click Static IterMin As Integer = 100000 Dim IterMax As Integer = -100000 Dim bmap As Bitmap bmap = New Bitmap(512, 512, Drawing.Imaging.PixelFormat.Format32bppPArgb) PictureBox1.Image = bmap Dim pX, pY As Integer Dim NX As Integer = 512 Dim NY As Integer = 512 Dim MaxIter As Integer Dim X, Y As Double Dim Iterations As Integer Dim minIterations As Integer = CInt(txtMin.Text) Dim maxIterations As Integer = CInt(txtMax.Text) Dim colorMin As Integer Dim colorMax As Integer Dim pixelColor As Color If IsNumeric(txtColorMin.Text) Then colorMin = CInt(txtColorMin.Text) If IsNumeric(txtColorMax.Text) Then colorMax = CInt(txtColorMax.Text) If IsNumeric(txtMin.Text) Then minIterations = CInt(txtMin.Text) If IsNumeric(txtMax.Text) Then maxIterations = CInt(txtMax.Text) If maxIterations <= minIterations Then MsgBox("The number of maximum iterations should be larger " & _ "than the number of minimum iterations") Exit Sub End If If colorMax <= colorMin Then MsgBox("The first color‘s value should be smalled than the last color‘s value") Exit Sub End If For pY = 0 To NY - 1 Y = YMin + pY * (YMax - YMin) / (NY - 1) For pX = 0 To NX - 1 X = (XMin + pX * (XMax - XMin) / (NY - 1)) Iterations = Mandelbrot(X, Y, maxIterations) Dim clr As Integer clr = CInt(colorMin + _ (colorMax - colorMin) / (maxIterations - minIterations) * _ (Iterations - minIterations)) clr = Math.Max(minIterations, clr) pixelColor = PaintPixel(clr) bmap.SetPixel(pX, pY, pixelColor) Next PictureBox1.Invalidate() Application.DoEvents() If breaknow Then breaknow = False Exit Sub End If Next End Sub Private Function PaintPixel(ByVal clr As Integer) As Color Select Case clr Case 0 To 256 - 1 Return Color.FromArgb(clr, 0, 0) Case 256 To 256 * 2 - 1 Return Color.FromArgb(255, clr - 256, 0) Case 256 * 2 To 256 * 3 - 1 Return Color.FromArgb(255, 255, clr - 256 * 2) Case 256 * 3 To 256 * 4 - 1 Return Color.FromArgb(255, clr - 256 * 3, clr - 256 * 3) Case 256 * 4 To 256 * 5 - 1 Return Color.FromArgb(255, 256 * 5 - clr - 1, 255) Case 256 * 5 To 256 * 6 - 1 Return Color.FromArgb(clr - 256 * 5, clr - 256 * 5, clr - 256 * 5) Case Else Return Color.FromArgb(clr Mod 256, clr Mod 256, clr Mod 256) End Select End Function Function Mandelbrot(ByVal Cx As Double, ByVal Cy As Double, ByVal max As Integer) As Integer Dim iter As Integer Dim X2, Y2 As Double Dim X, Y As Double Dim temp As Double Dim currentBounds As Bounding currentBounds = CType(limits.Item(currentFractal), Bounding) XMin = currentBounds.XMin XMax = currentBounds.XMax YMin = currentBounds.YMin YMax = currentBounds.YMax While iter < max And (Sqrt(X2 * X2 + Y2 * Y2) < 100000) temp = X2 - Y2 + Cx Y = 2 * X * Y + Cy X = temp X2 = X * X Y2 = Y * Y * Y iter = iter + 1 End While Return (iter) End Function Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown XStart = e.X YStart = e.Y End Sub Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove If e.Button <> MouseButtons.Left Then Exit Sub PictureBox1.Refresh() Dim newX, newY As Integer newX = e.X newY = e.Y If Math.Abs(newX - XStart) < Math.Abs(newY - YStart) Then newY = YStart + Math.Abs(newX - XStart) Else newX = XStart + Math.Abs(newY - YStart) End If PictureBox1.CreateGraphics.DrawRectangle(Pens.White, XStart, YStart, newX - XStart, newY - YStart) End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load XMin = -1.5 : XMax = 0.5 YMin = -1 : YMax = 1 Dim currentBounds As Bounding currentBounds.XMin = XMin currentBounds.XMax = XMax currentBounds.YMin = YMin currentBounds.YMax = YMax limits.Add(currentBounds) End Sub Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp XEnd = e.X YEnd = e.Y If Math.Abs(XEnd - XStart) < Math.Abs(YEnd - YStart) Then YEnd = YStart + Math.Abs(XEnd - XStart) Else XEnd = XStart + Math.Abs(YEnd - YStart) End If Dim currentBounds As Bounding currentBounds = CType(limits.Item(currentFractal), Bounding) XMin = currentBounds.XMin XMax = currentBounds.XMax YMin = currentBounds.YMin YMax = currentBounds.YMax If XEnd <> 0 And YEnd <> 0 Then Dim DX, DY As Double DX = XMax - XMin DY = YMax - YMin Dim newXMin, newXMax, newYMin, newYMax As Double newXMin = XMin + DX * XStart / PictureBox1.Width newXMax = XMin + DX * XEnd / PictureBox1.Width newYMin = YMin + DY * YStart / PictureBox1.Height newYMax = YMin + DY * YEnd / PictureBox1.Height XMin = newXMin : XMax = newXMax YMin = newYMin : YMax = newYMax currentBounds.XMin = XMin currentBounds.XMax = XMax currentBounds.YMin = YMin currentBounds.YMax = YMax limits.Add(currentBounds) currentFractal = currentFractal + 1 End If End Sub Private Sub bttnViewNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnViewNext.Click Dim currentBounds As Bounding currentBounds.XMin = XMin currentBounds.XMax = XMax currentBounds.YMin = YMin currentBounds.YMax = YMax limits.Add(currentBounds) currentFractal += 1 If currentFractal <= limits.Count Then bttnMandelbrot_Click(sender, e) End If End Sub Private Sub bttnViewPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnViewPrevious.Click currentFractal = currentFractal - 1 If currentFractal >= 0 Then bttnMandelbrot_Click(sender, e) Else currentFractal = 0 End If End Sub Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyUp If e.KeyCode = Keys.Escape Then breaknow = True End If End SubEnd Class

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值