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_valPoint。其次,您尝试将一单位解雇操作员*应用于结果。而且由于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