Beispiel #1
0
        public static DataAccess.Weather DetermineDayWether(Point sun, Point planetOne, Point planetTwo, Point planetThree)
        {
            // Determine if planets are in line
            var planetsInLine = GeometricsService.PointsAreInLine(planetOne, planetTwo, planetThree);

            if (planetsInLine.PointsAreInLine)
            {
                // If the sun is in line of the planets, there will be a drought
                if (planetsInLine.LineCrossPointZero)
                {
                    return(DataAccess.Weather.Drought);
                }

                // If planets are in line, the weather in the system is optimal
                return(DataAccess.Weather.Optimal);
            }

            // If the sun is inside the planets triangle, it will be a rainy day
            if (GeometricsService.PointIsInsideTriangle(sun, planetOne, planetTwo, planetThree))
            {
                return(DataAccess.Weather.Rain);
            }

            return(DataAccess.Weather.Incorrect);
        }
        public async Task <bool> LoadWeatherForecastPrevisions(int years)
        {
            var now      = DateTime.Now;
            var lastDate = now.AddYears(years);
            var timeSpan = lastDate.Subtract(now);

            var totalDays = Math.Floor(timeSpan.TotalDays);

            int    dayOfMaximumTrianglePerimeter = 0;
            double maximumTrianglePerimeter      = 0;

            Point sun = new Point {
                X = 0, Y = 0
            };

            ConcurrentBag <DayWeatherForecast>  planetDayWeatherForecasts = new ConcurrentBag <DayWeatherForecast>();
            ConcurrentBag <DayPlanetsPositions> planetDayPositions        = new ConcurrentBag <DayPlanetsPositions>();

            List <Task> weatherForecastTasks = new List <Task>();

            Parallel.For(1, Convert.ToInt32(totalDays + 1), (i) =>
            {
                double ferengiAngle = i;
                var ferengiPosition = GeometricsService.GetPointInCircleCoordinates(500, ferengiAngle);

                double betasoideAngle = 3 * i;
                var betasoidePosition = GeometricsService.GetPointInCircleCoordinates(2000, betasoideAngle);

                double vulcanoAngle = -5 * i;
                var vulcanoPosition = GeometricsService.GetPointInCircleCoordinates(1000, vulcanoAngle);

                var date = now.AddDays(i);

                var isLine = GeometricsService.PointsAreInLine(ferengiPosition, betasoidePosition, vulcanoPosition);
                var sunIsInsideTriangle = GeometricsService.PointIsInsideTriangle(sun, ferengiPosition, betasoidePosition, vulcanoPosition);

                planetDayPositions.Add(new DayPlanetsPositions
                {
                    Day     = i,
                    Date    = date,
                    Ferengi = new DataAccess.Point {
                        X = ferengiPosition.X, Y = ferengiPosition.Y
                    },
                    Betasoide = new DataAccess.Point {
                        X = betasoidePosition.X, Y = betasoidePosition.Y
                    },
                    Vulcano = new DataAccess.Point {
                        X = vulcanoPosition.X, Y = vulcanoPosition.Y
                    },
                    IsLine              = isLine.PointsAreInLine,
                    SunIsInLine         = isLine.LineCrossPointZero,
                    SunIsInsideTriangle = sunIsInsideTriangle
                });

                planetDayWeatherForecasts.Add(new DayWeatherForecast
                {
                    Date    = date.Date,
                    Day     = i,
                    Weather = WeatherForecastService.DetermineDayWether(sun, ferengiPosition, betasoidePosition, vulcanoPosition)
                });

                var trianglePerimeter = GeometricsService.CalculateTrianglePerimeter(ferengiPosition, betasoidePosition, vulcanoPosition);

                if (trianglePerimeter > maximumTrianglePerimeter)
                {
                    maximumTrianglePerimeter      = trianglePerimeter;
                    dayOfMaximumTrianglePerimeter = i;
                }
            });

            var rainPeakDay = planetDayWeatherForecasts.Single(forecast => forecast.Day == dayOfMaximumTrianglePerimeter);

            rainPeakDay.IsMaxTrianglePerimeter = true;

            await dayWeatherForecastRepository.DeleteAllDocumentsFromCollection();

            await dayWeatherForecastRepository.InsertMany(planetDayWeatherForecasts.OrderBy(p => p.Date));

            return(true);
        }