/// <summary> /// Creates a new ILA algorithm. /// </summary> /// <param name="x1">The x1 coordinate.</param> /// <param name="x2">The x2 coordinate.</param> /// <param name="y1">The y1 coordinate.</param> /// <param name="y2">The y2 coordinate.</param> /// <param name="maxDistance">The max distance.</param> /// <param name="algorithm">The algorithm type.</param> public ILA(ushort x1, ushort x2, ushort y1, ushort y2, byte maxDistance = 10, Algorithm algorithm = Algorithm.DDA) { _algorithm = algorithm; this.X1 = x1; this.Y1 = y1; this.X2 = x2; this.Y2 = y2; this.MaxDistance = maxDistance; if (_algorithm == Algorithm.DDA) { _lineCoordinates = LineCoords(x1, y1, x2, y2); } Direction = (byte)RangeTools.GetAngle(x1, y1, x2, y2);; }
/// <summary> /// Checks whether a coordinate is in line. /// </summary> /// <param name="x">The x coordinate.</param> /// <param name="y">The y coordinate.</param> /// <returns>True if the coordinate is in line.</returns> public bool InLine(ushort x, ushort y) { int mydst = (int)RangeTools.GetDistance((ushort)X1, (ushort)Y1, x, y); byte dir = (byte)RangeTools.GetAngle(X1, Y1, x, y); if (mydst <= MaxDistance) { if (_algorithm == Algorithm.SomeMath) { if (dir != Direction) { return(false); } //calculate line eq if (X2 - X1 == 0) { //=> X - X1 = 0 //=> X = X1 return(x == X1); } else if (Y2 - Y1 == 0) { //=> Y - Y1 = 0 //=> Y = Y1 return(y == Y1); } else { double val1 = ((double)(x - X1)) / ((double)(X2 - X1)); double val2 = ((double)(y + Y1)) / ((double)(Y2 + Y1)); bool works = Math.Floor(val1) == Math.Floor(val2); return(works); } } else if (_algorithm == Algorithm.DDA) { return(Contains(_lineCoordinates, new ILACoordinate(x, y))); } } return(false); }