/// <summary> /// Sets the percent of a species in the population group equal to the desired value. /// It will take shares from all others proportionally. /// </summary> /// <param name="spe">The political group we are setting.</param> /// <param name="percent">The percent we are setting it to.</param> public void SetSpeciesPercent(Species spe, double percent) { if (percent <= 0 || percent > 1) { throw new ArgumentOutOfRangeException("Percent must be greater than 0 or less than or equal to 1."); } if (!SpeciesBreakdown.Any(x => x.SpeciesId == spe.Id)) { SpeciesBreakdown.Add(new SpeciesBreakdown { ParentId = Id, Percent = 0, SpeciesId = spe.Id }); } double oldPercent = SpeciesBreakdown .Single(x => x.SpeciesId == spe.Id) .Percent; double add = (oldPercent - percent) / (percent - 1); SpeciesBreakdown .Single(x => x.SpeciesId == spe.Id) .Percent += add; var newSum = SpePercent(); foreach (var group in SpeciesBreakdown) { group.Percent = group.Percent / newSum; } }
public void ShiftSpeciesPercent(Species spe, double percent) { if (!SpeciesBreakdown.Any(x => x.SpeciesId == spe.Id)) { SpeciesBreakdown.Add(new SpeciesBreakdown { ParentId = Id, Percent = 0, SpeciesId = spe.Id }); } SpeciesBreakdown.Single(x => x.SpeciesId == spe.Id) .Percent += percent; var newSum = SpePercent(); foreach (var part in SpeciesBreakdown) { part.Percent = part.Percent / newSum; } }
private double SpePercent() { return(SpeciesBreakdown.Sum(x => x.Percent)); }