Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Need help with an algorithm

Discussion in 'Scripting' started by Janju, Aug 16, 2017.

  1. Janju

    Janju

    Joined:
    Nov 24, 2013
    Posts:
    7
    Hi Guys, i try to move an object over a grid. The problem is that between the points i am getting very short stuttering. So i thought i would need to find lines in my point list Like that:
    Code (CSharp):
    1. List<Point2> sample = new List<Point2>()
    2.             {
    3.                 new Point2(5,5),
    4.                 new Point2(4,5),
    5.                 new Point2(3,5),
    6.                 new Point2(3,4),
    7.                 new Point2(3,3),
    8.                 new Point2(2,3)
    9.             };
    10.  
    11.             // this is what the algorithim needs to return me ==>
    12.             List<List<Point2>> result = new List<List<Point2>>();
    13.             result.Add(new List<Point2>() { new Point2(5, 5), new Point2(4, 5), new Point2(3, 5) });
    14.             result.Add(new List<Point2>() { new Point2(3, 5), new Point2(3, 4), new Point2(3, 3) });
    15.             result.Add(new List<Point2>() { new Point2(3, 3), new Point2(2, 3) });
    16.  
    17.             /////////////////////////////////////////////////
    18.             List<Point2> sample2 = new List<Point2>()
    19.             {
    20.                 new Point2(1,2),
    21.                 new Point2(2,2)
    22.             };
    23.  
    24.             // this is what i need ==>
    25.             List<List<Point2>> result2 = new List<List<Point2>>();
    26.             result.Add(new List<Point2>() { new Point2(1, 2), new Point2(2, 2) });
    Maybe anyone has the time to help me out here.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    Are you looking for tweening to go between these points smoothly? There's lots of free tweening packages in the asset store, each with its strengths and weaknesses.
     
  3. Janju

    Janju

    Joined:
    Nov 24, 2013
    Posts:
    7
    Well i solved the problem myself:

    Code (CSharp):
    1. public static List<List<Point2>> LineFinder(List<Point2> sample)
    2.         {
    3.             Point2 startingP = new Point2(-1, -1);
    4.             int pointInLine = 0;
    5.             List<Point2> current = new List<Point2>();
    6.             Point2.Direction currentDirection = Point2.Direction.Undefined;
    7.             List<List<Point2>> result = new List<List<Point2>>();
    8.  
    9.             for (int i = 0; i + 1 < sample.Count; i++)
    10.             {
    11.                 // start a new Line
    12.                 if (pointInLine == 0)
    13.                 {
    14.                     pointInLine++;
    15.  
    16.                     current = new List<Point2>
    17.                     {
    18.                         sample[i],
    19.                         sample[i + 1]
    20.                     };
    21.  
    22.                     result.Add(current);
    23.  
    24.                     currentDirection = (sample[i] - sample[i + 1]).ToDirection();
    25.  
    26.                     if (currentDirection == Point2.Direction.Undefined)
    27.                     {
    28.                         pointInLine = 0;
    29.                     }
    30.                 }
    31.  
    32.                 // start a new Line
    33.                 if (pointInLine != 0)
    34.                 {
    35.                     pointInLine++;
    36.  
    37.                     if (sample.Count - 1 < i + 2)
    38.                     {
    39.                         break;
    40.                     }
    41.                     else
    42.                     {
    43.                         Point2.Direction p2d = (sample[i + 1] - sample[i + 2]).ToDirection();
    44.  
    45.                         if (p2d == currentDirection)
    46.                         {
    47.                             current.Add(sample[i + 2]);
    48.                         }
    49.                         else
    50.                         {
    51.                             pointInLine = 0;
    52.                         }
    53.                     }
    54.                 }
    55.  
    56.             }
    57.  
    58.             return result;
    59.         }