Circular Algorithm

Share your scripts and packages with other Mudlet users.
Post Reply
User avatar
Akaya
Posts: 412
Joined: Thu Apr 19, 2012 1:36 am

Circular Algorithm

Post by Akaya » Mon Jul 22, 2013 2:22 am

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

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest