Set the loc of graphic to random point on a circle

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
calmrr3
Posts: 100
Joined: Mon Oct 28, 2013 2:39 pm

Set the loc of graphic to random point on a circle

Post by calmrr3 » Wed Nov 27, 2013 5:24 pm

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

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10332
Joined: Wed May 06, 2009 2:28 pm

Re: Set the loc of graphic to random point on a circle

Post by dunbarx » Wed Nov 27, 2013 6:09 pm

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

splash21
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 369
Joined: Sun Dec 19, 2010 1:10 am
Contact:

Re: Set the loc of graphic to random point on a circle

Post by splash21 » Thu Nov 28, 2013 10:37 am

I've used the following function in a few game stacks;

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
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

set the loc of graphic "SmallOval" to calculatePoint(tOrigin, tDistance, tAngle)
:lol:
LiveCode Development & Training : http://splash21.com

kjon2010
Posts: 5
Joined: Tue Jan 07, 2014 12:46 am

Re: Set the loc of graphic to random point on a circle

Post by kjon2010 » Tue Jan 07, 2014 12:53 am

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

splash21
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 369
Joined: Sun Dec 19, 2010 1:10 am
Contact:

Re: Set the loc of graphic to random point on a circle

Post by splash21 » Tue Jan 07, 2014 1:28 am

Hi, Kelly. No need to rejig - I just happen to have another function for angles that will hopefully do the trick ;)

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
Just supply a couple of points and the angle is calculated.
LiveCode Development & Training : http://splash21.com

kjon2010
Posts: 5
Joined: Tue Jan 07, 2014 12:46 am

Re: Set the loc of graphic to random point on a circle

Post by kjon2010 » Thu Jan 09, 2014 5:51 am

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!

Post Reply