Showing posts with label points. Show all posts
Showing posts with label points. Show all posts

Sunday, March 18, 2012

Is point p inside a Triangle?


Check Whether Point Lie Inside, on or outside the triangle efficiently what will be time complexity & which data structure algorithm 
as you can see google asked they wants quality code & efficient algorithm.


An easier way than what Richie mentioned is to the following:


if a point P is inside triangle ABC, then


Area PAB + Area PBC + Area PAC == Area ABC


Area of a triangle = Math.sqrt(S(S-a)(S-b)(S-c))
S = (a+b+c)/2
Where a, b and c is the length of the sides of the triangle.


Code input/output:
3
1 0
5 7
8 0
x: 1.0 y: 0.0
x: 5.0 y: 7.0
x: 8.0 y: 0.0


Atotal: 24.5
Aabc: 24.5
Check if point p inside triangle ? true


Atotal: 39.5
Aabc: 24.5
Check if point p inside triangle ? false

Sunday, February 19, 2012

Find a line that intersects max number of points in a plane.


Given a bunch of points, how would you find the line which intersects the most number of points? Write pseudo-code for this algorithm.


Approach:


S = {p1,p2,p3,p4 ..., pn}
Where each point pi = {x, y} coordinates.


for every pair of Pi and Pj calculate:
Equation of line is: y = m*x + b
calculate slope m = (yj-yi)/(xj-xi)
also calculate y intercept b = (yi - m*xi)


Now, have a HashMap with key, value as HashMap H = <slope, <HashMap<y-intercept, count>>
Let G = HashMap<y-intercept, count>;


So for every pair of points we will calculate slope and y-intercept.
First we find if the slope already exists in the hashMap H and then we will check if the y-intercept exists in the 
hash G, if so then increment the counter C.
The Max counter Ci with slope mi and y-intercept bi is the answer.
ie y = mi * x + bi;

Saturday, February 18, 2012

Find k closest stars in 3D space.


You have a huge set of stars as three dimensional coordinates. How would you find the k closest stars?


Approach:
S1 = {x1,y1,z1};
S2 = {x2,y2,z2};
S3 = {x3,y3,z3}; and so on..


1. First sort S[1..n] according to x coordinate. Then for every S[i] find the distance from k points in the front and then k points in the back by maintaing a max-heap of size k.
2. Secondly sort S[1..n] according to y coordinate. Then for every S[i] find the distance from k points in the front and k points in tha back and updating the k-size max-heap defined above if required.
3. Now sort S[1..n] according to z coordinate. And for every S[i] find the distance from k points in the front and k points in tha back and update the k-size max-heap if required.
4. Finally, k sized max-heap is the closed k stars.


Total complexity: 3nlogn + 6kn + 5klogk.
Considering k << n, we have: O(nlogn).

Monday, December 26, 2011

You are given a list of points in the plane, write a program that outputs each point along with the three other points that are closest to it.


You are given a list of points in the plane, write a program that
outputs each point along with the three other points that are closest
to it. These three points ordered by distance.
The order is less then O(n^2) .
For example, given a set of points where each line is of the form: 
ID x-coordinate y-coordinate
1 0.0 0.0
2 10.1 -10.1
3 -12.2 12.2
4 38.3 38.3
5 79.99 179.99
Your program should output:
1 2,3,4
2 1,3,4
3 1,2,4
4 1,2,3
5 4,3,1

Approach:
First Have all the points in an array and sort them according to their X axis, which would give:
3 -12.2 12.2 
1 0.0 0.0 
2 10.1 -10.1 
4 38.3 38.3 
5 79.99 179.99 
Then for every point P, calculate the distance from 3 points in the front and 3 point in the back:
For Ex: Point with Id=3 (-12.2, 12.2) does not have 3 points in the back.
Always store only min 3 distances and the corresponding point Ids - Max heap can help here.

Then Sort the points according to their Y Axis, which would give:
2 10.1 -10.1 
1 0.0 0.0 
3 -12.2 12.2 
4 38.3 38.3 
5 79.99 179.99 
Again calculate for every point p, distances from 3 points in the front and 3 points in the back - Always maintaining the Max heap of size 3.

Total complexity: 2nlogn + 4kn + klogk = O(nlogn)
k = 3 here.