private bool CheckBlockLines(RenderInfo MapData, Point NewCharPos) { Point CalculatedPoint = new Point(); float CalculatedSlope; float CalculatedInvertSlope; int CalculatedIntercept; int CalculatedInvertIntercept; int Distance; int Dist1; int Dist2; int Dist3; Rendering.ILine TmpLine; Rendering.ILabel TmpLable; foreach (MapData.MapAttrabutes.TwoPointAttrabute PointPair in MapData.MapInfo.Attrabutes.BlockLines) { try { if (PointPair.Pos1.X - PointPair.Pos2.X == 0) { CalculatedPoint.X = PointPair.Pos1.X; CalculatedPoint.Y = NewCharPos.Y; } else { CalculatedSlope = CalSlop(PointPair.Pos1, PointPair.Pos2); if (CalculatedSlope == 0) { CalculatedPoint.X = NewCharPos.X; CalculatedPoint.Y = PointPair.Pos1.Y; } else { CalculatedInvertSlope = (-1 / CalculatedSlope); CalculatedIntercept = CalYInc(CalculatedSlope, PointPair.Pos1); CalculatedInvertIntercept = CalYInc(CalculatedInvertSlope, NewCharPos); CalculatedPoint = GetCross(CalculatedSlope, CalculatedInvertSlope, CalculatedIntercept, CalculatedInvertIntercept, PointPair.Pos1.Y, PointPair.Pos2.Y); } } //check that the calculated point is betweem the 2 points if (Between(PointPair.Pos1.X, PointPair.Pos2.X, CalculatedPoint.X, 0) && Between(PointPair.Pos1.Y, PointPair.Pos2.Y, CalculatedPoint.Y, 0)) { Distance = CalDist(CalculatedPoint, NewCharPos); if (Distance <= BarriorDist) { return true; } } } catch (Exception e) { e.ToString(); } } return false; }
MapData.MapAttrabutes.TwoPointAttrabute = Contains 2 Point vars(Int X, Int Y) MapData.MapInfo.Attrabutes.BlockLines = a collection(Forget the type exactly) private bool Between(int A, int B, int C, int Offset) { //Checks if C is between A and B return ((A - Offset <= C && C <= B + Offset) || (A + Offset >= C && C >= B - Offset)); } private float CalSlop(Point a, Point b) { return (float)(a.Y - b.Y) / (float)(a.X - b.X); } private int CalDist(Point a, Point b) { return (int)Math.Sqrt(Math.Pow((b.Y - a.Y), 2) + Math.Pow((b.X - a.X), 2)); } private int CalYInc(float Slope, Point a) { return (int)(a.Y - (Slope * a.X)); } private Point GetCross(float Slope1, float Slope2, float YInt1, float YInt2, int LY1, int LY2) { Point Rturn = new Point(); float x; float y; if (Slope1 == 0) // 'Line one is horizontal { Rturn.Y = LY1; Rturn.X = (int)((YInt2 - LY1) / Slope2); } else if (Slope2 == 0) // Then 'Line two is horizontal { Rturn.Y = LY2; Rturn.X = (int)((YInt2 - LY2) / Slope1); } else { y = ((YInt1 * (Slope2 / Slope1) - YInt2) / ((Slope2 / Slope1) - 1)); x = ((y - YInt1) / Slope1); Rturn.X = (int)Math.Round(x); Rturn.Y = (int)Math.Round(y); } return Rturn; }