Creating all possible combinations of points I want to create all possible combinations of points (0,0),(0,1)...(12345,6789) and then all segments from these points.

The code I have written is simple and with no optimization.  Is there any algorithm to generate it in less time?

public static void main(String[] args) {
    int m=12345;
    int n=6789;

    for(int x1=0;x1&lt;=m;x1++)
    {
        for(int y1=0;y1&lt;=n;y1++)
        {
            for(int x2=0;x2&lt;=m;x2++)
            {
                for(int y2=0;y2&lt;=n;y2++)
                {
                    for(int x3=0;x3&lt;=m;x3++)
                    {
                        for(int y3=0;y3&lt;=n;y3++)
                        {
                            for(int x4=0;x4&lt;=m;x4++)
                            {
                                for(int y4=0;y4&lt;=n;y4++)
                                {       
                                    Point p1 = new Point(x1, y1);
                                    Point p2 = new Point(x2, y2);
                                    Point p3 = new Point(x3, y3);
                                    Point p4 = new Point(x4, y4);

                                    Segment s1 = new Segment(p1, p2);
                                    Segment s2 = new Segment(p2, p3);
                                    Segment s3 = new Segment(p3, p4);
                                    Segment s4 = new Segment(p4, p1);

                                    //operate on those segements

                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
Google Guava has a cartesianProduct helper method, I would try to use it instead of the nested loops. I don't think that it will be faster but it would be easier to read an maintain.