patternjavaMinor
Creating all possible combinations of points
Viewed 0 times
combinationscreatingpointsallpossible
Problem
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?
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<=m;x1++)
{
for(int y1=0;y1<=n;y1++)
{
for(int x2=0;x2<=m;x2++)
{
for(int y2=0;y2<=n;y2++)
{
for(int x3=0;x3<=m;x3++)
{
for(int y3=0;y3<=n;y3++)
{
for(int x4=0;x4<=m;x4++)
{
for(int y4=0;y4<=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
}
}
}
}
}
}
}
}
}Solution
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.Context
StackExchange Code Review Q#39274, answer score: 7
Revisions (0)
No revisions yet.