|
| 1 | +package com.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 | +public final class ThinLens { |
| 22 | + |
| 23 | + private ThinLens() { |
| 24 | + throw new AssertionError("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 | + public static double imageDistance(double focalLength, double objectDistance) { |
| 36 | + |
| 37 | + if (focalLength == 0 || objectDistance == 0) { |
| 38 | + throw new IllegalArgumentException("Focal length and object distance must be non-zero."); |
| 39 | + } |
| 40 | + |
| 41 | + return 1.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 | + public static double magnification(double imageDistance, double objectDistance) { |
| 57 | + |
| 58 | + if (objectDistance == 0) { |
| 59 | + throw new IllegalArgumentException("Object distance must be non-zero."); |
| 60 | + } |
| 61 | + |
| 62 | + return imageDistance / 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 | + public static boolean isRealImage(double imageDistance) { |
| 72 | + return imageDistance > 0; |
| 73 | + } |
| 74 | +} |
0 commit comments