/// /// Line is defined by starting point and ending point on 2D dimension. /// Point on 2D landscape. /// author Roman Kushnarenko (sromku@gmail.com) /// C# version https://github.com/BergWerkGIS /// public class Line { private Point _start; private Point _end; private double _a = double.NaN; private double _b = double.NaN; private bool _vertical = false; public Line(Point start, Point end) { _start = start; _end = end; if (_end.x - _start.x != 0) { _a = ((_end.y - _start.y) / (_end.x - _start.x)); _b = _start.y - _a * _start.x; } else { _vertical = true; } } /// /// Indicate whereas the point lays on the line. /// /// The point to check /// True if the point lays on the line, otherwise return False public bool isInside(Point point) { double maxX = _start.x > _end.x ? _start.x : _end.x; double minX = _start.x < _end.x ? _start.x : _end.x; double maxY = _start.y > _end.y ? _start.y : _end.y; double minY = _start.y < _end.y ? _start.y : _end.y; if ((point.x >= minX && point.x <= maxX) && (point.y >= minY && point.y <= maxY)) { return true; } return false; } /// /// Indicate whereas the line is vertical. /// For example, line like x=1 is vertical, in other words parallel to axis Y. /// In this case the A is (+/-)infinite. /// /// public bool isVertical() { return _vertical; } /// /// y = Ax + B /// /// The A public double getA() { return _a; } /// /// y = Ax + B /// /// The B public double getB() { return _b; } /// /// Get start point. /// /// The start point. public Point getStart() { return _start; } /// /// Get end point. /// /// The end point. public Point getEnd() { return _end; } public override string ToString() { return $"{_start}-{_end}"; } }