Convert point on circle to point on square

Hello everyone!

I need some help with figuring out how I can convert a point on a circle to a point on a square.

This is what I have right now:

local function convCircSquare(beginning, radius, theta)
   	local u = (radius * cos(theta))
	local v = (radius * sin(theta))
    local usq = u * u;
    local vsq = v * v;
    local fu = twoSqrt2 * u;
    local fv = twoSqrt2 * v;
    local x =
      0.5 * sqrt(2 + fu + usq - vsq) - 0.5 * sqrt(2 - fu + usq - vsq);
    local z =
      0.5 * sqrt(2 + fv - usq + vsq) - 0.5 * sqrt(2 - fv - usq + vsq);
	x = tostring(x) == "-nan(ind)" and 0 or x
	z = tostring(z) == "-nan(ind)" and 0 or z
	x = beginning.X + x
	z = beginning.Z + z
    return Vector3.new(x,beginning.Y,z)
end

But it doesn’t work correctly.
If anyone has any ideas on how this could be done it would be highly appreciated :smiley:

2 Likes

What do you mean? Do you mean getting the coordinates of a point on a circle given a certain radius and angle? Can you explain what you’re trying to accomplish?

1 Like


Sure!
On the above photo, you can see on the left we have a circle and on the right a square. I want to make the circle into a square and find the same point on the square as on the circle (Vector3). In this case that could be the red dot that i drew.

2 Likes

You could try this:

1 Like

Thank you for your idea! We are getting closer now, but this only creates half a square as seen here:
image
And this was the resulting code from your link.

local function convCircSquare(beginning, radius, theta)
	local x = cos(theta)
	local z = sin(theta)
	local M = math.max(math.abs(x),math.abs(z))
	x = radius*x/M
	z = radius*z/M
	x = beginning.X + x
	z = beginning.Z + z
    return v3(x,beginning.Y,z)
end
2 Likes