2013-10-25 139 views
0

我已經完成了形狀分配的轉換,我已經完成了我的編碼。 但是,我不知道如何編寫此方法。轉換形狀分配! (需要幫助!)

public Point2D apply(Point2D p) { 

     } 

我談過教授,他說,「申請()需要創建給定的點的副本,然後變換副本 你所做的複印件;現在返回之前轉換副本。 「

任何人都可以根據他說的代碼爲這個方法嗎?

問候,

+1

您的代碼片段不顯示*應用哪個轉換*。 –

+0

應用什麼旋轉?請提供更多信息。 – Christian

+0

不只是變換(newPoint);在返回之前? – gtgaxiola

回答

1

「你所做的複印件;現在返回之前的副本變換」。

你的老師直接給你答案。使用副本調用變換方法。

看,我想我很難去任何比這更清晰......

Point2D newPoint = new Point2D (x, y); 
    transform(newPoint); // <---- You need to add this line 
    return newPoint; 
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); 
} 

(x,y)圍繞另一個(center_x,center_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; 
} 
+0

不,他已經有了旋轉點的代碼。他只需要在給予apply()的點的副本上做。 – jwatkins