A
AW
Simple(?) program to draw random lines in a rectangle but when I
discard all random lines longer than size and graph the results I get
a set of diagonal bands. How can random lines have a pattern? Can
anyone explain? When I discard a line, I generate a new random line
until is it short enough - so how can it not be random?
Win XP & VB 2008 Express. Thanks.
--------------------------
Imports System
Imports System.Drawing
Imports Microsoft.VisualBasic
Imports Microsoft.VisualBasic.VBMath
Imports Microsoft.VisualBasic.PowerPacks
Public Class Form1
Dim rec As New RectangleShape
Dim arx(5000, 2) As Integer
Dim ary(5000, 2) As Integer
Public Sub New()
InitializeComponent()
Randomize()
Me.rec.Left = 300
Me.rec.Top = 2
Me.rec.Width = 900
Me.rec.Height = 900
For n = 0 To 4999
arx(n, 0) = arx(n, 1) = ary(n, 0) = ary(n, 1) = 0
Next
End Sub
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As
System.Windows.Forms.PaintEventArgs) Handles Me.Paint
PGr()
End Sub
Private Sub PGr()
Dim e As Graphics
e = Me.CreateGraphics
e.FillRectangle(Brushes.White, rec.Left, rec.Top, rec.Width,
rec.Height)
For n = 0 To 4999
e.DrawLine(Pens.Black, rec.Left + arx(n, 0), rec.Top +
ary(n, 0), _
rec.Left + arx(n, 1), rec.Top + ary(n, 1))
Next
End Sub
Private Sub ReP_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles ReP.Click
Dim Dret As Integer = 0
For n = 0 To 4999
Do
arx(n, 0) = Int(Rnd() * 900)
arx(n, 1) = Int(Rnd() * 900)
ary(n, 0) = Int(Rnd() * 900)
ary(n, 1) = Int(Rnd() * 900)
Dret = Dist(n)
Loop Until Dret = 1
Next
PGr()
End Sub
Private Function Dist(ByVal n As Integer) As Integer
Dim x1, x2, y1, y2, h, h1, h2, size As Double
size = 40
x1 = arx(n, 0)
x2 = arx(n, 1)
h1 = Math.Pow((x1 - x2), 2)
y1 = ary(n, 0)
y2 = ary(n, 1)
h2 = Math.Pow((y1 - y2), 2)
h = h1 + h2
h = Math.Sqrt(h)
If h > size Then Return 0
Return 1
End Function
Private Sub Quit_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Quit.Click
End
End Sub
End Class
discard all random lines longer than size and graph the results I get
a set of diagonal bands. How can random lines have a pattern? Can
anyone explain? When I discard a line, I generate a new random line
until is it short enough - so how can it not be random?
Win XP & VB 2008 Express. Thanks.
--------------------------
Imports System
Imports System.Drawing
Imports Microsoft.VisualBasic
Imports Microsoft.VisualBasic.VBMath
Imports Microsoft.VisualBasic.PowerPacks
Public Class Form1
Dim rec As New RectangleShape
Dim arx(5000, 2) As Integer
Dim ary(5000, 2) As Integer
Public Sub New()
InitializeComponent()
Randomize()
Me.rec.Left = 300
Me.rec.Top = 2
Me.rec.Width = 900
Me.rec.Height = 900
For n = 0 To 4999
arx(n, 0) = arx(n, 1) = ary(n, 0) = ary(n, 1) = 0
Next
End Sub
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As
System.Windows.Forms.PaintEventArgs) Handles Me.Paint
PGr()
End Sub
Private Sub PGr()
Dim e As Graphics
e = Me.CreateGraphics
e.FillRectangle(Brushes.White, rec.Left, rec.Top, rec.Width,
rec.Height)
For n = 0 To 4999
e.DrawLine(Pens.Black, rec.Left + arx(n, 0), rec.Top +
ary(n, 0), _
rec.Left + arx(n, 1), rec.Top + ary(n, 1))
Next
End Sub
Private Sub ReP_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles ReP.Click
Dim Dret As Integer = 0
For n = 0 To 4999
Do
arx(n, 0) = Int(Rnd() * 900)
arx(n, 1) = Int(Rnd() * 900)
ary(n, 0) = Int(Rnd() * 900)
ary(n, 1) = Int(Rnd() * 900)
Dret = Dist(n)
Loop Until Dret = 1
Next
PGr()
End Sub
Private Function Dist(ByVal n As Integer) As Integer
Dim x1, x2, y1, y2, h, h1, h2, size As Double
size = 40
x1 = arx(n, 0)
x2 = arx(n, 1)
h1 = Math.Pow((x1 - x2), 2)
y1 = ary(n, 0)
y2 = ary(n, 1)
h2 = Math.Pow((y1 - y2), 2)
h = h1 + h2
h = Math.Sqrt(h)
If h > size Then Return 0
Return 1
End Function
Private Sub Quit_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Quit.Click
End
End Sub
End Class