กำหนด 2 ค่าทศนิยม: ระยะห่างภาพ (image distance) และระยะห่างวัตถุ (object distance) จากเลนส์ หาความยาวโฟกัส
ความยาวโฟกัส (Focal Length) คืออะไร?
ระยะห่างระหว่างศูนย์กลางเลนส์/กระจกโค้งกับโฟกัส (Focus)
สูตร: 1/F = 1/O + 1/I
- F = ความยาวโฟกัส
- O = ระยะห่างเลนส์ถึงวัตถุ (Object distance)
- I = ระยะห่างเลนส์ถึงภาพ (Image distance)
ตัวอย่าง
Input: image_distance=5, object_distance=10 Output: Focal length = 3.333333 Explanation: 1/5 + 1/10 = 3/10 → F = 10/3 = 3.333333 Input: image_distance=7, object_distance=10 Output: Focal length = 4.117647
อัลกอริทึม
focal_length(image_distance, object_distance):
return 1 / ((1/image_distance) + (1/object_distance))
ตัวอย่างโค้ด
#include <stdio.h>
float focal_length(float image_distance, float object_distance) {
return 1 / ((1 / image_distance) + (1 / object_distance));
}
int main() {
float image_distance = 5;
float object_distance = 10;
printf("Focal length of a lens is: %f\n", focal_length(image_distance, object_distance));
return 0;
}
เอาต์พุต
Focal length of a lens is: 3.333333