Advertisement
Guest User

XiaolinWuLine algorithm

a guest
Aug 15th, 2018
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.63 KB | None | 0 0
  1. public static class XiaolinWuLine
  2.     {
  3.  
  4.         #region Nested members
  5.  
  6.         public class Vector2IntConfidence
  7.         {
  8.             public readonly Vector2Int Vector2Int;
  9.             public readonly float Confidence;
  10.  
  11.             public Vector2IntConfidence(Vector2Int _vector2Int, float _confidence)
  12.             {
  13.                 Vector2Int = _vector2Int;
  14.                 Confidence = _confidence;
  15.             }
  16.         }
  17.  
  18.         public class Vector2IntConfidencePair
  19.         {
  20.             public readonly Vector2IntConfidence[] vectorConfidencePair = new Vector2IntConfidence[2];
  21.  
  22.             public Vector2IntConfidencePair(Vector2IntConfidence _vector2IntConfidence0,
  23.                 Vector2IntConfidence _vector2IntConfidence1)
  24.             {
  25.                 vectorConfidencePair[0] = _vector2IntConfidence0;
  26.                 vectorConfidencePair[1] = _vector2IntConfidence1;
  27.             }
  28.         }
  29.  
  30.         #endregion
  31.  
  32.         #region Methods
  33.  
  34.         private static float IntPart(float _x)
  35.         {
  36.             return Mathf.Floor(_x);
  37.         }
  38.  
  39.         private static float Round(float _x)
  40.         {
  41.             return Mathf.Round(_x);
  42.         }
  43.  
  44.         private static float FractPart(float _x)
  45.         {
  46.             return _x - Mathf.Floor(_x);
  47.         }
  48.  
  49.         private static float RfPart(float _x)
  50.         {
  51.             return 1 - FractPart(_x);
  52.         }
  53.  
  54.  
  55.         public static List<Vector2IntConfidencePair> CreateLine(Vector2 _start, Vector2 _end)
  56.         {
  57.             bool steep = Mathf.Abs(_end.y - _start.y) > Mathf.Abs(_end.x - _start.x);
  58.             if (steep)
  59.             {
  60.                 float temp = _start.x;
  61.                 _start.x = _start.y;
  62.                 _start.y = temp;
  63.  
  64.                 temp = _end.x;
  65.                 _end.x = _end.y;
  66.                 _end.y = temp;
  67.             }
  68.  
  69.             bool reverse = _start.x > _end.x;
  70.             if (reverse)
  71.             {
  72.                 float temp = _start.x;
  73.                 _start.x = _end.x;
  74.                 _end.x = temp;
  75.  
  76.                 temp = _start.y;
  77.                 _start.y = _end.y;
  78.                 _end.y = temp;
  79.             }
  80.  
  81.             float dx = _end.x - _start.x;
  82.             float dy = _end.y - _start.y;
  83.             float gradient = Math.Abs(dx) < float.Epsilon ? 1.0f : dy / dx;
  84.  
  85.             float xEnd = Round(_start.x);
  86.             float yEnd = _start.y + gradient * (xEnd - _start.x);
  87.             float xGap = RfPart(_start.x + 0.5f);
  88.  
  89.             float xPixel1 = xEnd;
  90.             float yPixel1 = IntPart(yEnd);
  91.  
  92.             List<Vector2IntConfidencePair> vector2IntConfidencePairs = new List<Vector2IntConfidencePair>();
  93.  
  94.             if (steep)
  95.             {
  96.                 vector2IntConfidencePairs.Add(new Vector2IntConfidencePair(
  97.                     new Vector2IntConfidence(new Vector2(yPixel1, xPixel1).GetVector2Int(), RfPart(yEnd) * xGap),
  98.                     new Vector2IntConfidence(new Vector2(yPixel1 + 1.0f, xPixel1).GetVector2Int(), FractPart(yEnd) * xGap)));
  99.             }
  100.             else
  101.             {
  102.                 vector2IntConfidencePairs.Add(new Vector2IntConfidencePair(
  103.                     new Vector2IntConfidence(new Vector2(xPixel1, yPixel1).GetVector2Int(), RfPart(yEnd) * xGap),
  104.                     new Vector2IntConfidence(new Vector2(xPixel1, yPixel1 + 1.0f).GetVector2Int(), FractPart(yEnd) * xGap)));
  105.             }
  106.  
  107.             float interY = yEnd + gradient;
  108.  
  109.             xEnd = Round(_end.x);
  110.             yEnd = _end.y + gradient * (xEnd - _end.x);
  111.             xGap = FractPart(_end.x + 0.5f);
  112.             float xPixel2 = xEnd;
  113.             float yPixel2 = IntPart(yEnd);
  114.  
  115.             if (steep)
  116.             {
  117.                 vector2IntConfidencePairs.Add(new Vector2IntConfidencePair(
  118.                     new Vector2IntConfidence(new Vector2(yPixel2, xPixel2).GetVector2Int(), RfPart(yEnd) * xGap),
  119.                     new Vector2IntConfidence(new Vector2(yPixel2 + 1.0f, xPixel2).GetVector2Int(), FractPart(yEnd) * xGap)));
  120.             }
  121.             else
  122.             {
  123.                 vector2IntConfidencePairs.Add(new Vector2IntConfidencePair(
  124.                     new Vector2IntConfidence(new Vector2(xPixel2, yPixel2).GetVector2Int(), RfPart(yEnd) * xGap),
  125.                     new Vector2IntConfidence(new Vector2(xPixel2, yPixel2 + 1.0f).GetVector2Int(), FractPart(yEnd) * xGap)));
  126.             }
  127.  
  128.             if (steep)
  129.             {
  130.                 for (int x = (int)xPixel1 + 1; x <= xPixel2 - 1; x++)
  131.                 {
  132.                     vector2IntConfidencePairs.Add(new Vector2IntConfidencePair(
  133.                         new Vector2IntConfidence(new Vector2(IntPart(interY), x).GetVector2Int(), RfPart(interY)),
  134.                         new Vector2IntConfidence(new Vector2(IntPart(interY) + 1.0f, x).GetVector2Int(), FractPart(interY))));
  135.                     interY += gradient;
  136.                 }
  137.             }
  138.             else
  139.             {
  140.                 for (int x = (int)xPixel1 + 1; x <= xPixel2 - 1; x++)
  141.                 {
  142.                     vector2IntConfidencePairs.Add(new Vector2IntConfidencePair(
  143.                         new Vector2IntConfidence(new Vector2(x, IntPart(interY)).GetVector2Int(), RfPart(interY)),
  144.                         new Vector2IntConfidence(new Vector2(x, IntPart(interY) + 1.0f).GetVector2Int(), FractPart(interY))));
  145.                     interY += gradient;
  146.                 }
  147.             }
  148.  
  149.             if (!steep && reverse || steep && reverse)
  150.             {
  151.                 vector2IntConfidencePairs.Reverse();
  152.             }
  153.  
  154.             return vector2IntConfidencePairs;
  155.         }
  156.  
  157.         #endregion
  158.  
  159.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement