C 段线相交
C++ segment line intersection
本文关键字:段线相 更新时间:2023-10-16
我想问问题在哪里?
我有一个错误:" Unary *"
我是C 编程中的新手,我使用Java样式。指针和解雇对我来说是一个大问题。
我的应用程序获得了输入值并保存为点对象,此后,我应该找到2行的交点。
我想返回一个要计算x和y值的点对象。
.h文件
class Point {
public:
double x_val, y_val;
Point(double, double);
double x();
double y();
double dist(Point other);
Point add(Point b);
Point sub(Point b);
void move(double a, double b);
};
class Triungle {
public:
Triungle(std::string);
void compute_length();
void lines_intersect(Point a, Point b, Point c, Point d, Point *intersection);
Point a, b, c;
};
.cpp文件
Point::Point(double x = 0.0, double y = 0.0) {
x_val = x;
y_val = y;
}
double Point::x() {
return x_val;
}
double Point::y() {
return y_val;
}
double Point::dist(Point other) {
double xd = this->x() - other.x();
double yd = this->y() - other.y();
return sqrt(xd * xd + yd * yd);
}
Point Point::add(Point b) {
return Point(x_val + b.x_val, y_val + b.y_val);
}
Point Point::sub(Point b) {
return Point(x_val - b.x_val, y_val - b.y_val);
}
void Point::move(double a, double b) {
x_val += a;
y_val += b;
}
void Triungle::lines_intersect(Point a, Point b, Point c, Point d, Point *intersection) {
double x, y;
double A1 = b.y_val - a.y_val;
double B1 = b.x_val - a.x_val;
double C1 = a.y_val - (A1 / B1) * a.x_val;
double A2 = d.y_val - c.y_val;
double B2 = d.x_val - c.x_val;
double C2 = c.y_val - (A2 / B2) * c.x_val;
double det = (A1 / B1) - (A2 / B2);
if (det == 0) {
// lines are paralel
} else {
x = (C2 - C1) / det;
y = (A1 * C2 - A2 * C1) / det;
}
*intersection->x_val = x; // here i got error
*intersection->y_val = y; // here i got error
}
Triungle::Triungle(std::string s) {
cout << "enter first point of " << s << " triangle: ";
cin >> a.x_val;
cin >> a.y_val;
if (!(cin)) {
cout << "input error." << endl;
exit(1);
}
cin.clear();
cout << "enter second point of " << s << " triangle: ";
cin >> b.x_val;
cin >> b.y_val;
if (!(cin)) {
cout << "input error." << endl;
exit(1);
}
cin.clear();
cout << "enter 3 point of " << s << " triangle: ";
cin >> c.x_val;
cin >> c.y_val;
if (!cin) {
cout << "input error." << endl;
exit(1);
}
cin.clear();
}
我以这种方式调用函数
int main(int argc, char** argv) {
Triungle a("first");
Triungle b("second");
Point p;
a.lines_intersect(a.a, a.b, a.c, a.a, &p);
}
intersection->member
将取消指针intersection
。这与
(*intersection).member
您不需要两次解雇。
您在代码中所做的工作
*intersection->x_val = x;
等效于
(*(intersection->x_val)) = x;
由于通过指针->
选择运算符的优先级高于DEREFERCE操作员*
,并且后者的优先级高于分配操作员=
。
因此,首先,您的选择成员double x_val
类Point
。其次,您尝试将一单位解雇操作员*
应用于结果。而且由于X_val是双重的,而不是指针,这是由Dereference操作员期望的,所以编译器报告了一个错误。
因此,在此处取消运算符,足以执行以下
intersection->x_val = x;
假设您获得的错误是两行上的汇编错误:
*intersection->x_val = x; // here i got error
*intersection->y_val = y; // here i got error
问题是您正在推荐指针,然后在其上使用放电运算符->
。
相反,您应该做:
intersection->x_val = x;
intersection->y_val = y; // leave it as a pointer
或
*intersection.x_val = x;
*intersection.y_val = y; // use it as an object
相关文章:
- ::grpc::ServerReaderWriter 对象在另一个线程中一段时间后无法调用
- 将数组划分为 k 个非空的非相交子段,以便每个子段都有奇数和
- 通过创建单个线程来运行一段代码可加快执行速度
- C++11 线程段错误
- PCL:访问 pcl::ModelCoefficients::P tr 的字段,使用 RANSAC 进行线模型近似
- MFC:在 CRichEditCtrl 中突出显示一条线一段时间,然后恢复
- 在多线程程序中使用动态数组的头段安全吗
- 实现现有的网络接口,以相对于C 11中的Endianess定义位字段
- C 段线相交
- 这段代码安全吗,是否可以从构造函数C++生成线程
- 使用类中的线程C++段错误
- 线程是否共享一些类字段
- 具有不同模板参数的相同类无法访问彼此的私有字段
- 我们是否需要互斥锁来访问 C++11 - 多线程中单例对象中的数据字段
- 将椭圆转换为多段线
- 当从matlab使用boost::线程时,挂起和/或段错误,而不是直接调用时
- 如何从现有vtkPolyData中复制多段线的子集
- 线程在一段时间后终止
- c++不能理解为什么这段代码不是线程安全的
- 是否可以在没有多段线近似的情况下渲染贝塞尔曲线?如果是,如何