Advertisement
Guest User

Untitled

a guest
Mar 21st, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.81 KB | None | 0 0
  1. using System;
  2.  
  3. // Created By:
  4. // Niek van den Brink
  5. // S1078937
  6. namespace VillagePeople.Util
  7. {
  8.     public class LinearEquation
  9.     {
  10.         // Difference in X and Y
  11.         private float _dX, _dY;
  12.         // Contant C
  13.         private float _c;
  14.         // Function boundaries
  15.         private float _maxX, _maxY, _minX, _minY;
  16.  
  17.         public LineType Type;
  18.  
  19.         public LinearEquation(Vector2D a, Vector2D b)
  20.         {
  21.             _maxX = Math.Max(a.X, b.X);
  22.             _maxY = Math.Max(a.Y, b.Y);
  23.             _minX = Math.Min(a.X, b.X);
  24.             _minY = Math.Min(a.Y, b.Y);
  25.  
  26.             _dX = b.X - a.X;
  27.             _dY = b.Y - a.Y;
  28.  
  29.             // Determine the type of the equation
  30.             if (_dX == 0)
  31.             {
  32.                 Type = LineType.Vertical;
  33.                 return;
  34.             }
  35.             if (_dY == 0)
  36.             {
  37.                 Type = LineType.Horizontal;
  38.                 return;
  39.             }
  40.  
  41.             Type = LineType.Slanted;
  42.             _c = a.Y - a.X * (_dY / _dX);
  43.         }
  44.  
  45.         public float F(float x)
  46.         {
  47.             if (Type == LineType.Horizontal && x >= _minX && x <= _maxX) // Line is horizontal and x is within range
  48.                 return _minY; //  return y
  49.             if (Type == LineType.Vertical && x == _minX) // Line is vertical and line is on x
  50.                 return (_minY + _maxY) / 2; //  should actually return everything between minY and maxY
  51.             if (Type == LineType.Slanted && x >= _minX && x <= _maxX) // Line is slanted and x is within range
  52.                 return _dY / _dX * x + _c; //  return y
  53.             return float.MinValue; // Error => returns min value of float
  54.         }
  55.  
  56.         // Check if this equation intersects with another, f2
  57.         public bool Intersects(LinearEquation f2)
  58.         {
  59.             if (Type == LineType.Horizontal && f2.Type == LineType.Horizontal) // Both are horizontal
  60.                 return F(_minX) == f2.F(_minX);
  61.             if (Type == LineType.Vertical && f2.Type == LineType.Vertical) // Both are vertical
  62.             {
  63.                 if (_minX != f2._minX)
  64.                     return false;
  65.                 if (_minX > f2._maxX || _maxX < f2._minX)
  66.                     return false;
  67.                 return true;
  68.             }
  69.             if (Type == LineType.Slanted && f2.Type == LineType.Slanted) // Both are slanted
  70.             {
  71.                 float derivedC, derivedDyOverDx, derivedX;
  72.  
  73.                 derivedC = f2._c - _c;
  74.                 derivedDyOverDx = _dY / _dX / (f2._dY / f2._dX);
  75.                 derivedX = derivedC / derivedDyOverDx;
  76.  
  77.                 return F(derivedX) != f2.F(derivedX);
  78.             }
  79.  
  80.             if (Type == LineType.Slanted && f2.Type == LineType.Horizontal)
  81.             {
  82.                 float derivedC, derivedX;
  83.  
  84.                 derivedC = f2._minY - _c;
  85.                 derivedX = derivedC / (_dY / _dX);
  86.  
  87.                 return derivedX >= f2._minX && derivedX <= f2._maxX && F(derivedX) != float.MinValue;
  88.             }
  89.  
  90.             if (Type == LineType.Horizontal && f2.Type == LineType.Slanted)
  91.             {
  92.                 float derivedC, derivedX;
  93.  
  94.                 derivedC = _minY - f2._c;
  95.                 derivedX = derivedC / (f2._dY / f2._dX);
  96.  
  97.                 return derivedX >= _minX && derivedX <= _maxX && f2.F(derivedX) != float.MinValue;
  98.             }
  99.  
  100.             if (Type == LineType.Vertical)
  101.             {
  102.                 if (f2.Type == LineType.Horizontal)
  103.                 {
  104.                     if (f2._minY < _minY || f2._minY > _maxY)
  105.                         return false;
  106.                     if (f2._minX > _minX || f2._maxX < _maxX)
  107.                         return false;
  108.                     return true;
  109.                 }
  110.                 // f2 must be slanted
  111.                 return f2.F(_minX) <= _maxY && f2.F(_minX) >= _minY;
  112.             }
  113.  
  114.             // f2 must be Vertical
  115.             if (Type == LineType.Horizontal)
  116.             {
  117.                 if (_minY < f2._minY || _minY > f2._maxY)
  118.                     return false;
  119.                 if (_minX > f2._minX || _maxX < f2._maxX)
  120.                     return false;
  121.                 return true;
  122.             }
  123.             // this must be slanted
  124.             return F(f2._minX) <= f2._maxY && F(f2._minX) >= f2._minY;
  125.         }
  126.  
  127.         public override string ToString()
  128.         {
  129.             if (Type == LineType.Horizontal)
  130.                 return " f( { " + _minX + " .. " + _maxX + " } ) = " + _minY + " ";
  131.             if (Type == LineType.Vertical)
  132.                 return " f( " + _minX + " ) = { " + _minY + " .. " + _maxY + " } ";
  133.             return " f(x) = (" + _dY + "/" + _dX + ")x + " + _c + " ";
  134.         }
  135.     }
  136.  
  137.     public enum LineType
  138.     {
  139.         Horizontal,
  140.         Vertical,
  141.         Slanted
  142.     }
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement