# Python algorithm to scan a rectangular area by a drone and calculate the flying time

1.6k views

### Python algorithm to scan a rectangular area by a drone and calculate the flying time

I have a course project where I need to write a python algorithm to do the following steps:

1- Take the camera angle, flyings height and sped for the user.

2- Take x and y (represents longitude and altitude) from the user.

3- Calculate the area of the rectangle the drone needs to scan.

4- Divide the area into columns based on the camera angle.

5- Make the drone start from the middle of the first column from top then goes to bottom, then moves half of the next column's width and goes from bottom to top. the repeat till the whole area covered.

6- calculate distance then flying time.

The first 4 steps are straight forward and i added them to clarify the task details.

The algorithm required in the 5th step and then how to calculate the flying time is what I need help at.

I'm new to algorithm writing and coding in python in general, can anyone help on how I can write the algorithm required in step 5

I added the code I wrote to getting the total distance. I keep getting the distance = 0. plus I don't think the distance of the path is correctly calculated (please see the attached screenshot to shows the path)

``````def distance(height, angle, speed, x1, y1, x2, y2):
xc = (x1 + x2)/2
yc = (y1 + y2)/2
xd = (x1 - x2)/2
yd = (y1 - y2)/2

#Third corner
x3 = xc - yd
y3 = yc + xd

#Fourth corner
x4 = xc + yd
y4 = yc - xd

#width and heigth of the rectangle
width = x2 - x1
length= y2 - y1

#calculating a single column width
column_w = (math.sin(angle/2) * height) * 2

total_columns = int(width / column_w)
total_distance= 0

for i in range(total_columns):
total_distance = total_distance + length
if(i % 2 == 0):
print("drone movement: down, right, up")
else:
print("drone movement: up, right, down")
print(total_distance)
print(legnth)
``````

#To get the inputs from the user

``````height = int(input("Enter height:"))
angle = int(input("Enter angle:"))
speed = int(input("Enter speed:"))
x1 = int(input("Enter x1:"))
y1 = int(input("Enter y1:"))
x2 = int(input("Enter x2:"))
y2 = int(input("Enter y2:"))

distance(height, angle, speed, x1, y1, x2, y2)
`````` by (71.8m points)

I think this is more to do as a formula rather than an "algorithm".

Your resulting flight path is a series of "S"s starting top left, down, U-turn, back up, U-turn, down and so on and so forth.
From what you've written in step #5, when the drone does a U-turn it goes sideways half the width of a column.

The total "hight" is given by the `y` input, so `h1` is similar to that (minus some for camera angle etc). The total width of the field is given by the `x` input (I think).
Assuming we have `n` columns in it with width `w1`, we get the number of U-turns:
Each one moves `w1/2` sideways and so coverage is (for `m` sideways moves):
`1 + m * (w1/2) = n * w1`.
This results in `m = 2n - 1` (after rounding up) U-turns.
Works out you'll need `2n * h1` (up/down flights - maybe `2n-1` for odd/even cases) + `(2n-1) * w1` sideway fligths.

For a complete path:

``````direction = 'down'
nextUTurn = 'left'
columnsNeeded = n
for (columnIndex of columnsNeeded) {
print(direction)
print('U-Turn ' + nextUTurn)
direction = getOther(direction)
nextUTurn = getOther(nextUTurn)
}
``````