forked from twobitcoder101/Flat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RandomHelper.cs
164 lines (131 loc) · 4.38 KB
/
RandomHelper.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
using System;
using Microsoft.Xna.Framework;
namespace Flat
{
public static class RandomHelper
{
private static Random Rand = new Random();
public static int RandomInteger()
{
return Rand.Next();
}
public static int RandomInteger(Random rand)
{
return rand.Next();
}
public static int RandomInteger(int min, int max)
{
if(min == max)
{
return min;
}
if (min > max)
{
FlatUtil.Swap(ref min, ref max);
}
int result = min + Rand.Next() % (max - min);
return result;
}
public static int RandomInteger(Random rand, int min, int max)
{
if (min > max)
{
FlatUtil.Swap(ref min, ref max);
}
int result = min + rand.Next() % (max - min);
return result;
}
public static bool RandomBooleon()
{
int value = RandomHelper.RandomInteger(0, 2);
if(value == 0)
{
return false;
}
else
{
return true;
}
}
public static float RandomSingle()
{
return (float)Rand.NextDouble();
}
public static float RandomSingle(Random rand)
{
return (float)rand.NextDouble();
}
public static float RandomSingle(float min, float max)
{
if (min > max)
{
FlatUtil.Swap(ref min, ref max);
}
float result = min + (float)Rand.NextDouble() * (max - min);
return result;
}
public static float RandomSingle(Random rand, float min, float max)
{
if (min > max)
{
FlatUtil.Swap(ref min, ref max);
}
float result = min + (float)rand.NextDouble() * (max - min);
return result;
}
public static Color RandomColor()
{
Color result = new Color((float)Rand.NextDouble(), (float)Rand.NextDouble(), (float)Rand.NextDouble());
return result;
}
public static Color RandomColor(Random rand)
{
Color result = new Color((float)rand.NextDouble(), (float)rand.NextDouble(), (float)rand.NextDouble());
return result;
}
public static Color RandomColor(float brightness)
{
// https://www.nbdtech.com/Blog/archive/2008/04/27/Calculating-the-Perceived-Brightness-of-a-Color.aspx
// brightness = sqrt( .241 R^2 + .691 G^2 + .068 B^2 )
brightness = FlatMath.Clamp(brightness, 0f, 1f);
float r = RandomHelper.RandomSingle(0f, 1f);
float g = RandomHelper.RandomSingle(0f, 1f);
float b = RandomHelper.RandomSingle(0f, 1f);
float dec = 0.98f;
float inc = 1f / dec;
for(int i = 0; i < 64; i++)
{
float perceivedBrightness = FlatUtil.PercievedBrightness(r, g, b);
if(perceivedBrightness < brightness)
{
r *= inc;
g *= inc;
b *= inc;
}
else if(perceivedBrightness > brightness)
{
r *= dec;
g *= dec;
b *= dec;
}
dec += 0.0001f;
inc -= 0.0001f;
if(dec > 1f) { dec = 1f; }
if(inc < 1f) { inc = 1f; }
}
return new Color(r, g, b);
}
public static Vector2 RandomDirection()
{
float angle = RandomSingle(0, MathHelper.TwoPi);
Vector2 result = new Vector2(MathF.Cos(angle), MathF.Sin(angle));
return result;
}
public static Vector2 RandomDirection(Random rand)
{
float angle = RandomSingle(rand, 0, MathHelper.TwoPi);
Vector2 result = new Vector2(MathF.Cos(angle), MathF.Sin(angle));
return result;
}
}
}