Hello! Please help me write a console application in the Java language.
Describe the class representing the triangle. Provide methods for creating objects, moving on a plane, resizing and rotating at a given angle. Describe properties to get the state of an object. If it is not possible to construct a triangle, an exception is thrown.
Why have you got all those setXXX() methods?
Why aren't you instantiating your objects with their correct values from their constructors?
You are throwing the wrong sort of exception; you should throw an IllegalArgumentException.
We can only help if we know the full details.
"it does not work" doesn't tell us a great deal.
You need to explain what exactly doesn't work.
Do you get a compilation error, if so what is it exactly?
Do you get an exception thrown when you run it, if so (again) what is it exactly (including the stack trace).
Does it not return the correct values, if so then what test data do you use and what results do you expect and what results do you get?
Just a note on how you are posting: it is unnecessary to quote the entire previous reply. Only quote if the context is not obvious. Usually you can just press the reply button. See WhenToQuote (that's a link).
This calculation will produce a positive area if the vertices are given in counter-clockwise order, a negative area if they are given in clockwise order. But if the inputs are bad, it will give a value very close to zero. So I suggest something like:
if(Math.abs(area)<1.0e-6){
throw new IllegalArgumentException("Vertices are colinear or specify a degenerate triangle");
}
As noted in the above post, IllegalArgumentException is an appropriate exception. Throwing an Error is seldom a good idea.
Also, a warning about something that might cause you trouble later on... Your rotation code would rotate the triangle about the origin. For graphics purposes, this is fine if the coordinates of the triangle are close to the origin. But if the triangle is not close to the origin, you could end up rotating it right out of your display area. I have no idea what problem you're trying to solve here, but keep this in mind as a potential cause if you're debugging and get a result you don't understand.
You tend to overuse the word 'this' when it would have been the default anyway. This makes your code harder to read.
There is a lot of redundancy in your code that could be collapsed by moving more responsibility to the Point class. Here is your rotate() method after adding a corresponding rotate() method to your Point class.
Assuming the three Points are vertices, then isn't it easier to check whether or not they are colinear to see if they define a triangle? That is, if the x's are equal or the y's are equal, then the points are colinear and don't define a triangle.
I stupidly wrote:Assuming the three Points are vertices, then isn't it easier to check whether or not they are colinear to see if they define a triangle? That is, if the x's are equal or the y's are equal, then the points are colinear and don't define a triangle.
Duh, I just realized it's really not as simple as Xs or Ys being equal. That only takes care of horizontal or vertical lines. Anyway...
Carey Brown wrote:Here is your rotate() method after adding a corresponding rotate() method to your Point class.
That's kind of misplaced, isn't it? You can't actually rotate a point per se because, well, it's a point. You can rotate a line a certain angle though. You can rotate a Point in reference to another point but that still brings you back to rotating a line really. To rotate a triangle ABC, you'd fix one vertex and rotate the other vertices the same angle with reference to the fixed vertex. In other words, to rotate △ABC with point A fixed, then rotate AB and AC the same angle.
Carey Brown wrote:Here is your rotate() method after adding a corresponding rotate() method to your Point class.
That's kind of misplaced, isn't it? You can't actually rotate a point per se because, well, it's a point. You can rotate a line a certain angle though. You can rotate a Point in reference to another point but that still brings you back to rotating a line really. To rotate a triangle ABC, you'd fix one vertex and rotate the other vertices the same angle with reference to the fixed vertex. In other words, to rotate △ABC with point A fixed, then rotate AB and AC the same angle.
Unless otherwise specified the rotation of a point will rotate it around the origin.
Carey Brown wrote:Unless otherwise specified the rotation of a point will rotate it around the origin.
[pedanticMode=on]
Sure. That's still in reference to another point (the origin is a Point), so basically, you're back to rotating a line. Even rotating a line is relative to a point because the displacement of any point on the line will vary depending on what the point of rotation is. Again, it really has a lot to do with the semantics you use and whether or not you have an assumed reference point.
Indeed, rotation in R2 is around a Point, can be any Point, as Gary explained. Fun is to rotate the Triangle around the center of its surrounding circle (the point where the contour lines cross)
@Junilu
if you have three different points in the R2, say a, b, c, then they are collinear if and only if the Matrix of (a - b) and (b - c) has determinant 0.
@OP
a static method distance(Point p, Point q) is to be advised.
Piet Souris wrote:
@Junilu
if you have three different points in the R2, say a, b, c, then they are collinear if and only if the Matrix of (a - b) and (b - c) has determinant 0.
Good point on the determinant. It's kind of a reminder that, if the three points define a valid triangle, they can also be used to construct the axes for a 3D coordinate system.
Incidentally, the area computation I posted earlier is algebraically equivalent to the determinant (with an appropriate assignment of variables to the vertices).