【问题标题】:Is there a way to "transfer" data from one function to an other one without static variable JAVA?有没有一种方法可以在没有静态变量 JAVA 的情况下将数据从一个函数“传输”到另一个函数?
【发布时间】:2020-04-21 12:10:27
【问题描述】:

以下代码有 2 个函数。 (感谢帮助我的@AlexRudenko)

他们都得到一个段,一条线(不一定是不定式)。除了他们已经拥有的那个。 第一个函数检查是否有一个交点(只有一个),并返回一个布尔值。 第二个应该检查相同并返回点本身。

// Returns true if the lines intersect, false otherwise
    public boolean isIntersecting(Line other) {
        if (equals(other)){
            return false;
        }
        double x11 = this.start.getX();
        double y11 = this.start.getY();
        double x12 = this.end.getX();
        double y12 = this.end.getY();

        double x21 = other.start.getX();
        double y21 = other.start.getY();
        double x22 = other.end.getX();
        double y22 = other.end.getY();

        if (x11 == x12 && x21 == x22) {  // both lines are constant x

            // no intersection point
            return false;

        } else if (x11 == x12 || x21 == x22) { // either line is constant x
            double x;
            double m;
            double b;
            if (x11 == x12) { // first line is constant x, second is sloped
                x = x11;
                m = (y22 - y21) / (x22 - x21);
                b = (x22 * y21 - x21 * y22) / (x22 - x21);
            } else { // second line is constant x, first is sloped
                x = x21;
                m = (y12 - y11) / (x12 - x11);
                b = (x12 * y11 - x11 * y12) / (x12 - x11);
            }
            double y = m * x + b;

            Point.intersection = new Point (x,y);

            return true;

        } else { // both lines are sloped
            double m1 = (y12 - y11) / (x12 - x11);
            double b1 = (x12 * y11 - x11 * y12) / (x12 - x11);

            double m2 = (y22 - y21) / (x22 - x21);
            double b2 = (x22 * y21 - x21 * y22) / (x22 - x21);

            if ((long) m1 == (long) m2) {
                // no intersection point
                return false;
            }
            // calculating intersection coordinates
            double x = (b2 - b1)/(m1 - m2);
            double y = m1 * x + b1;  // or m2 * x + b2

            Point.intersection = new Point (x,y);
            return true;
        }
    }
    // Returns the intersection point if the lines intersect,
    // and null otherwise.
    public Point intersectionWith(Line other) {
        if (isIntersecting(other)) {
            return Point.intersection;
        }
        return null;
    }

我考虑在返回“真”值之前将点“保存”在第一个函数中的静态变量中,然后使用此静态变量“拉”第二个函数中的点并返回它的值。

另外,你认为关于段而不是不定式,我应该在第一个函数中添加更多条件吗? 我的意思是,可以说两者具有相同的斜率,但这并不意味着它们不能只有一个交点。因为如果我们在谈论 第 1 行:(1,0) (1,2) 和第 2 行: (1,2) (1,3) 它们都具有相同的斜率,但它们仍然在 (1,2) 处有一个交点。 那么你认为我应该在代码中添加什么来消除所有这些情况? 我想到了类似的东西:

if(!this.start().equals(line.start())
||!this.start().equals(line.end())
||!this.end().equals(line.start())
||!this.end().equals(line.end()))
return false;

我想我还应该添加一个小检查,检查交点是否在两个段上。我应该取交点的 X 和 Y 值并检查 X 值是否在起点的 X 值和终点的 X 值之间(在两个段上)并对 Y 值进行相同的检查。 你怎么看?

谢谢。

【问题讨论】:

  • 将数据从一个函数传递到另一个函数的方法是使用变量和返回值。对于更复杂的事情,可以考虑使用 objects
  • 返回值或使用可变容器。

标签: java math geometry line


【解决方案1】:

反过来说不是更容易吗?

public Point isIntersecting(Line other) {
//your calculation here
return Point
}

public Boolean intersectionWith(Line other) {
Point p=isIntersecting(other)
if(p==null) 
   return false
return true
}

【讨论】:

  • 哇,我觉得自己没有做到这一点真是太愚蠢了。该死的!但是我在“Point p=isIntersecting(other)”行中仍然有错误。它要求我将其更改为布尔变量,然后它就不起作用了。
猜你喜欢
  • 1970-01-01
  • 2020-05-21
  • 2021-02-03
  • 1970-01-01
  • 1970-01-01
  • 2015-03-23
  • 1970-01-01
相关资源
最近更新 更多