This site uses strictly necessary cookies. More Information

X- Home /

# How to map square coordinates to a circle?

Hey.

I have a square 2D minimap. I want to make a circular version of that minimap, so I want to map the original square minimap's X and Y coordinates onto an inscribed circle.

How does one map a square XY coordinate onto a circle of equal dimensions? The original square coordinates are stored in a Vector2 "pos", and the new circle coordinates are stored in a Vector2 "newPos".

I had tried the following function. But I must have made some error, as it's returning undefined numbers instead of proper coordinates.

```
public Vector2 squareToCirclePosition(Vector2 pos)
{
Vector2 newPos = new Vector2(0,0);
newPos.x = pos.x * Mathf.Sqrt(1 - ((Mathf.Pow(pos.y, 2F)) / 2));
newPos.y = pos.y * Mathf.Sqrt(1 - ((Mathf.Pow(pos.x, 2F)) / 2));
...
return newPos;
}
```

Any ideas here? Sorry if I'm clueless at math. And scripting.

Thanks!

**Answer** by Eno-Khaon
·
Apr 03, 2016 at 12:12 AM

There's one main, basic problem here: **A square does not have equal dimensions to a circle.**

That won't prevent you from displaying your minimap in a circular area, but you may want to ask yourself a question about what you're looking to do:

**Are you sure you want to convert these distances in this manner?** If you try and make use of the entire area of a square, compressed into the shape of a circle, anything not exactly on a cardinal direction on your minimap will be at a skewed relative location because rather than a *1:1* ratio of object position to map position, it will be at a *1:sqrt(x*x+y*y)* ratio, depending on its position around the circle.

It may be more prudent to simply keep track of your minimap in the shape of a circle exclusively, instead. This would provide a (potentially) equidistant range on your minimap without favoring diagonals (not counting a square/rectangular minimap where that's intended) by basing what's displayed on it purely on its distance from the player.

If you do wish to proceed with remapping a square to a circle as-is, that is done by dividing the position of the object displayed by the maximum length to reach the edge of the minimap (i.e. an enemy at position (0.2, 0.4) on a -1 to 1 range minimap remaps to (0.1789, 0.3578) by dividing both side by 1.1180, the length of the line found by dividing 1 by the greater of the x or y value)

If you're absolutely sure you want to do this, remapping the vectors would be done as follows:

```
Vector2 SquareToCircle(Vector2 input)
{
if(input.sqrMagnitude == 0)
{
return Vector2.zero; // Avoid division by 0
}
Vector2 output;
float multiplier = 1.0f / Mathf.Max(input.x, input.y);
output = input * multiplier; // Extend the line to the edge of the square
float divisor = Mathf.Sqrt((output.x * output.x) + (output.y * output.y));
output = input / divisor; // Contract the original point away from the circle's edge
return output;
}
```

Edit: The simplest alternative, if you simply wish to use a circular shape, is to assemble the square map, but then cull contents outside the circular shape.

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.