This site uses strictly necessary cookies. More Information

X- Home /

# Math question - Circle from n straight lines or planes, end to end

I can't seem to find this math equation on Google, the examples do not go from end to end, instead the lines cross over each other.

I am looking for the equation for n straight lines (2D), or planes (3D), that gives me the [n].transform.position and [n].transform.Eulerangels so that they make a circle end to end (meaning where one line ends, the next begins).

I'm not entirely clear what you mean regarding the lines crossing over each other.

If you had 6 points around a circle, with each point at...

```
// Example using degrees
sin(0)
sin(60)
sin(120)
sin(180)
sin(240)
sin(300)
```

... then, wouldn't the center of each be the center between each pair of points in order, the width as the distance between those two points, and (Euler angle) rotation as the mid-point between their angle inputs?

**Answer** by TreyH
·
Oct 18, 2018 at 08:27 PM

Sure, so it's pretty straightforward if you're comfortable with geometry.

Drop this into an empty scene and play around with the inspector fields to see it work with different configurations. You should see something resembling:

```
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CircleFromLines : MonoBehaviour {
public int sides = 6;
public int radius = 7;
public int z = 5;
// We'll use cubes for this
private List<GameObject> blocks;
// Update is called once per frame
void Recalculate()
{
// Create if necessary
if (this.blocks == null)
this.blocks = new List<GameObject>();
// Clear if necessary
for (int k=0; k<this.blocks.Count; k++)
GameObject.Destroy(this.blocks[k]);
// How far apart our pieces are from the center
float innerAngle = 360f / this.sides;
// Create them
for (int k=0; k<this.sides; k++)
{
// Create the block
var block = GameObject.CreatePrimitive(PrimitiveType.Cube);
// Get position and the angle values
int index = k % this.sides;
float degrees = index * innerAngle;
float radians = Mathf.Deg2Rad * degrees;
float x = this.radius * Mathf.Cos(radians);
float y = this.radius * Mathf.Sin(radians);
// Determine their length
float length = 2 * this.radius * Mathf.Abs(Mathf.Tan(Mathf.Deg2Rad * innerAngle / 2));
// Then scale, rotate, and move them. We'll keep all but one dimension small
block.transform.localScale = new Vector3(0.2f, length, 0.2f);
block.transform.rotation = Quaternion.Euler(0, 0, degrees);
block.transform.position = new Vector3(x, y, this.z);
this.blocks.Add(block);
}
}
void OnGUI()
{
if (GUI.Button(new Rect(0, 0, 100, 50), "Recalculate"))
{
this.Recalculate();
}
}
}
```

This works great! The only issue I am having, with the blocks being 3d objects, they are connected top to bottom (on the Y plane). If I switch from a cube to a plane or a straight line, the ends are always on the X plane. When I change the localScale to (length, 1, 1) they still try to attach from top to bottom.

I think I can figure it out though, playing around with it still. Thank you so much for this code!

### 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.

### Follow this Question

### Related Questions

Collidiers intersection 1 Answer

How to draw a circle around a moving game object 1 Answer

How to build a car script that allows for car to turn in full circles 1 Answer

Oscillating variable 6 Answers

Authoritative Rotation 2 Answers