The code you posted is essentially my algorithm, I don't think I explained with enough clarity in my first post.
The question I was asking is should I filter the points that definitely could not be within the semi-circle i.e. have a distance from the origin of the circle bigger than it's radius and then run the algorithm on the points that could be in the circle.
OR
Is it more efficient to just iterate through all points with the algorithm?
Option A:
Iterate through each point
- If the point is not within the radius, go to the next point
- If you get to this point, add it to a temporary list
Iterate through each point in the list
- If the point is not in the right half of the circle, go to the next point
- If you get to this point in the loop, add it to the final loop.
Option B:
Iterate through each point
- If the point is not within the radius, go to the next point
- If the point is not in the right half of the circle, go to the next point
- Add point to the final list if you get to this point
You are basically asking if its faster to filter through a list of points once, then filter it again. Or filter it once and go directly to the result. In order to filter over something, you have iterate over every element. For option A, you do N elements on the first pass and M elements on the second. For option B you test N elements and do a second test on the M elements that pass the first test. The difference is you don't have to waste time copying data to a new array, doing M extra loop tests, and waiting for the CPU to cache extra memory when inserting into the temporary list or starting back at the beginning of the list in the second loop.
Also I realise that the better method would be to use radians (I don't understand these yet), but would the following work to determine whether a point within the circle is in the top semicircle or bottom semicircle:
1 2 3 4 5 6 7 8 9 10 11 12
| A = Answer P = Point (x,y) C = Circle Origin (x,y)
P = (P - C) C = (C - C)
A = (C - P)
If A is -ve then P lies within the top half of the circle.
If A is +ve then P lies within the bottom half of the circle. |
This is obviously not in Java, it is more of a mental exercise.
Radians are totally irrelevant and your algorithm must have a typo because it does not do anything useful.