Circles and Distances | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 2

Circles and Distances

Circles and Distances Problem Description Task is to calculate the straight line distance between the two objects moving in a circular path. They may move at different velocities. The distance has to be calculated after N seconds. The figure and commentary below it, explains the problem in detail. https://www.tcscodevita.com/CodevitaV7/images/Circles%20and%20Distancesimage1.png We have two point objects B and C at rest on a straight line at a distance r1 and r2 units from a point A. At time t=0 seconds, the objects start moving in a circular path with A at the center with velocity v1 and v2 degrees per second. Given inputs v1, v2, r1 and r2, calculate the distance between the B and C after N seconds. Distance should be printed up to an accuracy of 2 places after the decimal point. Use Rounding Half-up semantics. Input Format First line contains velocity of object B in degrees per second (v1) Second line contains distance of object B from A (r1) Third line contains velocity of object C in degrees per second (v2) Fourth line contains distance of object C from A (r2) Fifth line contains time in seconds after which the distance between B and C, is to be measured (N) Output The distance between B and C, N seconds after they are set in motion Constraints v1, v2, r1, r2 > 0 and all are integer values. r2 > r1 0 < n <= 100 The objects move in anticlockwise direction v1, v2 <=360 r2 <= 100 Explanation Example 1 Input 90 5 270 10 1 Output 15.00 Explanation After 1 second, the object at B would cover 90 degrees and the object at C would cover 270 degrees. Both the objects would be vertically opposite to each other and would lie in a straight line. So the distance between them would be equal to the sum of their distance from the origin A=5+10= 15 units

4th Aug 2018, 5:27 AM
Naveen Reddy
Naveen Reddy - avatar
3 Answers
+ 1
// javascript code -- easily adaptable to any language ^^ function dist_after(v1,r1,v2,r2,n) { // get cartesian coordinates r1 = pos(n*v1,r1); r2 = pos(n*v2,r2); // get distance n = Math.hypot(r1.x-r2.x,r1.y-r2.y); /* // hypotenuse could be 'manually' done by: v1 = r1.x - r2.x; v2 = r1.y - r2.y; n = Math.sqrt(v1*v1 + v2*v2); */ // return rounded distance to 2 digits after dot return Math.round(n*100)/100; } function pos(a,r) { a = (a/180)*Math.PI; // degrees to radians return { x: r*Math.cos(a), y: r*Math.sin(a) }; } console.log(dist_after(90,5,270,10,1)); // Anyway, the image link you provided is not valid... [ edit ] code playground: https://code.sololearn.com/Wbp27gD28n5y/#js
4th Aug 2018, 5:56 AM
visph
visph - avatar
0
Naveen Reddy You have to post some code then you can get help on it. Dont expect that some ethical user make YOUR assignement for you
4th Aug 2018, 9:22 AM
KrOW
KrOW - avatar
4th Aug 2018, 6:29 AM
Naveen Reddy
Naveen Reddy - avatar