There is task to implement some triangle class with interface like this
public interface Triangle {
void moveApex(Point from, Point to);
List<Point> getApexes();
void rotate(double angle);
void setLocation(Point p);
Point getLocation();
void setSize(Dimension d);
Dimension getSize();
}
Where Point and Dimension are some pairs of integers. In what way would you implement it? Would you creates some fields with Apexes or you will operate with 2 angles and one base line or smt else?
Thanks.
Points can – as you mentioned – be represented as a pair of points. Let’s call such a pair a 2-dimensional vector. A vector can be interpreted as an arrow from the origin (of your coordinate system) to a point or just as an arrow starting anywhere indication a direction or movement. I.e. a vector is a representation for both a single point and a movement.
Thus, you can define a Triangle using vectors. Vectors can be transformed via 2×2-dimensioal arrays of numbers called matrices. With the help of these matrices, arbitrary transformations can be expressed nicely.
Here are some links that could help you and give more detailed information.
Vectors: http://en.wikipedia.org/wiki/Euclidean_vector
Triangle overview: http://en.wikipedia.org/wiki/Triangle
Matrix overview: http://en.wikipedia.org/wiki/Matrix_multiplication
Rotation via matrices: http://en.wikipedia.org/wiki/Rotation_matrix
setLocationrequires vector movement (i.e. vector addition)setSizemight require vector multiplicationrotateImplement matrix rotationSo please read the above links, and just convert the maths to Java. The maths itself isn’t that complicated so it should be possible to do so without much difficulty.
Of course there are supposedly plenty of implementations around.