There are two ways to do this.
1. Kicking
The simplest way is to kick (px, py)
coordinates and check if they are within the desired area.
To do this, do so:
Calculate the distance of the generated point from the center (cx, cy)
:
-
d = raiz((px - cx)² + (py - cy)²)
Check that the d
distance is within the smallest and largest allowed radius ( r1
, r2
):
To check if the generated point is in the desired quadrant, verify that the coordinates are larger * than the center coordinates:
* In the case of the y
axis, considering that the original Cartesian plane, the correct one would be py
< cy
. However, generally this axis is inverted in the graphic systems that we use in the computers.
2. Calculating radius and angle
The second form is a bit more complicated at first, but will result in a point known to be within the desired area.
Calculate a random% radius between the smallest and the largest allowed radius (% with%,% with%):
Calculate an angle r1
within the desired quadrant of the circumference:
Calculate the resulting r = r1 + (r2 - r1) * rand
coordinates:
* Formula using the inverted coordinate system
Putting Scenario # 2 into practice
The script below implements method # 2 above. The calculation of the coordinate is basically what @bfavaretto implemented, but with a visual presentation of the results.
var data = {
cx: 150,
cy: 150,
r: 100,
r1: 40,
r2: 70
};
function desenharAlvo(ctx) {
//alvo completo
ctx.beginPath();
ctx.fillStyle = '#DDEEFF';
ctx.arc(data.cx, data.cy, data.r, 0, Math.PI * 2, false);
ctx.fill();
ctx.closePath();
//coordenadas
ctx.beginPath();
ctx.strokeStyle = '#000';
ctx.moveTo(data.cx, data.cy - data.r);
ctx.lineTo(data.cx, data.cy + data.r);
ctx.moveTo(data.cx - data.r, data.cy);
ctx.lineTo(data.cx + data.r, data.cy);
ctx.stroke();
ctx.closePath();
//raio menor
ctx.beginPath();
ctx.strokeStyle = '#8899AA';
ctx.arc(data.cx, data.cy, data.r1, 0, Math.PI * 2, false);
ctx.stroke();
ctx.closePath();
//raio maior
ctx.beginPath();
ctx.strokeStyle = '#8899AA';
ctx.arc(data.cx, data.cy, data.r2, 0, Math.PI * 2, false);
ctx.stroke();
ctx.closePath();
}
function desenharDardo(ctx, x, y) {
//alvo completo
ctx.beginPath();
ctx.fillStyle = '#F00';
ctx.arc(data.cx + x, data.cy + y, 1, 0, Math.PI * 2, false);
ctx.fill();
ctx.closePath();
}
function aremessarDardo() {
//passo 1
var r = data.r1 + (data.r2 - data.r1) * Math.random();
//passo 2
var a = Math.PI / 2 * Math.random();
//passo 3
var x = r * Math.cos(a);
var y = r * Math.sin(a);
desenharDardo(ctx, x, y);
}
var canvas = document.getElementById('alvo');
var ctx = canvas.getContext('2d');
desenharAlvo(ctx);
setInterval(aremessarDardo, 250);
<canvas id="alvo" width="300" height="300"></canvas>