Created
April 16, 2014 03:02
-
-
Save castaneai/10801489 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sim02; | |
public class River { | |
/** | |
* 横の長さと縦の長さを使って三平方の定理より斜辺の長さを求める | |
* @param width | |
* @param height | |
* @return | |
*/ | |
private static double getTriangle(double width, double height) { | |
return Math.sqrt(Math.pow(width, 2) + Math.pow(height, 2)); | |
} | |
public static void main(String[] args) { | |
final double walkSpeed = 1.0; | |
final double shipSpeed = 6.0; | |
final double flowSpeedX = 0.0; | |
final double flowSpeedY = 0.9; | |
final double riverWidth = 140; | |
final double riverHeight = 210; | |
final double distAP = 70; | |
final double distQB = 30; | |
double minTime = Double.MAX_VALUE; | |
double minP = -1; | |
double minQ = -1; | |
double minFlowTime = Double.MAX_VALUE; | |
double minFlowP = -1; | |
double minFlowQ = -1; | |
final double step = 0.01; // (p,q)の刻み | |
final double rangeMin = 0; // (p,q)の範囲の下限 | |
final double rangeMax = 10.0; // (p,q)の範囲の上限 | |
for (double p = rangeMin; p <= rangeMax; p += step) { | |
for (double q = rangeMin; q <= rangeMax; q += step) { | |
final double timeAP = getTriangle(distAP, p) / walkSpeed; | |
final double timePQ = getTriangle(riverWidth, riverHeight - (p + q)) / shipSpeed; | |
final double timeQB = getTriangle(distQB, q) / walkSpeed; | |
final double timePQ_Flow = getTriangle( | |
riverWidth - flowSpeedX * timePQ, | |
riverHeight - (p + q) - flowSpeedY * timePQ) / shipSpeed; | |
final double time = timeAP + timePQ + timeQB; | |
final double flowTime = timeAP + timePQ_Flow + timeQB; | |
if (time < minTime) { | |
minP = p; | |
minQ = q; | |
minTime = time; | |
} | |
if (flowTime < minFlowTime) { | |
minFlowP = p; | |
minFlowQ = q; | |
minFlowTime = flowTime; | |
} | |
} | |
} | |
System.out.println("川の流れを無視する場合------------------"); | |
System.out.printf("(p,q)=(%.4f, %.4f)のとき最少時間%.4f[秒]\n", minP, minQ, minTime); | |
System.out.println(); | |
System.out.println("川の流れを考慮する場合-------------------"); | |
System.out.printf("(p,q)=(%.4f, %.4f)のとき最少時間%.4f[秒]\n", minFlowP, minFlowQ, minFlowTime); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment