有没有一种方法可以 "transfer" 从一个函数到另一个函数的数据而不用静态变量 JAVA?
Is there a way to "transfer" data from one function to an other one without static variable JAVA?
以下代码有 2 个函数。 (感谢帮助我的@AlexRudenko)
他们都得到了一段,一行(不一定是不定式)。除了他们已经拥有的。
第一个函数检查是否有交点(只有一个),return是一个布尔值。
第二个应该检查相同的和 return 点本身。
// 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;
}
我想 "saving" 在 return 一个 "true" 值之前第一个函数中的静态变量中的点,然后使用这个静态变量来 "pull"第二个函数中的点和 return 它的值。
另外,您认为对于段而不是不定式行,我应该在第一个函数中添加更多条件吗?
我的意思是是的,可以说两者具有相同的斜率,但这并不意味着它们不能只有一个交点。因为如果我们谈论
第 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 值进行相同的检查。
你怎么看?
谢谢。
反过来不是更容易吗?
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
}
以下代码有 2 个函数。 (感谢帮助我的@AlexRudenko)
他们都得到了一段,一行(不一定是不定式)。除了他们已经拥有的。 第一个函数检查是否有交点(只有一个),return是一个布尔值。 第二个应该检查相同的和 return 点本身。
// 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;
}
我想 "saving" 在 return 一个 "true" 值之前第一个函数中的静态变量中的点,然后使用这个静态变量来 "pull"第二个函数中的点和 return 它的值。
另外,您认为对于段而不是不定式行,我应该在第一个函数中添加更多条件吗? 我的意思是是的,可以说两者具有相同的斜率,但这并不意味着它们不能只有一个交点。因为如果我们谈论 第 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 值进行相同的检查。 你怎么看?
谢谢。
反过来不是更容易吗?
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
}