Given a two dimensional graph with points on it, fin a line which passes the most number of points

View previous topic View next topic Go down

Given a two dimensional graph with points on it, fin a line which passes the most number of points

Post  Admin on Fri Aug 26, 2011 3:46 pm

public static Line findBestLine(GraphPoint[] points){
HashTable<Line,Integer> hash=new HashTable<Line,Integer>();
int len=points.length();
for(int i=0;i<len;i++){
for(int j=i+1;j<len;j++){
int slope=(point[i].gety()-point[j].gety())/(point[i].getx()-point[j].getx());
int y=***;
Line line=new Line(slope,y);
int numpoints=2;
for(int z=0;z<len;z++){
if(point[z]!=point[i]&&point[z]!=point[j]){
if(line.include(point[z])){
numpoints++;
}
}
hash.put(line.new Integer(count));
}
}

}
Iterator pointer=hash.iterator();
int max=0;
Line final;
while(pointer.hasNext()){
if(pointer.getvalue().intValue()>max)
final=pointer.getKey();
max=pointer.getValue().intValue();
}
}
return final;

class Line{
double slope;
double y;
public Line(){};
public Line(double s, double yy){
slope=s;
y=yy;
}
public double getslope(){
return slope;
}
public double gety(){
return y;
}
public boolean include(point a){
if(a.gety()==this.slope*a.getx()+this.y)
return true;
return false;
}
}

class GraphPoint{
double x;
double y;
public GraphPoint(double a,double b){
x=a;
y=b;
}
}



Admin
Admin

Posts : 131
Join date : 2011-08-16

View user profile http://codefornongeek.forumotion.com

Back to top Go down

View previous topic View next topic Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum