Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit267d2ab

Browse files
Merge branch 'master' into master
2 parents89c7c04 +4f293b3 commit267d2ab

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
packagecom.thealgorithms.physics;
2+
3+
/**
4+
* Implements the Thin Lens Formula used in ray optics:
5+
*
6+
* <pre>
7+
* 1/f = 1/v + 1/u
8+
* </pre>
9+
*
10+
* where:
11+
* <ul>
12+
* <li>f = focal length</li>
13+
* <li>u = object distance</li>
14+
* <li>v = image distance</li>
15+
* </ul>
16+
*
17+
* Uses the Cartesian sign convention.
18+
*
19+
* @see <a href="https://en.wikipedia.org/wiki/Thin_lens">Thin Lens</a>
20+
*/
21+
publicfinalclassThinLens {
22+
23+
privateThinLens() {
24+
thrownewAssertionError("No instances.");
25+
}
26+
27+
/**
28+
* Computes the image distance using the thin lens formula.
29+
*
30+
* @param focalLength focal length of the lens (f)
31+
* @param objectDistance object distance (u)
32+
* @return image distance (v)
33+
* @throws IllegalArgumentException if focal length or object distance is zero
34+
*/
35+
publicstaticdoubleimageDistance(doublefocalLength,doubleobjectDistance) {
36+
37+
if (focalLength ==0 ||objectDistance ==0) {
38+
thrownewIllegalArgumentException("Focal length and object distance must be non-zero.");
39+
}
40+
41+
return1.0 / ((1.0 /focalLength) - (1.0 /objectDistance));
42+
}
43+
44+
/**
45+
* Computes magnification of the image.
46+
*
47+
* <pre>
48+
* m = v / u
49+
* </pre>
50+
*
51+
* @param imageDistance image distance (v)
52+
* @param objectDistance object distance (u)
53+
* @return magnification
54+
* @throws IllegalArgumentException if object distance is zero
55+
*/
56+
publicstaticdoublemagnification(doubleimageDistance,doubleobjectDistance) {
57+
58+
if (objectDistance ==0) {
59+
thrownewIllegalArgumentException("Object distance must be non-zero.");
60+
}
61+
62+
returnimageDistance /objectDistance;
63+
}
64+
65+
/**
66+
* Determines whether the image formed is real or virtual.
67+
*
68+
* @param imageDistance image distance (v)
69+
* @return {@code true} if image is real, {@code false} if virtual
70+
*/
71+
publicstaticbooleanisRealImage(doubleimageDistance) {
72+
returnimageDistance >0;
73+
}
74+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
packagecom.thealgorithms.physics;
2+
3+
importstaticorg.junit.jupiter.api.Assertions.assertEquals;
4+
5+
importorg.junit.jupiter.api.Test;
6+
7+
classThinLensTest {
8+
9+
@Test
10+
voidtestConvexLensRealImage() {
11+
doublev =ThinLens.imageDistance(10,20);
12+
assertEquals(20,v,1e-6);
13+
}
14+
15+
@Test
16+
voidtestMagnification() {
17+
assertEquals(2.0,ThinLens.magnification(20,10),1e-6);
18+
}
19+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp