[英]Exclude points from circle in javascript
to get a random point ona circle i use this code :为了获得一个随机点ona我使用这个代码:
const position = randomCirclePoint(circleRadius, circleX, circleY);
const x = position.x;
const y = position.y;
function randomCirclePoint(circleRadius, circleX, circleY) {
let ang = Math.random() * 2 * Math.PI,
hyp = Math.sqrt(Math.random()) * circleRadius,
adj = Math.cos(ang) * hyp,
opp = Math.sin(ang) * hyp
const x = circleX + adj;
const y = circleY + opp;
return {x, y}
}
But how can I exclude some points from where the loop can take the random point ?但是我怎样才能从循环可以取随机点的地方排除一些点?
If you want to exclude certain angles from the circle, you could do something like this:如果你想从圆中排除某些角度,你可以这样做:
const randomChoice = (list) => {
return list[Math.floor(Math.random() * list.length)];
};
const randomCirclePoint = (circleRadius, circleX, circleY) => {
const ang = 2 * Math.PI * Math.random();
// for example, only in the upper half
const angUpper = Math.PI * Math.random();
// or only in the lower half
const angLower = Math.PI + Math.PI * Math.random();
// or only in quadrants 1 and 3
const angQ1Q3 = randomChoice([
(Math.PI / 2) * Math.random(),
Math.PI + (Math.PI / 2) * Math.random(),
]);
// or only in certain small slices of the circle
const angSpecial = randomChoice([
(Math.PI / 4) * Math.random(),
(Math.PI * 8) / 7 + (Math.PI / 6) * Math.random(),
(Math.PI * 2) / 3 + (Math.PI / 6) * 4 * Math.random(),
]);
const hyp = Math.sqrt(Math.random()) * circleRadius;
const adj = Math.cos(ang) * hyp;
const opp = Math.sin(ang) * hyp;
const x = circleX + adj;
const y = circleY + opp;
return { x, y };
};
console.log(randomCirclePoint(1, 0, 0));
Just change the way you generate the random angle and restrict it to a certain set of angles.只需更改生成随机角度的方式并将其限制为特定的一组角度即可。
Since we don't know your specific application, it's a bit difficult to tell what you actually want.由于我们不知道您的具体应用程序,因此很难说出您真正想要什么。 Hopefully this helps.希望这会有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.