转换形状分配! (需要帮忙!)

时间:2013-10-25 18:38:48

标签: java inheritance

我对形状赋值进行了转换,而且我的编码工作已经完成了。 但是,我不知道如何为这种方法编写代码。

    public Point2D apply(Point2D p) {

        }

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

根据他所说的话,任何人都能为我编码这种方法吗?

此致

2 个答案:

答案 0 :(得分:1)

“你已经制作了副本;现在转换副本,然后再返回。”

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

看,我认为我几乎不能比这更清楚......

    Point2D newPoint = new Point2D (x, y);
    transform(newPoint);  // <---- You need to add this line
    return newPoint;

答案 1 :(得分:0)

在您的代码中,您必须使用transform()方法:

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);
}

如果你想围绕另一个点(center_x,center_y)旋转一个点(x,y),一定的度数(角度),这可能会有所帮助:

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;
}