fork(4) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10.  
  11. public static final double PI = 3.14159;
  12. public static final double TOLERANCE = 0.0001;
  13.  
  14. public static void main (String[] args) throws java.lang.Exception
  15. {
  16. double boxWidth = 13;
  17. double boxHeight = 8;
  18. double tileWidth = 14;
  19. double tileHeight = 1;
  20.  
  21. double radians = getAngle(boxWidth, tileWidth, tileHeight);
  22. if(radians < 0){
  23. System.out.println("No angle found to fit!");
  24. System.exit(0);
  25. }
  26.  
  27. double y1 = Math.sin(radians) * tileWidth;
  28. double y2 = Math.sin((PI * 0.5) - radians) * tileHeight;
  29. int numTiles = 0;
  30. if(y1 <= boxHeight){
  31. numTiles = (int)((boxHeight - y1) / y2);
  32. }
  33.  
  34. double degrees = radians * 180 / PI; //convert to degrees
  35. System.out.println("Rotated " + degrees + " degrees");
  36. System.out.println("y1 = " + y1);
  37. System.out.println("y2 = " + y2);
  38. System.out.println("numTiles = " + numTiles);
  39. }
  40.  
  41. // boxWidth, tileWidth, tileHeight
  42. public static double getAngle(double bw, double tw, double th){
  43. double maxAngle = PI * 0.25; // 45 degrees, any more would be taller than wide
  44. double angle = maxAngle * 0.5; // start in the middle
  45. double angleDelta = angle * 0.5; // amount to change;
  46. int count = 0;
  47. while(count++ < 100){
  48. double rotatedWidth = tw * Math.cos(angle) + th * Math.sin(angle);
  49. double err = rotatedWidth - bw;
  50. if(Math.abs(err) < TOLERANCE){
  51. return angle;
  52. } else if(err < 0){
  53. angle -= angleDelta;
  54. } else {
  55. angle += angleDelta;
  56. }
  57. angleDelta *= 0.5;
  58. }
  59. return -1; // found no good angle in 100 attempts
  60. }
  61. }
Success #stdin #stdout 0.08s 380224KB
stdin
Standard input is empty
stdout
Rotated 26.23397827148437 degrees
y1 = 6.188525444904378
y2 = 0.8969959689614577
numTiles = 2