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).
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();
}
}
}