David G

David G

  • 1.5k
  • 224
  • 14.6k

Determining correct box to place image according to ratio

Mar 3 2018 7:24 AM
Hey all I am in need of some help with determines what box I need to place a given image into depending on that images ratio (image width / image height) to the box areas Aspect Ratio (box width / box height).
 
The code below is what I currently have that I am using to determine this (which does not work)
  1. public static Dictionary<string, List<double>> sizeOfPhotoBoxes = new Dictionary<string, List<double>>()  
  2. {/* 
  3. [Width] 
  4. | [Height] 
  5. | | [Aspact Ratio] 
  6. ↓ ↓ ↓ 
  7. KEY: VALUE(s): [1] [2] [3]*/  
  8. "box1"new List<double> {357, 272, 1.31} },  
  9. "box2"new List<double> {357, 272, 1.31 } },  
  10. "box3"new List<double> {365, 460, 0.79 } },  
  11. "box4"new List<double> {365, 265, 1.38 } },  
  12. "box5"new List<double> {715, 455, 1.57 } },  
  13. "box6"new List<double> {360, 465, 0.77 } },  
  14. "box7"new List<double> {360, 465, 0.77 } },  
  15. "box8"new List<double> {360, 465, 0.77 } },  
  16. "box9"new List<double> {540, 290, 1.86 } },  
  17. "box10"new List<double> {540, 290, 1.86 } }  
  18. };  
  19. double RA = (double)item.images[0].width / item.images[0].height;  
  20. foreach (var AR in sizeOfPhotoBoxes)  
  21. {  
  22. if (AR.Value[2] >= RA && AR.Value[2] <= RA)  
  23. {  
  24. System.Diagnostics.Debug.WriteLine("found");  
  25. }  
  26. }  
I'm trying to figure out how to loop through my boxes' Aspect Ratio's to determine which box that image should go into according to that image Ratio. Currently with the code above, I am not getting the "found" output.
 
A few image samples:
 
1) 960/720 = 1.33
2) 1440/800 = 1.8
 
3) 1078/1440 = 0.74
 
4) 2048/1152 = 1.77
 
So taking the example above:
 
Image 1 would be best in Box 1 or 2 since that box has a 1.31 Aspect Ratio.
 
Image 2 would be best in box 9 or 10 since that box has a 1.86 Aspect Ratio.
 
Image 3 would be best in box 6, 7 or 8 since that box has a 0.77 Aspect Ratio.
 
Image 4 would be best in box 9 or 10 since that box has a 1.86 Aspect Ratio.
 
Any help with the code to determine this above would be great! Thanks!!