I have given a rectangle whose lower left and upper right vertex coordinates are given as (x1,y1) and (x2,y2). A general point (x, y) is given outside the rectangle. and an integer value R is also given.
Now my objective is to find total number of integral points on and inside the rectangle whose distance from (x,y) is less than or equal to R.
What I did is:
n=0
for i in range(x1, x2+1, 1):
for j in range(y1, y2+1, 1):
if (i-x)**2+(j-y)**2<=R2:
n+=1
print(n)
My code is very inefficient and its time complexity is high as nested for loops have been used. Can you please provide an efficient method to solve the same problem in python?
Example: (x1,y1)=(0,0) and (x2,y2)=(1,1)
let (x,y)=(-8,0) and R=9
then output must be 3 as only (0,0),(1,0) and (0,1) satisfies the conditions.