How can I optimize my code that uses nested for loops? I am working to solve a problem where I need to determine if a Point lies on a line connecting two other Points.  For example, if I have Point a, b, c, I want to determine if c is on the line segment connecting a and b.  In my code, I have a point, me (a in the example) and two lists of points, hits (b in the example) and reachable (c in the example).  For each point in hits, I want to determine if there is any point in reachable that is on the line segment that connects me and the point in hits. If there is a point on that line segment, then numHits needs to be decremented. Here is my code:

Point me; //Point a from the example above
ArrayList&lt;Point&gt; hits = new ArrayList&lt;&gt;();  //a list of Point b's from the example above
ArrayList&lt;Point&gt; reachable = new ArrayList&lt;&gt;();  //a list of point c's from the example above

for(Point hit : hits) {
    for(Point p : reachable) {
        if(!hit.equals(p) &amp;&amp; !me.equals(p)) {
            //find the equation of a line from me to hit
            if(hit.x - me.x == 0) { //if the line has an undefined slope... if the line is vertical
                if( (((p.y &lt;= hit.y) &amp;&amp; (p.y &gt;= me.y)) || ((p.y &gt;= hit.y) &amp;&amp; (p.y &lt;= me.y))) &amp;&amp; p.x - me.x == 0) {  //if there is any occupied point on that line in between me and the hit, that point blocks the hit
                    numHits--;
                    break;
                }
            } else {
                //create a line from me to the hit... if there is any occupied point on that line in between me and the hit, that point blocks the hit
                double deltaY = hit.y - me.y;
                double deltaX = hit.x - me.x;

                double m = deltaY / deltaX; //slope
                double b = me.y - (double)(m*me.x);  //y intercept

                if((double) p.y == ((double)(m * p.x) + b)) {  //if this point is on the same line 
                    if( ((p.x &lt;= hit.x &amp;&amp; p.x &gt;= me.x) &amp;&amp; (p.y &lt;= hit.y &amp;&amp; p.y &gt;= me.y)) ||
                                ((p.x &lt;= hit.x &amp;&amp; p.x &gt;= me.x) &amp;&amp; (p.y &gt;= hit.y &amp;&amp; p.y &lt;= me.y)) || 
                                ((p.x &gt;= hit.x &amp;&amp; p.x &lt;= me.x) &amp;&amp; (p.y &gt;= hit.y &amp;&amp; p.y &lt;= me.y)) ||
                                ((p.x &gt;= hit.x &amp;&amp; p.x &lt;= me.x) &amp;&amp; (p.y &lt;= hit.y &amp;&amp; p.y &gt;= me.y))) {  //if the point is in between me and the hit
                        numHits--;
                        break;
                    }
                }
            }
        }
    }
}


My code works to determine if there is any point in reachable between me and each point in hits, it just gets incredibly slow the larger hits and reachable get.  For example, if hits has a size of 780,000 and reachable has a size of 1,500,000 the code takes a very long time to run.  I was wondering how I may be able to optimize this to run more quickly.  I'm not sure if the bottleneck issue lies in the loops themselves or in the code within the loops.  Any help or optimization ideas are greatly appreciated.  Thank you!
It is normally good to break code into several methods.
Everything that is inside your

if(!hit.equals(p) &amp;&amp; !me.equals(p)) 

should be moved to a separate method taking as input those 3 points a,b and c

When coding problems of a mathematical flavor it is often very useful to support your code with some kind of theory.

In this case you might want to look up the mathematical terms "dot product" and "cross product". Once you understand those it becomes easy to implement your function.

And remember to make several functions (dotProduct() and crossProduct()) so you can reuse code later.

When it comes to performance you need to look at your algorithm.
There is no need to test all pairs of points (hit, p).
Maybe you should make a small method that computes the angle of a vector from me to p? If angle is the same they hit, otherwise they miss. 

So the question becomes given a list of angles for points in hits and a list of angles in reachable, do they contain a common angle?