Search Unity

Trig Question

Discussion in 'Scripting' started by reveriejake, May 25, 2013.

  1. reveriejake

    reveriejake

    Joined:
    Jul 8, 2007
    Posts:
    819
    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.


    $TrigProblem.png

    Thanks in advance for any help!

    Jake
     
    Last edited: May 25, 2013
  2. dogzerx2

    dogzerx2

    Joined:
    Dec 27, 2009
    Posts:
    3,971
    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.
     
    Last edited: May 25, 2013
  3. reveriejake

    reveriejake

    Joined:
    Jul 8, 2007
    Posts:
    819
    Thank you!


    $CorrectTrig.png

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Experiment : MonoBehaviour
    5. {
    6.     public enum PieSections
    7.     {
    8.         _4 = 4,
    9.         _8 = 8,
    10.         _16 = 16,
    11.         _32 = 32,
    12.         _64 = 64
    13.     }
    14.  
    15.     public PieSections pieSections = PieSections._4;
    16.     public float diameter = 1;
    17.  
    18.     void OnDrawGizmos()
    19.     {
    20.         Gizmos.color = Color.black;
    21.         Gizmos.DrawWireCube(transform.position, new Vector3(diameter * 2f, diameter * 2f, 0));
    22.  
    23.         int itt = (int)pieSections;
    24.  
    25.         for (int i = 0; i < itt; i++)
    26.         {
    27.             float a = i * (360f / (float)itt);
    28.             float aX = Mathf.Sin(a * Mathf.Deg2Rad) / Mathf.Cos(a * Mathf.Deg2Rad);
    29.             float aY = Mathf.Cos(a * Mathf.Deg2Rad) / Mathf.Sin(a * Mathf.Deg2Rad);
    30.             float x = 0;
    31.             float y = 0;
    32.  
    33.             if (a >= 315 || a <= 45)    // RIGHT SIDE
    34.             {
    35.                 Gizmos.color = Color.red;
    36.                 x = diameter;
    37.                 y = x * aX;
    38.  
    39.                 Gizmos.DrawLine(transform.position, transform.position + new Vector3(x, y, 0));
    40.             }
    41.  
    42.             if (a > 45  a < 135)      // TOP SIDE
    43.             {
    44.                 Gizmos.color = Color.green;
    45.                 y = diameter;
    46.                 x = y * aY;
    47.  
    48.                 Gizmos.DrawLine(transform.position, transform.position + new Vector3(x, y, 0));
    49.             }
    50.  
    51.             if (a >= 135  a <= 225)   // LEFT SIDE
    52.             {
    53.                 Gizmos.color = Color.blue;
    54.                 x = -diameter;
    55.                 y = x * aX;
    56.  
    57.                 Gizmos.DrawLine(transform.position, transform.position + new Vector3(x, y, 0));
    58.             }
    59.  
    60.             if( a > 225  a < 315)     // BOTTOM SIDE
    61.             {
    62.                 Gizmos.color = Color.yellow;
    63.                 y = -diameter;
    64.                 x = y * aY;
    65.  
    66.                 Gizmos.DrawLine(transform.position, transform.position + new Vector3(x, y, 0));
    67.             }
    68.         }
    69.     }
    70. }
    71.  
     
    Last edited: May 25, 2013
  4. dogzerx2

    dogzerx2

    Joined:
    Dec 27, 2009
    Posts:
    3,971
    Nice!! :dd