attraction = 3 # play with this value
t = random.random() * 2 * math.pi
r = random.random() ** attraction * radius # <
new_x = x_center + r * math.cos(t)
new_y = y_center + r * math.sin(t)
下面是一个JavaScript算法的交互式演示,您可以在其中使用附加参数并查看它给出了什么:
&13;
第13部分,;
let x_center = 80,
y_center = 80,
radius = 80;
function refresh(attraction=1) {
clear();
// Plot 10^4 random points in a disc
for (let i = 0; i < 10000; i++) {
let t = Math.random() * 2 * Math.PI
let r = Math.random() ** attraction * radius
let new_x = x_center + r * Math.cos(t)
let new_y = y_center + r * Math.sin(t)
plot(new_x, new_y);
}
}
// I/O handling (would be different in Python)
let input = document.querySelector("input");
let canvas = document.querySelector("canvas");
let ctx = canvas.getContext("2d");
function plot(x, y) {
ctx.beginPath();
ctx.moveTo(x, y);
ctx.lineTo(x + 0.5, y + 0.5);
ctx.stroke();
}
function clear() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
}
input.addEventListener("input", () => refresh(input.value));
refresh(input.value);
您的算法已经在中心产生了更高的密度
您可以通过将
random.random()
提高到可变幂来影响密度,并使用它来定义r
例如,你可以这样做:
下面是一个JavaScript算法的交互式演示,您可以在其中使用附加参数并查看它给出了什么: