/// <summary> /// Sets the percent of a culture in the population group equal to the desired value. /// It will take shares from all others proportionally. /// </summary> /// <param name="cult">The political group we are setting.</param> /// <param name="percent">The percent we are setting it to.</param> public void SetCulturePercent(Culture cult, double percent) { if (percent <= 0 || percent > 1) { throw new ArgumentOutOfRangeException("Percent must be greater than 0 or less than or equal to 1."); } if (!CultureBreakdown.Any(x => x.CultureId == cult.Id)) { CultureBreakdown.Add(new CultureBreakdown { ParentId = Id, Percent = 0, CultureId = cult.Id }); } double oldPercent = CultureBreakdown .Single(x => x.CultureId == cult.Id) .Percent; double add = (oldPercent - percent) / (percent - 1); CultureBreakdown .Single(x => x.CultureId == cult.Id) .Percent += add; var newSum = CulPercent(); foreach (var group in CultureBreakdown) { group.Percent = group.Percent / newSum; } }
public void ShiftCulturePercent(Culture cult, double percent) { if (!CultureBreakdown.Any(x => x.CultureId == cult.Id)) { CultureBreakdown.Add(new CultureBreakdown { ParentId = Id, Percent = 0, CultureId = cult.Id }); } CultureBreakdown.Single(x => x.CultureId == cult.Id) .Percent += percent; var newSum = CulPercent(); foreach (var cul in CultureBreakdown) { cul.Percent = cul.Percent / newSum; } }