I asked the evil LLM for a polar coordinates solution using a vector type. It is hard to dislike the result.
Here, with just the changes I made to fit it to my vector type rather than the one it assumed (and wrote for me) is a polar-coordinates solution. Comments are from the LLM.
-- Function to find points on a circle at a given distance from an external point
function findPointsOnCircleAtDistanceVector(circleCenter, circleRadius, externalPoint, givenDistance)
-- Vector from circle center to external point
local centerToExternal = externalPoint -(circleCenter)
-- Distance between circle center and external point
local d_ce = centerToExternal:magnitude()
-- Apply Law of Cosines to find angle `alpha`
local cos_alpha = (circleRadius^2 + d_ce^2 - givenDistance^2) / (2 * circleRadius * d_ce)
-- Check for valid solutions
if cos_alpha < -1 or cos_alpha > 1 then
return {} -- No real solutions
end
local alpha = math.acos(cos_alpha)
-- Angle of the line from circle center to external point
local theta_ce = centerToExternal:angle()
-- The two possible angles for points on the circle
local theta1 = theta_ce + alpha
local theta2 = theta_ce - alpha
-- Create vectors representing the points on the circle
local point1Direction = vector(
math.cos(theta1),
math.sin(theta1))*(circleRadius)
local point2Direction = vector(
math.cos(theta2),
math.sin(theta2))*(circleRadius)
local p1 = circleCenter+(point1Direction)
local p2 = circleCenter+(point2Direction)
return {p1, p2}
end
That’s less than 40 lines, less than half the length of the quadratic version I wrote about this morning. And it is probably no less clear, though I have not yet studied the solution to be sure that I understand it.
I’ll create a test before I close here, as I did for the other version.
function _:featurePolarCircleDistance()
_:describe("polar find points on circle at defined distance from external point", function()
_:test("provided example", function() local circleCenter = vector(0, 0)
local externalPoint = vector(10, 0)
local circleRadius = 5
local targetDistance = 7
local points = findPointsOnCircleAtDistanceVector(circleCenter, circleRadius, externalPoint, targetDistance)
for i, point in points do
_:expect(point:dist(externalPoint)).is(targetDistance)
end
end)
end)
end
So far so good, though again I want to understand the code, and to test it more thoroughly. But at this point, I think it works, and I feel very sure it would have taken me longer to create it by myself.
Very interesting. These captive AI demons are clever. I suspect that trusting them is very risky but these three little experiments have come down in their favor.
Safe paths!