我无法在我的项目代码中实现 scaleByFactor() 方法。我不断收到错误,说 return 语句是错误的,但我根本不知道代码应该返回什么。
public class LineSegment {
/**
* The start and end Points of this LineSegment.
*/
private final Point start;
private final Point end;
private int x1;
private int y1;
private int x2;
private int y2;
private double slopeX;
private double slopeY;
private double slope;
private double yIntercept;
private double length;
private double scalar;
private double scalarX;
private double scalarY;
/**
* Construct a LineSegment having the given Point parameters as start and end.
*
* @param start the Point (X1, Y1) at the start of this LineSegment.
* @param end the Point (X2, Y2) at the end of this LineSegment.
*/
public LineSegment(Point start, Point end) {
this.start = start;
this.end =end;
}
/**
* @return the slope of this LineSegment, calculated using the formula:
* (Y2 - Y1)/(X2 - X1).
*/
public double slope() {
start.getX();
end.getY();
slopeX = x2 - x1;
slopeY = y2 - y1;
slope = (slopeY / slopeX);
return slope;
}
/**
* @return the Y intercept of this LineSegment, calculated using the formula:
* Y1 - (M * X1)
* where M is the slope of this LineSegment.
*/
public double yIntercept() {
yIntercept = y1 - (slope * x1);
yIntercept = y2 - (slope * x2);
System.out.print(yIntercept);
return yIntercept;
}
/**
* @return the start Point of this LineSegment.
* This corresponds to the Point (X1, Y1).
*/
public Point getStart() {
return start;
}
/**
* @return the end Point of this LineSegment.
* This corresponds to the Point (X2, Y2).
*/
public Point getEnd() {
return end;
}
/**
* @return the length of this LineSegment, calculated using the formula:
* sqrt((X2 - X1)^2 + (Y2 - Y1)^2).
*/
public double length() {
Math.sqrt((x2 - x1)^2 + (y2 - y1)^2);
return length;
}
/**
* @param scalar the double value to be used to
* scale this LineSegment. Scaling is done
* with the following formula:
* (fill in the formula you will use here!).
* @return a new LineSegment whose start Point is the
* same start Point of this LineSegment and
* the end Point has been scaled by the given
* scalar value.
*/
public LineSegment scaleByFactor(double scalar) {
scalarX = x2 + (x2 - x1) * length;
scalarY = y2 + (y2 - x1) * length;
return scalar;
}
/**
* @return the following String message:
* "y = M x + B"
* where M is the slope of this LineSegment,
* and B is the Y intercept of this LineSegment.
* For example: if the slope were 5 and the Y
* intercept were 7, this method would return:
* "y = 5 x + 7".
*/
@Override
public String toString() {
return "y = "+slope+"x + "+yIntercept;
}
}
基本上,目标是通过给定标量来缩放先前制作的线段,我似乎正在努力解决这个问题。
在此先感谢您的帮助!