Home c# Arrangement of objects by a mathematical function in a loop (optimization)

# Arrangement of objects by a mathematical function in a loop (optimization)

Author

Date

Category

Good day.
I have some list of objects, and they need to be located in a semicircle relative to a certain center: The number of objects is different all the time and varies from 0 to 5. Points of location of objects are indicated by green circles; the order is from right to left.
And, in principle, I understand what conditions need to be checked in order to set a position for an object. The question is: how to optimize?

``````for (int i = 0; i & lt; gameObjects.Count; i ++)
{
while (x! = x_center || y! = y_center || x! = x_center + deltaX || x! = x_center - deltaX)
{
x - = 0.01f;
y = y_center + (float) Math.Sqrt (radius * radius - x * x);
}
gameObjects [i] .transform.position = new Vector3 (x, 0, y);
x - = 0.01f;
y = y_center + Mathf.Sqrt (radius * radius - x * x);
if (x & lt; (x_center - radius)) {break; }
}
``````

The program hangs without producing a result, which is absolutely obvious: through debugging, I saw that the while loop takes too long. What can be done? Maybe you should use Math.Round () and neglect fractional digits and precision? Or is there a way to quickly run through a math function?

In general, I realized that it is necessary to set conditions in the while loop with errors. Special thanks to Alexander, who answered me in the comments.

If you have any problems – I will write here; if not, the topic is closed.

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.