- Home /
Random Coordinates Outside a Circle
Hi all,
I understand that there's a Random.insideUnitCircle variable to get a random 2D coordinate inside a theoretical circle.
But what if I wanted to get a random 2D coordinate outside of that theoretical circle? Any ideas on how to go about that?
Thanks in advance,
Dendro
I've answered assu$$anonymous$$g that you know the $$anonymous$$/max bounds of the screen (or positions that are valid for your random 2D coordinate). If this is not the case, please update your question with further details. Cheers.
Answer by · Sep 15, 2010 at 06:33 AM
Broadly, this is what I would do:
- Randomise a position within the min/max bounds
- Check distance from the centrepoint to the randomised position
- If it's less than a desired radius, recalculate the random position
Be aware that sqrMagnitude is much faster than Vector2.magnitude. To use this, you will then obviously need to square the radius value. For example:
if ( (randomPos.position - centrePoint.position).sqrMagnitude <= radius*radius )
{ // retry }
Thanks $$anonymous$$arowi. I'm not entirely sure on how to 'reply to an answer', and I hope I'm doing it right. I'll try out what you've suggested. Just a thought, though, isn't the logic you are suggesting quite similar to a brute-force solution? Thanks again.
Comments are perfect for replying! It is fairly brute-force, but to my knowledge there's no built-in functionality to find a point inside a rectangle and outside of a circle. Perhaps that was the answer you were after, but I thought I'd make a suggestion on how you could go about it. If you get stuck with any of the scripting, I can take a look at what you have if you'd like.
Hi $$anonymous$$arowi, really sorry for the superbly late feedback on this. Although I wasn't able to impplement your code solution, I did use the concept you had suggested and came up with a rather cruder (if there is such a word hehe..) way of generating points outside a circle. To that extent, I do believe that the concept you suggested still answered my question. Thanks again!
btw: how do you post an answer to your own question?
Hey, no worries at all. It's quite common for people to ask a question and never return, so I'm just glad you're back! There should be a button at the bottom of the page that says, "Answer Your Question". Pressing that (and then confir$$anonymous$$g the dialogue) will cause the answer box to appear. I've got some time this week, so if you'd like help with the script, feel free to update your question with what you have and I'll help where I can!