|
| 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 | +} |