Ejemplo n.º 1
0
//---------------------------------------------------------
// Returns the flow direction for a cell at x and y.
//---------------------------------------------------------
int CExercise_04::Get_Flow_Direction(CSG_Grid *grid, int x, int y)
{
	int result = -1;
	double resultAngle = 0;
	CSG_Grid_System system = grid->Get_System();
	double myElevation = grid->asDouble(x, y);

	for (int direction = 0; direction < 8; direction++)
	{
		int nx, ny;
		double length, nElevation, dz, angle;

		length = system.Get_Length(direction);

		// Get neighbor position
		nx = system.Get_xTo(direction, x);
		ny = system.Get_yTo(direction, y);

		if (grid->is_InGrid(nx, ny))
		{
			nElevation = grid->asDouble(nx, ny);

			dz = myElevation - nElevation;
			angle = atan(dz / length);

			if (angle > resultAngle)
			{
				result = direction;
				resultAngle = angle;
			}
		}
	}

	return result;
}