-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstars.js
104 lines (98 loc) · 2.29 KB
/
stars.js
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
function colorTemperatureToRGB(kelvin) {
var temp = kelvin / 100;
var red, green, blue;
if (temp <= 66) {
red = 255;
green = temp;
green = 99.4708025861 * Math.log(green) - 161.1195681661;
if (temp <= 19) {
blue = 0;
} else {
blue = temp - 10;
blue = 138.5177312231 * Math.log(blue) - 305.0447927307;
}
} else {
red = temp - 60;
red = 329.698727446 * Math.pow(red, -0.1332047592);
green = temp - 60;
green = 288.1221695283 * Math.pow(green, -0.0755148492);
blue = 255;
}
return [clamp(red, 0, 255), clamp(green, 0, 255), clamp(blue, 0, 255)];
}
function clamp(x, min, max) {
if (x < min) {
return min;
} else if (x > max) {
return max;
}
return x;
}
const types = [
{
type: "O",
temperature: 40000,
mass: 50 * MASS_SUN,
radius: 10 * RADIUS_SUN,
prob: 0.00001,
},
{
type: "B",
temperature: 20000,
mass: 10 * MASS_SUN,
radius: 5 * RADIUS_SUN,
prob: 0.1,
},
{
type: "A",
temperature: 8500,
mass: 2 * MASS_SUN,
radius: 1.7 * RADIUS_SUN,
prob: 0.7,
},
{
type: "F",
temperature: 6500,
mass: 1.5 * MASS_SUN,
radius: 1.3 * RADIUS_SUN,
prob: 2,
},
{
type: "G",
temperature: 5700,
mass: MASS_SUN,
radius: RADIUS_SUN,
prob: 3.5,
},
{
type: "K",
temperature: 4500,
mass: 0.7 * MASS_SUN,
radius: 0.8 * RADIUS_SUN,
prob: 8,
},
{
type: "M",
temperature: 3200,
mass: 0.2 * MASS_SUN,
radius: 0.3 * RADIUS_SUN,
prob: 80,
},
];
const types_name = types.map((a) => a.type);
const total_prob = types.reduce((a, b) => a + b.prob, 0);
function generateStar(type) {
let star = types.find((star) => star.type === type);
star.color = color(...colorTemperatureToRGB(star.temperature));
return star;
}
function sampleStar() {
const pick = random(0, total_prob);
let acc = 0;
for (let i = 0; i < types.length; i++) {
acc += types[i].prob;
if (acc > pick) {
return types[i].type;
}
}
}