bool isEnd(int x, int y, Cell End) { return(x == End.X && y == End.Y); }
public List <Cell> createLine(double StartX, double StartY, double StopX, double StopY, int flag) { List <Cell> shortest = new List <Cell>(); Queue <Cell> queue = new Queue <Cell>(); Cell[,] previous = new Cell[NumX, NumY]; int StartRow = findRow(StartX); int StopRow = findColumn(StopX); int StartColumn = findRow(StartY); int StopColumn = findColumn(StopY); Cell startCell = new Cell(StartRow, StartColumn, StartX, StartY); Cell stopCell = new Cell(StopRow, StopColumn, StopX, StopY); previous[StartRow, StartColumn] = startCell; queue.Enqueue(startCell); bool complitePath = false; while (queue.Count > 0) { Cell tempCell = queue.Dequeue(); if (isEnd(tempCell.X, tempCell.Y, stopCell))//da li je doslo do kraja { complitePath = true; break; } for (int i = 0; i < 4; i++) { int nextRow = tempCell.X + dr[i]; int nextColumn = tempCell.Y + dc[i]; if (nextRow < 0 || nextColumn < 0 || nextRow >= NumX || nextColumn >= NumY) { continue;//preskoci ako izadjes van matrice } if (previous[nextRow, nextColumn] != null) { continue;//preskoci polja koja si posetio; } if (!isEnd(nextRow, nextColumn, stopCell) && (Cells[nextRow, nextColumn].Space_ != Space.FREE) && flag == 0) { continue; } if (!isEnd(nextRow, nextColumn, stopCell) && (Cells[nextRow, nextColumn].Space_ == Space.NODE) && flag == 1) { continue; } queue.Enqueue(new Cell(nextRow, nextColumn, Cells[nextRow, nextColumn].X_Coord, Cells[nextRow, nextColumn].Y_Coord)); previous[nextRow, nextColumn] = tempCell; } } if (complitePath) { shortest.Add(stopCell); Cell prev = previous[stopCell.X, stopCell.Y]; while (prev.X > 0 && !compareCell(prev, startCell)) { MarkSpace(prev.X, prev.Y); shortest.Add(prev); prev = previous[prev.X, prev.Y]; } shortest.Add(prev); } return(shortest); }
private bool compareCell(Cell first, Cell sec) { return(first.X == sec.X && first.Y == sec.Y && first.X_Coord == sec.X_Coord && first.Y_Coord == sec.Y_Coord); }