0

我已经转换了形状分配,并且我的编码已经完成了。但是,我不知道如何为这种方法编写代码。

    public Point2D apply(Point2D p) {

        }

我和教授谈过,他说,“apply() 需要创建给定点的副本,然后转换副本。你已经制作了副本;现在在返回之前转换副本。”

任何人都可以根据他所说的为我编写此方法的代码吗?

问候,

4

2 回答 2

1

“你已经制作了副本;现在在归还之前转换副本。”

从字面上看,你的老师给了你答案。调用 transform 方法,使用副本。

听着,我想我再清楚不过了……

    Point2D newPoint = new Point2D (x, y);
    transform(newPoint);  // <---- You need to add this line
    return newPoint;
于 2013-10-25T18:44:05.040 回答
0

In your code you must use your transform() method:

public Point2D apply(Point2D p) {
    double x = p.getX();
    double y = p.getY();
    Point2D newPoint = (Point2D) p.clone();
    transform(newPoint);
    return newPoint;
}
public void transform(Point2D p) {
    double x = p.getX();
    double y = p.getY();
    double newx = Math.cos(radians) * x - Math.sin(radians) * y;
    double newy = Math.sin(radians) * x + Math.cos(radians) * y;
    p.setLocation(newx, newy);
}

If you want to rotate a point (x, y) around another (center_x, center_y), a certain number of degrees (angle), this may help:

public float[] rotatePoint(float x, float y, float center_x, float center_y,
        double angle) {
    float x1 = x - center_x;
    float y1 = y - center_y;
    double angle2 = Math.toRadians(angle);
    float res_x, res_y;
    res_x = (float) (x1 * Math.cos(angle2) - y1 * Math.sin(angle2));
    res_y = (float) (x1 * Math.sin(angle2) + y1 * Math.cos(angle2));
    x = res_x + center_x;
    y = res_y + center_y;

    float[] res = { x, y };
    return res;
}
于 2013-10-25T18:45:05.057 回答