Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- // Created By:
- // Niek van den Brink
- // S1078937
- namespace VillagePeople.Util
- {
- public class LinearEquation
- {
- // Difference in X and Y
- private float _dX, _dY;
- // Contant C
- private float _c;
- // Function boundaries
- private float _maxX, _maxY, _minX, _minY;
- public LineType Type;
- public LinearEquation(Vector2D a, Vector2D b)
- {
- _maxX = Math.Max(a.X, b.X);
- _maxY = Math.Max(a.Y, b.Y);
- _minX = Math.Min(a.X, b.X);
- _minY = Math.Min(a.Y, b.Y);
- _dX = b.X - a.X;
- _dY = b.Y - a.Y;
- // Determine the type of the equation
- if (_dX == 0)
- {
- Type = LineType.Vertical;
- return;
- }
- if (_dY == 0)
- {
- Type = LineType.Horizontal;
- return;
- }
- Type = LineType.Slanted;
- _c = a.Y - a.X * (_dY / _dX);
- }
- public float F(float x)
- {
- if (Type == LineType.Horizontal && x >= _minX && x <= _maxX) // Line is horizontal and x is within range
- return _minY; // return y
- if (Type == LineType.Vertical && x == _minX) // Line is vertical and line is on x
- return (_minY + _maxY) / 2; // should actually return everything between minY and maxY
- if (Type == LineType.Slanted && x >= _minX && x <= _maxX) // Line is slanted and x is within range
- return _dY / _dX * x + _c; // return y
- return float.MinValue; // Error => returns min value of float
- }
- // Check if this equation intersects with another, f2
- public bool Intersects(LinearEquation f2)
- {
- if (Type == LineType.Horizontal && f2.Type == LineType.Horizontal) // Both are horizontal
- return F(_minX) == f2.F(_minX);
- if (Type == LineType.Vertical && f2.Type == LineType.Vertical) // Both are vertical
- {
- if (_minX != f2._minX)
- return false;
- if (_minX > f2._maxX || _maxX < f2._minX)
- return false;
- return true;
- }
- if (Type == LineType.Slanted && f2.Type == LineType.Slanted) // Both are slanted
- {
- float derivedC, derivedDyOverDx, derivedX;
- derivedC = f2._c - _c;
- derivedDyOverDx = _dY / _dX / (f2._dY / f2._dX);
- derivedX = derivedC / derivedDyOverDx;
- return F(derivedX) != f2.F(derivedX);
- }
- if (Type == LineType.Slanted && f2.Type == LineType.Horizontal)
- {
- float derivedC, derivedX;
- derivedC = f2._minY - _c;
- derivedX = derivedC / (_dY / _dX);
- return derivedX >= f2._minX && derivedX <= f2._maxX && F(derivedX) != float.MinValue;
- }
- if (Type == LineType.Horizontal && f2.Type == LineType.Slanted)
- {
- float derivedC, derivedX;
- derivedC = _minY - f2._c;
- derivedX = derivedC / (f2._dY / f2._dX);
- return derivedX >= _minX && derivedX <= _maxX && f2.F(derivedX) != float.MinValue;
- }
- if (Type == LineType.Vertical)
- {
- if (f2.Type == LineType.Horizontal)
- {
- if (f2._minY < _minY || f2._minY > _maxY)
- return false;
- if (f2._minX > _minX || f2._maxX < _maxX)
- return false;
- return true;
- }
- // f2 must be slanted
- return f2.F(_minX) <= _maxY && f2.F(_minX) >= _minY;
- }
- // f2 must be Vertical
- if (Type == LineType.Horizontal)
- {
- if (_minY < f2._minY || _minY > f2._maxY)
- return false;
- if (_minX > f2._minX || _maxX < f2._maxX)
- return false;
- return true;
- }
- // this must be slanted
- return F(f2._minX) <= f2._maxY && F(f2._minX) >= f2._minY;
- }
- public override string ToString()
- {
- if (Type == LineType.Horizontal)
- return " f( { " + _minX + " .. " + _maxX + " } ) = " + _minY + " ";
- if (Type == LineType.Vertical)
- return " f( " + _minX + " ) = { " + _minY + " .. " + _maxY + " } ";
- return " f(x) = (" + _dY + "/" + _dX + ")x + " + _c + " ";
- }
- }
- public enum LineType
- {
- Horizontal,
- Vertical,
- Slanted
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement