Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

[SOLVED] I could use some help on a math problem!

Discussion in 'Editor & General Support' started by rob_vld, May 7, 2015.

  1. rob_vld

    rob_vld

    Joined:
    Jul 9, 2013
    Posts:
    187
    I understand this not being a unity3d question, but i am stuck :(

    http://i.imgur.com/vfc9UHf.png

    If given is 2 points... how would i be able to calculate a point on that line closest to the threshold?
    the expected outcome in this example should be 4,6

    Thank you!
     
  2. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,871
    Just take one point's location away from the other, and you get the distance. For actually calculating a point along a line, (Like, say a halfway point) this unity code clears that up!
    Code (csharp):
    1. var result = Vector2.Lerp(vector1, vector2, 0.5);
    In such a case, there would be no need to calculate this yourself, unless you want to get very heavily precise... :D
     
  3. rob_vld

    rob_vld

    Joined:
    Jul 9, 2013
    Posts:
    187
    @FuzzyQuills: thanks for your reply :)
    I am afraid that wont work, since the destination is always random

    The constants are :
    the grid size 13x13
    the center point being 0,0
    the outcome should always be within this 13x13 grid, any angle
     
  4. wkwan

    wkwan

    Joined:
    Sep 14, 2014
    Posts:
    27
    What are the two points in your example exactly? Because if the red border is the threshold you're talking about, it seems like the closest point would be the intersection of the line and the red border, which is (4, 6.5)

    But if I understand the question correctly, a general approach to solve the problem could be:
    1. Take the first point and find the angle to the other point by doing taking the inverse tangent of the y difference of the two points over the x difference of the two points.
    2. If the arrow is pointing towards the right, find the intersection of the arrow with the right border by using tan(angle we found) = (y/(horizontal distance from starting point to the right border)). If it is pointing towards the left, then do it with the left border instead. If it is perfectly vertical, then we can find the solution to the problem easily by just going up or down.
    3. The intersection gives us the answer if the y value is within the bounds. However, the y value might be higher than the upper border or lower than the lower border. So in this case, use similar triangles to find the intersection with the upper or lower border.
     
  5. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,000
    Some rounding errors in this one, but almost:
    Code (CSharp):
    1. Vector2 edgePixel = new Vector2(targetX,targetY).normalized*maxDist;
     
  6. roger_lew

    roger_lew

    Joined:
    Apr 21, 2015
    Posts:
    13
    How about this:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class GridProblem : MonoBehaviour
    6. {
    7.    public float x = 10;
    8.    public float y = 8;
    9.  
    10.    public int i = 0;
    11.    public int j = 0;
    12.  
    13.    // Use this for initialization
    14.    void Update()
    15.    {
    16.      var magX = Mathf.Abs(x);
    17.      var magY = Mathf.Abs(y);
    18.      if (magX >= magY)
    19.      {
    20.        // know magnitude of horizontal component is 6
    21.        // |j|   |y|
    22.        // --- = ---
    23.        //  6    |x|
    24.      
    25.        i = 6;
    26.        j = (int)Mathf.Round(magY / magX * i);
    27.      }
    28.      else
    29.      {
    30.        // know magnitude of vertical component is 6
    31.        // |i|   |x|
    32.        // --- = ---
    33.        //  6    |y|
    34.      
    35.        j = 6;
    36.        i = (int)Mathf.Round(magX / magY * j);
    37.      }
    38.  
    39.      // Now we just have to handle the signs
    40.      if (y < 0)
    41.        j *= -1;
    42.  
    43.      if (x < 0)
    44.        i *= -1;
    45.  
    46.  
    47.      Debug.DrawRay(Vector3.zero, new Vector3(i, 0, j), Color.red);
    48.    }
    49.  
    50. }
    51.  
    52.  
     
  7. rob_vld

    rob_vld

    Joined:
    Jul 9, 2013
    Posts:
    187
    I like to thank all of you for participating... your feedback helped me with creating this perhaps very (c)rude solution...

    So basically a straight line is 1f and a diagonal line is 1.414214f
    what i did is calculate the angle between starting and ending point... grab the angle and determine in which 45 segment it falls... assuming the 45 degrees & 0.414214 is 100%

    which will always give me the outcome between 1 and 1.414214f, then i multiply this by the number of tiles my gridsize/2 is
    this seems to work perfectly...

    Code (CSharp):
    1.         float percentage = 0;
    2.         if( angle >   0 && angle <=  45 ) { percentage =  ( 100f / 45f )        *   angle -  0f; }
    3.         if( angle >  45 && angle <=  90 ) { percentage = 100 - ( ( 100f / 45f ) * ( angle -  45f ) ); }
    4.         if( angle >  90 && angle <= 135 ) { percentage =  ( 100f / 45f )        * ( angle -  90f ); }
    5.         if( angle > 135 && angle <= 180 ) { percentage = 100 - ( ( 100f / 45f ) * ( angle - 135f ) ); }
    6.         if( angle > 180 && angle <= 225 ) { percentage =  ( 100f / 45f )        * ( angle - 180f ); }
    7.         if( angle > 225 && angle <= 270 ) { percentage = 100 - ( ( 100f / 45f ) * ( angle - 225f ) ); }
    8.         if( angle > 270 && angle <= 315 ) { percentage =  ( 100f / 45f )        * ( angle - 270f ); }
    9.         if( angle > 315 && angle <= 360 ) { percentage = 100 - ( ( 100f / 45f ) * ( angle - 315f ) ); }
     
  8. roger_lew

    roger_lew

    Joined:
    Apr 21, 2015
    Posts:
    13
    cool, glad it is getting figured out.

    What you have is equivalent to:

    Code (csharp):
    1. percentage = 100 - 2.22222f * Mathf.Abs(angle % 90 - 45);