forked from Hexagenic/wanikaniwallpaper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
grid.cpp
75 lines (58 loc) · 1.35 KB
/
grid.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include "grid.hpp"
#include <iostream>
namespace wanikani
{
namespace grid
{
double wastedSpace(int width, int &height, int num, double spaceRatio)
{
if(num == 0)
return 1;
if (num < width)
width = num;
if(num % width == 0) // Fits perfectly
height = num / width;
else
height = (num / width) + 1;
double contentRatio = float(width) / float(height);
double wasted;
double charArea;
if(contentRatio > spaceRatio)
{
wasted = ((1/spaceRatio) - (1/contentRatio)) * spaceRatio;
double charWidth = 1.0 / width;
charArea = (charWidth * charWidth) * spaceRatio;
}
else
{
wasted = (spaceRatio - contentRatio) / spaceRatio;
double charHeight = 1.0 / height;
charArea = (charHeight * charHeight) / spaceRatio;
}
int missing = (width * height) - num;
wasted += missing * charArea;
return wasted;
}
double findBest(int num, double spaceRatio, int &width, int &height)
{
int bestWidth = -1;
int bestHeight = -1;
double bestWasted = -1;
for(int i = 1; i <= num; i++)
{
int currentWidth = i;
int currentHeight = 0;
double currentWasted = wastedSpace(currentWidth, currentHeight, num, spaceRatio);
if(bestWasted < 0 || currentWasted < bestWasted)
{
bestWidth = currentWidth;
bestHeight = currentHeight;
bestWasted = currentWasted;
}
}
width = bestWidth;
height = bestHeight;
return bestWasted;
}
}
}