Terrarium.Game.WorldState.GetAvailableLight C# (CSharp) Method

GetAvailableLight() public method

Percentage of light reaching this plant. We assume the sun moves from east to west directly overhead We get a rough estimation like this: Get all plants with a certain radius whose radius blocks any East-West vector that intersects any part of the radius of the plant in question -- assume they block it completely Figure out which blocks it at the highest angle. Discount the amount of light the plant sees by angle / 180
public GetAvailableLight ( OrganismBase.PlantState plant ) : int
plant OrganismBase.PlantState The plant to get light for.
return int
        public int GetAvailableLight(PlantState plant)
        {
            var maxX = plant.GridX + plant.CellRadius + 25;
            if (maxX > _gridWidth - 1)
            {
                maxX = _gridWidth - 1;
            }
            var overlappingPlantsEast = FindOrganismsInCells(plant.GridX + plant.CellRadius,
                                                             maxX, plant.GridY - plant.CellRadius,
                                                             plant.GridY + plant.CellRadius);

            var minX = plant.GridX - plant.CellRadius - 25;
            if (minX < 0)
            {
                minX = 0;
            }
            var overlappingPlantsWest = FindOrganismsInCells(minX, plant.GridX - plant.CellRadius,
                                                             plant.GridY - plant.CellRadius,
                                                             plant.GridY + plant.CellRadius);

            double maxAngleEast = 0;
            foreach (OrganismState targetPlant in overlappingPlantsEast)
            {
                if (!(targetPlant is PlantState)) continue;
                var currentAngle = Math.Atan2(((PlantState) targetPlant).Height,
                                              targetPlant.Position.X - plant.Position.X);
                if (currentAngle > maxAngleEast)
                {
                    maxAngleEast = currentAngle;
                }
            }

            double maxAngleWest = 0;
            foreach (OrganismState targetPlant in overlappingPlantsWest)
            {
                if (!(targetPlant is PlantState)) continue;
                var currentAngle = Math.Atan2(((PlantState) targetPlant).Height,
                                              plant.Position.X - targetPlant.Position.X);
                if (currentAngle > maxAngleWest)
                {
                    maxAngleWest = currentAngle;
                }
            }

            return (int) (((Math.PI - maxAngleEast + maxAngleWest)/Math.PI)*100);
        }