> For the complete documentation index, see [llms.txt](https://docs.flatredball.com/flatredball/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.flatredball.com/flatredball/api/flatredball/math/geometry/polygon/optimizeradius.md).

# OptimizeRadius

### Introduction

The OptimizeRadius method can be called to adjust the relative points of a Polygon to reduce its radius. The radius of a polygon is used internally to improve the performance of collision methods.

### Usage

The OptimizeRadius method should be called if a Polygon's points are set or changed. This method will adjust the values of each relative point to optimize the radius of the Polygon. This method will also modify the position of the Polygon to keep the absolute position of each vertex constant. In other words, this method can be called **after** Polygons are positioned and it should have no impact on the final appearance or behavior of the Polygon - aside from improved performance.

### Code Example

The following code creates two Polygons. The Points are assigned such that the Polygon is not symmetric about its center. The Polygon called optimizedPolygon has its OptimizeRadius method called to make it symmetric. Finally, two circles are created to show the position and radius of both Polygons.

Add the following using statement:

```
using FlatRedBall.Math.Geometry;
```

Add the following to Initialize after initializing FlatRedBall:

```
FlatRedBall.Math.Geometry.Point[] points = new FlatRedBall.Math.Geometry.Point[5];

points[0] = new FlatRedBall.Math.Geometry.Point(0, 5);
points[1] = new FlatRedBall.Math.Geometry.Point(10, 5);
points[2] = new FlatRedBall.Math.Geometry.Point(10, -5);
points[3] = new FlatRedBall.Math.Geometry.Point(0, -5);
points[4] = points[0]; // close the shape

Polygon optimizedPolygon = new Polygon();
optimizedPolygon.Points = points;
optimizedPolygon.Visible = true;

Polygon unoptimizedPolygon = new Polygon();
unoptimizedPolygon.Points = points;
unoptimizedPolygon.Visible = true;

// Separate the Polygons
optimizedPolygon.X = -16;
unoptimizedPolygon.X = 11;

optimizedPolygon.OptimizeRadius();

// Let's show the result:
Circle optimizedRadius = new Circle();
optimizedRadius.Visible = true;
optimizedRadius.Position = optimizedPolygon.Position;
optimizedRadius.Radius = optimizedPolygon.BoundingRadius;

Circle unoptimizedRadius = new Circle();
unoptimizedRadius.Visible = true;
unoptimizedRadius.Position = unoptimizedPolygon.Position;
unoptimizedRadius.Radius = unoptimizedPolygon.BoundingRadius;
```

![OptimizeRadius.png](https://951240982-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M_fye9Ufg3vzJxwX5Hk%2Fuploads%2Fgit-blob-5d4b06f21e539721401978d893e7e701d2f0a9b4%2Fmigrated_media-OptimizeRadius.png?alt=media)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.flatredball.com/flatredball/api/flatredball/math/geometry/polygon/optimizeradius.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
