Home > Software design >  I can't seem to figure out the correct algorithm for finding the equidistant points on a semici
I can't seem to figure out the correct algorithm for finding the equidistant points on a semici

Time:10-09

I was asked to find 5 equidistant points to a semi circle in python. So this is the code I wrote as that's what made sense to me mathematically

import math
r=float(input())/2
op=[[round(r*math.cos(i*math.pi/180),2),round(r*math.sin(i*math.pi/180),2)] for i in range(36,181,36)]
print(op)

But when I executed, my code failed the test cases as the answer the professor gave was based on the below code.

import math
r=float(input())/2
op=[[round(r*math.cos(i*math.pi/4),2),round(r*math.sin(i*math.pi/4),2)] for i in range(5)]
print(op)

What I can't seem to understand is why did he take range of 5 and how does i*pi/4 works? Is the way I took my range(36,181,36) wrong? but it seems correct mathematically.

Below is the entire question for reference.(I haven't written the area finding part as I know how to code that its just this part of finding the coordinates that confuses me.)

A mobile phone network tower A covers the circular area which has the diameter of D meters. Another mobile phone network tower B covers the square shaped area with a side of D meters. The coverage area of network tower B is overlapped on the network coverage area of tower A in such a way that one side of square is perfectly fit with the diameter of the circle through the direction of θ between 0 to π radians. Write a Python function for returning the area of the shape of the overlapped network coverage area. Also, the function should return the five equally spaced co-ordinates (x, y) of the boundary of the overlapped circle. Use for loop and nested list for the calculation of co-ordinates. The output should be limited to two decimal values.

CodePudding user response:

Why pi/4...radian math? because we know that our semicircle is defined by a theta angle of 0-180 degrees or 0-pi. Since we need 5 points....and we start from 0....python counts 0,1,2,3,4. Therefore at MAX angle where i = 4 the calculated angle MUST be pi...therefore pi/4. Can be easily substituted for degree math if you like. math.cos and math.sin use radian math hence the use of pi/4 instead of 180/4.

Also your range is incorrect...as it does not satisfy the condition of 0-pi or 0-180 degrees

  • Related