Set the loc of graphic to random point on a circle
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
Set the loc of graphic to random point on a circle
Hi,
I'm having difficulty working out how to set the location of a small grc "Oval" to a random point on the circumference of a large circle.
Any advice on the best way to go about doing this?
Thanks
I'm having difficulty working out how to set the location of a small grc "Oval" to a random point on the circumference of a large circle.
Any advice on the best way to go about doing this?
Thanks
Re: Set the loc of graphic to random point on a circle
Hi.
Unfortunately, polygon and oval graphics do not have distinct "points" properties, or the task would be trivial.
Is your graphic oval a circle? If so, you know the loc and 1/2 the width, and you have enough information to find any point, using x^2 + y^2 = r^2
So if half your width = r, then you can find any point on the circle and set the loc of your other graphic accordingly. For example, if you have an oval with a width of 100, then r = 50.
You can now write a handler that takes successive values. of, say, y, and find the corresponding value of x:
0^2 + y^2 = 2500 (x=0, y=50)
1^2 + y^2 = 2500 (x=1, y= 50) You need to do a bit of rounding throughout all this to make sure you end up with the nearest integer
...
20^2 + y^2 = 2500 (x=20, y=46)
You can run this and make a table of "points". Remember that the offset of the loc of the base oval has to be taken into account, and added to the resultant x and y values. With that list, you can set the loc of your other graphic to any line of that list.
Of course, you can do this the other way,and set the loc of that graphic to a random value of y, say, (within the limits of the value of r) and calculate directly on a case by case basis.
Craig Newman
Unfortunately, polygon and oval graphics do not have distinct "points" properties, or the task would be trivial.
Is your graphic oval a circle? If so, you know the loc and 1/2 the width, and you have enough information to find any point, using x^2 + y^2 = r^2
So if half your width = r, then you can find any point on the circle and set the loc of your other graphic accordingly. For example, if you have an oval with a width of 100, then r = 50.
You can now write a handler that takes successive values. of, say, y, and find the corresponding value of x:
0^2 + y^2 = 2500 (x=0, y=50)
1^2 + y^2 = 2500 (x=1, y= 50) You need to do a bit of rounding throughout all this to make sure you end up with the nearest integer
...
20^2 + y^2 = 2500 (x=20, y=46)
You can run this and make a table of "points". Remember that the offset of the loc of the base oval has to be taken into account, and added to the resultant x and y values. With that list, you can set the loc of your other graphic to any line of that list.
Of course, you can do this the other way,and set the loc of that graphic to a random value of y, say, (within the limits of the value of r) and calculate directly on a case by case basis.
Craig Newman
Re: Set the loc of graphic to random point on a circle
I've used the following function in a few game stacks;
So if the centre of the large circle is in a variable 'tOrigin' with radius 100 pixels (tDistance) and the angle required is 45 degrees (tAngle);

Code: Select all
# calculate the new point which is a fixed distance and angle from the starting point
# 0 degrees is straight up and angles are measured clockwise
function calculatePoint pPoint, pDist, pAngle, pRound
put item 1 of pPoint into tX
put item 2 of pPoint into tY
put pAngle * PI / 180 into pAngle
add sin(pAngle) * pDist to tX
subtract cos(pAngle) * pDist from tY
if pRound then return round(tX) & comma & round(tY) else return tX & comma & tY
end calculatePoint
Code: Select all
set the loc of graphic "SmallOval" to calculatePoint(tOrigin, tDistance, tAngle)

LiveCode Development & Training : http://splash21.com
Re: Set the loc of graphic to random point on a circle
Hi Splash21
Your code is great, and by far the closest to what I am looking for for my program.
Currently your code returns the point (or rather places a small circle at the point) of a specific angle of a circle.
In my program I have the point but need to know the angle, i.e. people click on a certain part of a circle and I record the coordinates of where they clicked but need to know this info as an angle.
Do you think your code can be rejigged to provide this? ...My programing abilities are very basic...
Cheers,
Kelly
Your code is great, and by far the closest to what I am looking for for my program.
Currently your code returns the point (or rather places a small circle at the point) of a specific angle of a circle.
In my program I have the point but need to know the angle, i.e. people click on a certain part of a circle and I record the coordinates of where they clicked but need to know this info as an angle.
Do you think your code can be rejigged to provide this? ...My programing abilities are very basic...
Cheers,
Kelly
Re: Set the loc of graphic to random point on a circle
Hi, Kelly. No need to rejig - I just happen to have another function for angles that will hopefully do the trick 
Just supply a couple of points and the angle is calculated.

Code: Select all
# calculate the angle between 2 points
# 0 degrees is straight up and angles are measured clockwise
function calculateAngle pPoint1, pPoint2
put item 1 of pPoint2 - item 1 of pPoint1 into tX
put item 2 of pPoint2 - item 2 of pPoint1 into tY
if tX = 0 or tY = 0 then
if tX > 0 then
return 90
else if tX < 0 then
return 270
else if tY > 0 then
return 180
else
return 0
end if
end if
put 90 + atan2(tY, tX) * 180 / PI into tResult
if tResult < 0 then add 360 to tResult
return tResult
end calculateAngle
LiveCode Development & Training : http://splash21.com
Re: Set the loc of graphic to random point on a circle
Thank you so much!!
This problem has been bugging me for longer than I want to admit and your code seems to be working perfectly!
This problem has been bugging me for longer than I want to admit and your code seems to be working perfectly!