This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
thecheesaman's profile picture

Turtle movement according to an equation

Started by thecheesaman, 07 October 2017 - 03:50 PM
thecheesaman #1
Posted 07 October 2017 - 05:50 PM
Hello.

Sorry if this is a question that has already been dealt with but I can't seem to find an example. I want to make a turtle move according to an equation say (for example) the circle equation of
(x – h)2 + (y – k)2 = r2

(where r is the radius an (h,k) is the centre)

Is there an easy way to do that or is it many many lines of code?
Lupus590 #2
Posted 08 October 2017 - 07:06 PM
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
Edited on 08 October 2017 - 06:30 PM