确定使用布尔函数在线路上的所有2D点线还是在C 中的所有圆
detemining if all the 2D points line on a line or a circle or not in c++ using boolean function
我已经制作了一个C 程序来读取文本文件的一些2D值(x,y)。我的代码如下:
#include<iostream>
#include<sstream>
#include<fstream>
using namespace std;
template <typename T>
class Node {
public:
T data;
Node<T>* next;
Node<T>* previous;
Node(T data) {
this->data = data;
this->next = NULL;
this->previous = NULL;
}
};
template <typename T>
class List {
public:
int size;
Node<T>* start;
List() {
start = NULL;
size = 0;
}
Node<T>* insert(T data) {
Node<T>* new_node = new Node<T>(data);
new_node->next = start;
new_node->previous = NULL;
if (start != NULL) {
start->previous = new_node;
}
start = new_node;
size += 1;
return new_node;
}
};
class Point {
public:
double x;
double y;
Node<Point*>* points_node;
Point(double x, double y) {
this->x = x;
this->y = y;
}
};
main()
{
List<Point*>* input_points;
input_points = new List<Point*>();
ifstream ifs("input.txt");
double x,y;
ifs>>x>>y;
while(!ifs.eof())
{
Point* p = new Point(x, y);
input_points->insert(p);
ifs>>x>>y;
}
bool line=check_line(input_points); // boolean function not defined
bool circle=check_circle(input_points); // boolean function not defined
}
有什么方法可以编写布尔函数以确定所有点是否均位于线上还是在圆上?
输入文件格式如下:
5.0 10.0
10.0 10.0
15.0 10.0
您的列表和程序可以使用大量改进。
使用现有数据结构
您可以使用std::pair
类创建Point
:
typedef std::pair<double> Point;
您也可以使用std::list
而不是创建自己的(需要工作):
typedef std::list<Point> Point_List;
知道这一点,main
函数变为:
int main(void)
{
Point_List data_points;
ifstream input("input.txt");
if (!input)
{
cerr << "Error opening input.txtn";
return EXIT_FAILURE;
}
double x, y;
while (input >> x >> y)
{
Point p;
p.first = x;
p.second = y;
data_points.push_back(p);
}
if (line_check(data_points))
{
//...
}
if (circle_check(data_points))
{
//...
}
cout << "nnPaused. Press Enter to continue.n";
cin.ignore(100000, 'n');
return EXIT_SUCCESS;
}
您可以使用迭代器访问列表中的每个元素。搜索网络以查找" C 列表迭代示例"。
对于一条线,选择两个点并以y = mx b的形式从它们中得出一个方程(使用两个点求解m和b)点也满足方程式。如果没有,这些点并非全部在线上。
对于一个圆,方程形式为(x a)^2 (y b)^2 = r^2,因此再次取3点并求解a,b和r。然后看看其他点是否也满足该方程式。如果他们都这样做,那么点就在一个圆圈上。
我认为,阵列比链接列表更合适。您可以使用std :: vector。
相关文章:
- 2D数组来自文本输入,中间有空格
- 将值指定给向量(2D)的向量中的某个位置
- 如何使用用户输入在C++中正确填充2D数组
- 在线编译器中的分段C++没有打印消息
- 如何在C++中检查2D数组中负值的输入验证
- 当我在main中声明了我的2d数组时,为什么我的程序会退出
- 在 2D 向量中使用第三个 [ ] 有什么意义?
- 四边形的 2D 旋转
- 打印第二列时的2d字符矢量打印空间
- 如何将以逗号和空格分隔的整数读取到 2D 数组中?
- 如何在C++函数中声明静态 2D 数组?
- 我是 C++ 的初学者,我想知道如何在 2D 矢量中获取重复值
- 如何声明一个可以在整个程序中使用的全局 2d 3d 4d .. 数组(堆版本)变量?
- opengl glBegin(GL_LINES) 和 glBegin(GL_POINT) 在 2D 中不可视化点矢量
- 在C++中迭代 2D 容器的最干净方法
- 如何引用 2D 指针?
- 如何在 C++ 中使用它的构造函数初始化 unique_ptrs 的 2D 向量?
- C++动态安全 2D 交错阵列
- 如何在C++或OpenCV子矩阵中的2D子向量上使用OpenACC?
- 确定使用布尔函数在线路上的所有2D点线还是在C 中的所有圆