Camera gimbal control in Drone Programming - Time & Space Complexity
When controlling a drone's camera gimbal, the program adjusts angles to keep the camera steady. Understanding how the time to do this changes with input size helps us write smooth, responsive control code.
We want to know how the work grows as the number of angle adjustments increases.
Analyze the time complexity of the following code snippet.
function adjustGimbalAngles(angles) {
for (let i = 0; i < angles.length; i++) {
let angle = angles[i];
setGimbalAngle(angle.pitch, angle.yaw, angle.roll);
}
}
function setGimbalAngle(pitch, yaw, roll) {
// Sends commands to motors to set angles
}
This code loops through a list of angle settings and sends commands to adjust the gimbal motors accordingly.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through the angles array to send motor commands.
- How many times: Once for each angle in the input list.
Each new angle in the list causes one more motor command to be sent. So, if you have more angles, the work grows directly with that number.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 motor commands |
| 100 | 100 motor commands |
| 1000 | 1000 motor commands |
Pattern observation: The work grows in a straight line as the input size increases.
Time Complexity: O(n)
This means the time to adjust the gimbal grows directly with the number of angle commands given.
[X] Wrong: "The time to adjust the gimbal is constant no matter how many angles we send."
[OK] Correct: Each angle requires a separate motor command, so more angles mean more work and more time.
Understanding how loops affect performance is a key skill. It shows you can think about how your code behaves as inputs grow, which is important for real drone control systems.
"What if we batch multiple angle commands into one motor command? How would the time complexity change?"