
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Program to check two spheres can ever meet by accelerating or not in a 3D space in Python
Suppose there are two spheres whose radius values are r1 and r2. They are at (x1, y1, z1) and (x2, y2, z2) coordinates. And their acceleration values are given like (ax1, ay1, az1) and (ax2, ay2, az2). We have to check whether these two spheres will ever meet on 3D space if they move with given acceleration or not.
So, if the input is like r1 = 1 r2 = 2 pos1 = (0, 0, 0) acc1 = (100,0,0) pos2 = (4, 0, 0) acc2 = (0,0,0), then the output will be True, because second sphere has no acceleration, so it will not move, but first one will move at x direction so they will collide.
To solve this, we will follow these steps −
- px := pos1[0] - pos2[0]
- py := pos1[1] - pos2[1]
- pz := pos1[2] - pos2[2]
- ax := acc1[0] - acc2[0]
- ay := acc1[1] - acc2[1]
- az := acc1[2] - acc2[2]
- da := ax * ax + ay * ay + az * az
- dp := px * px + py * py + pz * pz
- co := ax * px + ay * py + az * pz
- x := 0.0
- if da is not same as 0, then
- x := - co / da
- x := maximum of x, 0
- dis := square root of (da * x * x + 2 * co * x + dp)
- if dis −= r1 + r2, then
- return True
- otherwise return False
Example
Let us see the following implementation to get better understanding −
def solve(r1, r2, pos1, acc1, pos2, acc2): px, py, pz = pos1[0] - pos2[0], pos1[1] - pos2[1], pos1[2] - pos2[2] ax, ay, az = acc1[0] - acc2[0], acc1[1] - acc2[1], acc1[2] - acc2[2] da = (ax * ax + ay * ay + az * az) dp = (px * px + py * py + pz * pz) co = (ax * px + ay * py + az * pz) x = 0.0 if da != 0: x = - co / da x = max(x, 0) dis = (da * x * x + 2 * co * x + dp) ** 0.5 if dis <= r1 + r2: return True else: return False r1 = 1 r2 = 2 pos1 = (0, 0, 0) acc1 = (100,0,0) pos2 = (4, 0, 0) acc2 = (0,0,0) print(solve(r1, r2, pos1, acc1, pos2, acc2))
Input
1, 2, (0, 0, 0), (100,0,0), (4, 0, 0), (0,0,0)
Output
False
- Related Questions & Answers
- Program to check two strings can be equal by swapping characters or not in Python
- Program to check whether two trees can be formed by swapping nodes or not in Python
- Program to check we can cross river by stones or not in Python
- Program to check two rectangular overlaps or not in Python
- Program to check a number is power of two or not in Python
- Program to check we can reach at position n by jumping or not in Python
- Program to check whether all can get a seat or not in Python
- Program to check whether two sentences are similar or not in Python
- Program to check two parts of a string are palindrome or not in Python
- Program to check robot can reach target position or not in Python
- Program to check robbers can rob the vault or not in Python
- Program to check we can reach leftmost or rightmost position or not in Python
- Program to check we can spell out the target by a list of words or not in Python
- Program to check whether two string arrays are equivalent or not in Python
- Program to check whether final string can be formed using other two strings or not in Python
Advertisements