Page 1 of 1

Circular Algorithm

Posted: Mon Jul 22, 2013 2:22 am
by Akaya
I wanted to make a label move in a circle but couldn't figure out the algorithm. Studied up on some basic trigonometry and wrote this nice function that can be used to place the x,y coordinates of a circle based on the center point and radius.
Code: [show] | [select all] lua
function drawCircle(cx,cy,rad,angle)
  cirCoors = {}
  cirCoors.x = {}
  cirCoors.y = {}
  local a = {} 
  local ang = angle or 360
  for i=1, ang do
    table.insert(a,math.rad(i))
  end
  for k,v in pairs(a) do
    local x = cx + math.floor(rad * math.cos(v))
    local y = cy + math.floor(rad * math.sin(v))
    table.insert(cirCoors.x,x)
    table.insert(cirCoors.y,y)
  end
end
cx and cy are the x,y coordinates of the center of the circle.
rad is the radius of the circle.
(optional) angle is the degree to which the coordinates will be grabbed. If attached to the movement of a label, this will determine the distance around the circle the label will travel.
the cirCoors table will hold your x and y coordinates in two separate tables.

Here's an example of it in use. It's being used to move the yellow orb in a circle. The video's a bit choppy and its much smoother in actuality.
http://youtu.be/OigPN7HswU8
Here's the script I used to move the yellow orb. It is attached to a UI timer and enabled after drawCircle() is ran:
Code: [show] | [select all] lua
if not CirCount then CirCount = 0 end
CirCount = CirCount + 1
if CirCount > table.maxn(cirCoors.x) then disableTimer("drawCircle") CirCount = nil return else
  cecho("\n<orange>("..cirCoors.x[CirCount]..","..cirCoors.y[CirCount]..")")
  GUI.PracOrb:move(cirCoors.x[CirCount],cirCoors.y[CirCount])
end
--edit: added the angle parameter