二位平面上,经过点数最多的线
2016-01-22 00:57
441 查看
#include<iostream>
#include<vector>
#include<math.h>
#include<hash_map>
using namespace std;
class Point{
public:
double x;
double y;
Point() :x(0.0), y(0.0){}
Point(double _x, double _y) :x(_x), y(_y){}
};
class Line{
private:
bool infinite_slope = false;
public:
double epsilon = 0.0001;
double slope;
double intercept;
Line(){}
Line(Point p1, Point p2){
if (fabs(p1.x - p2.x) > epsilon){
slope = (p1.y - p2.y) / (p1.x - p2.x);
intercept = p1.y - slope*p1.x;
}
else{
infinite_slope = true;
intercept = p1.x;
}
}
double floorToNearestEpsilon(double d){
int r = (int)(d / epsilon);
return ((double)r)*epsilon;
}
bool isEquivalent(double a, double b){ return fabs(a - b) < epsilon; }
bool isEquivalent(Line& l){
if (isEquivalent(l.slope, slope) && isEquivalent(l.intercept, intercept) && (infinite_slope == l.infinite_slope))
return true;
return false;
}
};
int countEquivalentLines(vector<Line> lines, Line line){
if (lines.size() == 0) return 0;
int count = 0;
for (int i = 0; i < (int)lines.size(); ++i)
if (lines[i].isEquivalent(line))
++count;
return count;
}
int countEquivalentLines(hash_map<double, vector<Line>> lineBySlope, Line line){
double key = line.floorToNearestEpsilon(line.slope);
int count = countEquivalentLines(lineBySlope[key], line) +
countEquivalentLines(lineBySlope[key - line.epsilon],line) +
countEquivalentLines(lineBySlope[key + line.epsilon],line);
return count;
}
void insertLine(hash_map<double, vector<Line>>& lineBySlope, Line& line){
vector<Line> v;
double key = line.floorToNearestEpsilon(line.slope);
hash_map<double, vector<Line>>::iterator it = lineBySlope.find(key);
if (it == lineBySlope.end()){
lineBySlope[key] = v;
lineBySlope[key].push_back(line);
}
else{
lineBySlope[key].push_back(line);
}
}
Line findBestLine(Point points[],int length){
Line bestLine;
int bestCount = 0;
hash_map<double, vector<Line>> linesBySlope;
for (int i = 0; i < length; ++i){
for (int j = i + 1; j < length; ++j){
Line line(points[i], points[j]);
insertLine(linesBySlope, line);
int count = countEquivalentLines(linesBySlope, line);
if (count > bestCount){
bestLine = line;
bestCount = count;
}
}
}
return bestLine;
}
int main(){
Point p[6] ;
p[0] = Point(1, 1);
p[1] = Point(2, 3);
p[2] = Point(3, 3);
p[3] = Point(4, 5);
p[4] = Point(6, 10);
p[5] = Point(7, 7);
Line l = findBestLine(p,sizeof(p)/sizeof(Point));
cout << std::fixed << l.intercept << " " << l.slope << endl;
cin.get();
return 0;
}
结果显示正确
#include<vector>
#include<math.h>
#include<hash_map>
using namespace std;
class Point{
public:
double x;
double y;
Point() :x(0.0), y(0.0){}
Point(double _x, double _y) :x(_x), y(_y){}
};
class Line{
private:
bool infinite_slope = false;
public:
double epsilon = 0.0001;
double slope;
double intercept;
Line(){}
Line(Point p1, Point p2){
if (fabs(p1.x - p2.x) > epsilon){
slope = (p1.y - p2.y) / (p1.x - p2.x);
intercept = p1.y - slope*p1.x;
}
else{
infinite_slope = true;
intercept = p1.x;
}
}
double floorToNearestEpsilon(double d){
int r = (int)(d / epsilon);
return ((double)r)*epsilon;
}
bool isEquivalent(double a, double b){ return fabs(a - b) < epsilon; }
bool isEquivalent(Line& l){
if (isEquivalent(l.slope, slope) && isEquivalent(l.intercept, intercept) && (infinite_slope == l.infinite_slope))
return true;
return false;
}
};
int countEquivalentLines(vector<Line> lines, Line line){
if (lines.size() == 0) return 0;
int count = 0;
for (int i = 0; i < (int)lines.size(); ++i)
if (lines[i].isEquivalent(line))
++count;
return count;
}
int countEquivalentLines(hash_map<double, vector<Line>> lineBySlope, Line line){
double key = line.floorToNearestEpsilon(line.slope);
int count = countEquivalentLines(lineBySlope[key], line) +
countEquivalentLines(lineBySlope[key - line.epsilon],line) +
countEquivalentLines(lineBySlope[key + line.epsilon],line);
return count;
}
void insertLine(hash_map<double, vector<Line>>& lineBySlope, Line& line){
vector<Line> v;
double key = line.floorToNearestEpsilon(line.slope);
hash_map<double, vector<Line>>::iterator it = lineBySlope.find(key);
if (it == lineBySlope.end()){
lineBySlope[key] = v;
lineBySlope[key].push_back(line);
}
else{
lineBySlope[key].push_back(line);
}
}
Line findBestLine(Point points[],int length){
Line bestLine;
int bestCount = 0;
hash_map<double, vector<Line>> linesBySlope;
for (int i = 0; i < length; ++i){
for (int j = i + 1; j < length; ++j){
Line line(points[i], points[j]);
insertLine(linesBySlope, line);
int count = countEquivalentLines(linesBySlope, line);
if (count > bestCount){
bestLine = line;
bestCount = count;
}
}
}
return bestLine;
}
int main(){
Point p[6] ;
p[0] = Point(1, 1);
p[1] = Point(2, 3);
p[2] = Point(3, 3);
p[3] = Point(4, 5);
p[4] = Point(6, 10);
p[5] = Point(7, 7);
Line l = findBestLine(p,sizeof(p)/sizeof(Point));
cout << std::fixed << l.intercept << " " << l.slope << endl;
cin.get();
return 0;
}
结果显示正确
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- 关于指针的一些事情
- c++ primer 第五版 笔记前言
- share_ptr的几个注意点
- C#数据结构之顺序表(SeqList)实例详解
- Lua中调用C++函数示例
- Lua教程(一):在C++中嵌入Lua脚本
- Lua教程(七):数据结构详解
- Lua教程(二):C++和Lua相互传递数据示例
- 解析从源码分析常见的基于Array的数据结构动态扩容机制的详解
- C#数据结构之队列(Quene)实例详解
- C#数据结构揭秘一
- C#中遍历Hashtable的4种方法
- C#数据结构之单链表(LinkList)实例详解
- C++联合体转换成C#结构的实现方法
- C++高级程序员成长之路
- C++编写简单的打靶游戏
- C++ 自定义控件的移植问题
- C++变位词问题分析
- C/C++数据对齐详细解析