Reputation: 1
I have transformation the shape assignment and I am pretty much done with my coding. However, I don't know how to code for this method.
public Point2D apply(Point2D p) {
}
I talked with professor, he said, "apply() needs to create a copy of the given point and then transform the copy. You've made the copy; now transform the copy before returning it."
Can anyone code for this method for me based on what he said?
Regards,
Upvotes: 0
Views: 128
Reputation: 34166
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;
}
Upvotes: 0
Reputation: 4706
"You've made the copy; now transform the copy before returning it."
Your teacher gave you the answer, literally. Call the transform method, with the copy.
Look, I think I can hardly go any clearer than this...
Point2D newPoint = new Point2D (x, y);
transform(newPoint); // <---- You need to add this line
return newPoint;
Upvotes: 1