Skip to content

Commit

Permalink
feat: add missing run comparison percentage
Browse files Browse the repository at this point in the history
  • Loading branch information
DorielRivalet committed Jan 14, 2024
1 parent e329d60 commit 9648b08
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions MHFZ_Overlay/Views/Windows/ConfigWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2425,6 +2425,31 @@ private double CalculateBetterLinePercentage(Dictionary<int, int> line1, Diction
}
}

private double CalculateBetterLinePercentage(Dictionary<int, double> line1, Dictionary<int, double> line2)
{
if (line1.Count == 0 || line2.Count == 0)
return 0.0;
// Find the cutoff time where the first series ends.
int cutoffTime = Math.Min(line1.Keys.Max(), line2.Keys.Max());

// Calculate the area under each line series up to the cutoff time.
double area1 = CalculateAreaUnderLine(line1, cutoffTime);
double area2 = CalculateAreaUnderLine(line2, cutoffTime);

// Determine which series has the larger area and calculate the percentage difference.
if (area1 == area2)
{
return 0; // Both series are equal.
}
else
{
double largerArea = Math.Max(area1, area2);
double smallerArea = Math.Min(area1, area2);
double percentageDifference = ((largerArea - smallerArea) / smallerArea) * 100;
return area1 > area2 ? percentageDifference : -percentageDifference;
}
}

private double CalculateAreaUnderLine(Dictionary<int, int> line, int cutoffTime)
{
// Assuming the line is sorted by time.
Expand Down Expand Up @@ -2453,6 +2478,34 @@ private double CalculateAreaUnderLine(Dictionary<int, int> line, int cutoffTime)
return area;
}

private double CalculateAreaUnderLine(Dictionary<int, double> line, int cutoffTime)
{
// Assuming the line is sorted by time.
double area = 0;
int previousTime = 0;
double previousScore = 0.0;

foreach (var point in line.OrderBy(p => p.Key))
{
if (point.Key > cutoffTime)
break;

if (previousTime != 0)
{
// Calculate the area of the trapezoid.
double base1 = point.Value;
double base2 = previousScore;
double height = point.Key - previousTime;
area += (base1 + base2) * height / 2;
}

previousTime = point.Key;
previousScore = point.Value;
}

return area;
}

private void SetLineSeriesForDictionaryIntDouble(Dictionary<int, double> data, Dictionary<int, double>? extraData)
{
if (this.graphChart == null)
Expand Down Expand Up @@ -2498,6 +2551,13 @@ private void SetLineSeriesForDictionaryIntDouble(Dictionary<int, double> data, D
Stroke = new SolidColorPaint(new SKColor(this.MainWindow.DataLoader.Model.HexColorToDecimal("#ff89b4fa"))) { StrokeThickness = 2 },
Fill = new LinearGradientPaint(new SKColor(this.MainWindow.DataLoader.Model.HexColorToDecimal("#ff89b4fa", "7f")), new SKColor(this.MainWindow.DataLoader.Model.HexColorToDecimal("#ff89b4fa", "00")), new SKPoint(0.5f, 0), new SKPoint(0.5f, 1)),
});

if (this.runIDComparisonTextBlock != null && this.extraRunIDTextBox != null)
{
var runComparisonPercentage = CalculateBetterLinePercentage(newData, newData2);
var betterRun = runComparisonPercentage >= 0.0 ? RunIDTextBox.Text : extraRunIDTextBox.Text;
this.runIDComparisonTextBlock.Text = string.Format(CultureInfo.InvariantCulture, "Run {0} is higher by around {1:0.##}%", betterRun, Math.Abs(runComparisonPercentage));
}
}

this.XAxes = new Axis[]
Expand Down Expand Up @@ -2717,6 +2777,13 @@ private void SetHitsTakenBlocked(Dictionary<int, Dictionary<int, int>> data, Dic
Stroke = new SolidColorPaint(new SKColor(this.MainWindow.DataLoader.Model.HexColorToDecimal("#ff89b4fa"))) { StrokeThickness = 2 },
Fill = new LinearGradientPaint(new SKColor(this.MainWindow.DataLoader.Model.HexColorToDecimal("#ff89b4fa", "7f")), new SKColor(this.MainWindow.DataLoader.Model.HexColorToDecimal("#ff89b4fa", "00")), new SKPoint(0.5f, 0), new SKPoint(0.5f, 1)),
});

if (this.runIDComparisonTextBlock != null && this.extraRunIDTextBox != null)
{
var runComparisonPercentage = CalculateBetterLinePercentage(newData, newData2);
var betterRun = runComparisonPercentage >= 0.0 ? RunIDTextBox.Text : extraRunIDTextBox.Text;
this.runIDComparisonTextBlock.Text = string.Format(CultureInfo.InvariantCulture, "Run {0} is higher by around {1:0.##}%", betterRun, Math.Abs(runComparisonPercentage));
}
}

this.XAxes = new Axis[]
Expand Down

0 comments on commit 9648b08

Please sign in to comment.