private void calculatePeriods(List <Planets> listPlanets, string weather, int years) { Planets pFerengi = listPlanets.Find(p => p.name == "Ferengi"); Planets pVulcano = listPlanets.Find(p => p.name == "Vulcano"); Planets pBetasoide = listPlanets.Find(p => p.name == "Betasoide"); int days = 360 / pFerengi.velocity, periods = 0; //la variable days contiene los dias que tarda Ferengi en dar una vuelta completa, completando un año, tome este planeta para hacer los calculos, ya que es el mas lento string respuesta = String.Empty; switch (weather) { case "sequia": periods = droughtPeriod(days, years, pFerengi, pVulcano, pBetasoide); respuesta = "El clima " + weather + " tendra " + periods + " periodos."; break; case "lluvia": double[] periodMax = new double[3]; periodMax = rainPeriod(days, years, pFerengi, pVulcano, pBetasoide); respuesta = "El clima " + weather + " tendra " + periodMax[0] + " periodos y el dia mas lluvioso sera el: " + periodMax[1] + "\nPerimetro: " + periodMax[2]; break; case "optimo": periods = optimumPeriod(days, years, pFerengi, pVulcano, pBetasoide); respuesta = "El clima " + weather + " tendra " + periods + " periodos."; break; } Console.WriteLine(respuesta); }
private double[] rainPeriod(int days, int years, Planets pFerengi, Planets pVulcano, Planets pBetasoide) { Point ferengiPos, vulcanoPos, betasoidePos, sunPosition; int count = 0, dayMaxP = 0; double[] respuesta = new double[3]; sunPosition = new Point(); sunPosition.x = 0; sunPosition.y = 0; double perimeter = 0; for (int i = 0; i <= days * years; i++) { ferengiPos = calculatePlanetPosition(i, pFerengi); vulcanoPos = calculatePlanetPosition(i, pVulcano); betasoidePos = calculatePlanetPosition(i, pBetasoide); if (pointBelongsInTriangle(ferengiPos, vulcanoPos, betasoidePos, sunPosition)) { count++; if (calculatePerimeter(ferengiPos, vulcanoPos, betasoidePos) > perimeter) { perimeter = calculatePerimeter(ferengiPos, vulcanoPos, betasoidePos); dayMaxP = count; } } } respuesta[0] = count; respuesta[1] = dayMaxP; respuesta[2] = perimeter; return(respuesta); }
//Para calcular la posicion de los planetas, uso el calculo de coordenadas cartesianas basandome en el angulo que es del tipo x = r*cos(angulo) private Point calculatePlanetPosition(int day, Planets planet) { int degree = (day * planet.velocity) % 360; degree = degree < 0 ? degree + 360 : degree; double x = Math.Cos(degree * Math.PI / 180) * planet.sunDistance; double y = Math.Sin(degree * Math.PI / 180) * planet.sunDistance; Point pos = new Point(); pos.x = Math.Round(x, 2); pos.y = Math.Round(y, 2); return(pos); }
//Verifica solo si son colineales los planetas entre si private int optimumPeriod(int days, int years, Planets pFerengi, Planets pVulcano, Planets pBetasoide) { Point ferengiPos, vulcanoPos, betasoidePos; int count = 0; for (int i = 0; i <= days * years; i++) { ferengiPos = calculatePlanetPosition(i, pFerengi); vulcanoPos = calculatePlanetPosition(i, pVulcano); betasoidePos = calculatePlanetPosition(i, pBetasoide); if (arePointsColineal(ferengiPos, vulcanoPos, betasoidePos)) { count++; } } return(count); }