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.
com.tcs.cv.automata.ei.middleware.DocxToHtmlConverter@491cde5b:image1.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
Answers
import java.io.*;
import java.util.*;
import java.text.DecimalFormat;
class Main
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int v1,v2,r1,r2,n;
double x1,y1,x2,y2;
v1=sc.nextInt();
r1=sc.nextInt();
v2=sc.nextInt();
r2=sc.nextInt();
n=sc.nextInt();
int t1=(v1*n);
int t2=(v2*n);
if(t1>90 && t1<180)
t1=180-t1;
else if(t1>180 && t1<270)
t1=270-t1;
else if(t1>270 && t1<360)
t1=360-t1;
if(t2>90 && t2<180)
t2=180-t2;
else if(t2>180 && t2<270)
t2=270-t2;
else if(t2>270 && t2<360)
t2=360-t2;
double p1=Math.toRadians((double)t1);
double p2=Math.toRadians((double)t2);
x1=r1*(Math.cos(p1));
y1=r1*(Math.sin(p1));
x2=r2*(Math.cos(p2));
y2=r2*(Math.sin(p2));
double res=Math.sqrt((Math.pow((x2-x1),2))+(Math.pow((y2-y1),2)));
System.out.print(String.format("%.2f",res));
} }