-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRiver.cs
66 lines (56 loc) · 1.43 KB
/
River.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
using System;
using System.Drawing;
using System.Collections.Generic;
using OpenTK;
namespace terrain
{
public class RiverTest
{
int w = 512;
int h = 512;
Bitmap b;
public void Draw ()
{
b = new Bitmap(512,512);
for (int x = 0; x < w; x++)
for (int y = 0; y < h; y++)
b.SetPixel(x,y,Color.Black);
DrawRiver (new Vector2(10,10), new Vector2 (1,1));
b.Save ("river.png");
}
public void DrawRiver(Vector2 p, Vector2 direction)
{
//keep going until we are off the screen.
bool going = true;
Vector2 currentDirection = direction;
Random r = new Random ();
int length = 4000;
int xt = 0;
int yt = 0;
while (length-- > 0)
{
float xFactor = ((float)r.NextDouble () - 0.5f);
float yFactor = ((float)r.NextDouble () - 0.5f);
if ( r.Next(8) == 0 )
currentDirection = Approximate (currentDirection, direction, 0.2f);
else
currentDirection = Vector2.Add (currentDirection, new Vector2 (xFactor,yFactor));
currentDirection.Normalize ();
for (int i = 0; i < 5; i++)
{
p = Vector2.Add(p,currentDirection);
if (!(p.X > w || p.Y > h || p.X < 0 || p.Y < 0))
b.SetPixel((int)p.X,(int)p.Y,Color.White);
}
}
}
public Vector2 Approximate (Vector2 v, Vector2 target, float dist)
{
if (v.X < target.X) v.X += dist;
else v.X -= dist;
if (v.Y < target.Y) v.Y += dist;
else v.Y -= dist;
return v;
}
}
}