So you want your turtle to travel the perimeter of the circle? (I'm going to assume yes.)
You will want to rearrange the equation to make
y the subject of the equation. Your turtle will also need to know its starting position in relation to the circle and the circle's radius. Using the rearranged equation the turtle should be able to generate a list of points which make the circle (by plugging in values for
x). It will then need to visit each of these points. You may want to use the
GPS API.
I have not tested this code
--# y = k ±sqrt(r^2 -(x -h)^2)
--# this function will perform the above calculation
--# the turtle will need to do this for multiple x values
--# due to the nature of squareroots, this functuon returns two values, the turtle will need to visit both of
--# these y values for the given x value (look at a circle on a graph if this confuses you)
--# one of the values in this function (marked) can be cached for a single circle, I am leaving this as an
--# exercise to the reader
--# some implementers may want to sacrifice readability for a performace increase
local function xToY(x,h,k,r)
local rSq = r*r --# cache me!
local xMinusHSq = (x-h)*(x-h) --# don't cache me, x changes every time
local sqrtPart = sqrt(rSq - xMinusHSq) --# optimisation advice: keep this part way
--# calculation variable, the sqrt function is slow
local kPlusSqrtPart = k + sqrtPart
local kMinusSqrtPart = k - sqrtPart
return kPlusSqrtPart, kMinusSqrtPart
end
--# using this function to map x coordinates to the corresponding y pair
--# I'm assuming that the turtle has (or will have) values for h, k, and r
local coords = {}
for x = h-r, h+r do
coords[x] = { xToY(x, h, k, r)}
--# the turtle may need to round the y values, so I'm going to floor all of them
coords[x][1] = math.floor(coords[x][1])
coords[x][2] = math.floor(coords[x][2])
end
--# using the resulting table to have the turtle walk the points
--# for my own sanity I will assume the turtle has a goto function which takes the shortist
--# route to a point (the points should be close enough)
--# this does one half of the circle
for x = h-r, h+r do
goto(x, coords[x][1])
end
--# do the other half of the circle
for x = h+r, h-r, -1 do --# inverted paramiters and -1 to do this half 'backwards' compared to the
--# other half, since the turtle will be on that side
goto(x, coords[x][2])
end