I want to get a random point in the red circle, and escaping the blue circle :
I made this:
double r = radius * Math.sqrt(R.nextDouble());
double theta = R.nextDouble() * 2 * Math.PI;
int x = (int) (r * Math.cos(theta));
int z = (int) (r * Math.sin(theta));
But it only find in red circle.
So, I tried to add something when X or Z are below the blue's circle radius, but I get this result :
I also tried to upgrade X or Z when distance between the random point and the center (which is 0/0) are too near.
I also made this snippet which test lot of X/Z position, but it will try everytimes. It's clearly not optimized because I have to filter all generated values, instead of directly generate values in required range.
function getNumberIn(min, max) {
return Math.random() * (max - min) + min;
}
var canvas = document.getElementById("circlecanvas");
var context = canvas.getContext("2d");
context.beginPath();
context.arc(150, 150, 150, 0, Math.PI * 2, false);
context.fillStyle = "yellow";
context.fill();
context.closePath();
context.beginPath();
context.arc(150, 150, 50, 0, Math.PI * 2, false);
context.fillStyle = "red";
context.fill();
context.closePath();
context.fillStyle = "black";
let nb = 0;
for (let i = 0; i < 1000; i++) {
let x = getNumberIn(0, 300);
let z = getNumberIn(0, 300);
let distance = Math.sqrt((x - 150) * (x - 150) + (z - 150) * (z - 150));
if ((distance >= 50 || distance <= -50) && !(distance >= 150 || distance <= -150)) {
context.fillRect(x, z, 3, 3);
} else {
nb++;
}
}
console.log("Amount of skipped values: " + nb);
<canvas id="circlecanvas" width="300" height="300"></canvas>
How can I get random point in the red circle, excluding the blue one ?