Hey guys, I am not really that great at trig so I am wondering if someone knows the following. I want to divide a square up into pie sections to build a mesh, so I want to know how to find a point (X,Y) along the perimeter of a square given the angle and the size of the square. I attached an image to explain a bit better what I am asking. Thanks in advance for any help! Jake
Well I'm not a math expert but i'll try to give you an idea... what I can think right now is doing an intersection between two lines, the sides of the square, and the angle's diagonal. You know the "math formula" for each side of the square (as if they were infinite lines) top : y = sideLenth / 2 bottom :y = -sideLengh / 2 left x = -sideLengh / 2 right x = sideLengh / 2 *(assuming "sideLength" is the size of the square, in your example sketch it's 5) And the formula for the diagonal line using the angle, if I'm not mistaken, would be something like: y = x * Mathf.Sin(angle * Mathf.Deg2Rad) /Mathf.Cos(angle * Mathf.Deg2Rad) But you'll have to calculate it depending on the side of the square it'll intersect. For example, between -45º (also 315º) and 45º you know it's going to hit on the right side of the square (asuming 0 degrees is to the right, 45º top right, and -45º lower right). Since you know it's right side of the square yo already know the value of x: x = sideLength *.5 and y = (sideLength *.5) * Mathf.Sin(angle * Mathf.Deg2Rad) /Mathf.Cos(angle * Mathf.Deg2Rad) The logic should be the same depending on which side of the square the diagonal will intersect (top side from 45º to 135º) (left side 135º to 225º)(bottom side(225º to 315º). Keep in mind when dealing with top and bottom sides of the square, you know the 'y' is (sideLength *.5) or (-sideLength *.5), and you must figure out x which I think it would be: x = (sideLength *.5) * /Mathf.Cos(angle * Mathf.Deg2Rad) / Mathf.Sin(angle * Mathf.Deg2Rad) Hopes this provides help for the time being. But hopefully someone can provide a more elegant solution later on.
Thank you! Code (csharp): using UnityEngine; using System.Collections; public class Experiment : MonoBehaviour { public enum PieSections { _4 = 4, _8 = 8, _16 = 16, _32 = 32, _64 = 64 } public PieSections pieSections = PieSections._4; public float diameter = 1; void OnDrawGizmos() { Gizmos.color = Color.black; Gizmos.DrawWireCube(transform.position, new Vector3(diameter * 2f, diameter * 2f, 0)); int itt = (int)pieSections; for (int i = 0; i < itt; i++) { float a = i * (360f / (float)itt); float aX = Mathf.Sin(a * Mathf.Deg2Rad) / Mathf.Cos(a * Mathf.Deg2Rad); float aY = Mathf.Cos(a * Mathf.Deg2Rad) / Mathf.Sin(a * Mathf.Deg2Rad); float x = 0; float y = 0; if (a >= 315 || a <= 45) // RIGHT SIDE { Gizmos.color = Color.red; x = diameter; y = x * aX; Gizmos.DrawLine(transform.position, transform.position + new Vector3(x, y, 0)); } if (a > 45 a < 135) // TOP SIDE { Gizmos.color = Color.green; y = diameter; x = y * aY; Gizmos.DrawLine(transform.position, transform.position + new Vector3(x, y, 0)); } if (a >= 135 a <= 225) // LEFT SIDE { Gizmos.color = Color.blue; x = -diameter; y = x * aX; Gizmos.DrawLine(transform.position, transform.position + new Vector3(x, y, 0)); } if( a > 225 a < 315) // BOTTOM SIDE { Gizmos.color = Color.yellow; y = -diameter; x = y * aY; Gizmos.DrawLine(transform.position, transform.position + new Vector3(x, y, 0)); } } } }