Point-In-Triangle Test

Try out this little applet. Make sure you open the Java Console while you run it so you can get additional feedback which will provide clues as to how this applet works. In general, when you click inside the triangle, the triangle changes color. When you click outside the triangle nothing happens. But in the Java Console you get additional information to help you understand the logic behind the visual presentation. You will also need to study the code.

Here are the files for this applet:
Point2D.java
Tools2D.java
Triangle_click.java

The most interesting and most important of these files is the Tools2D.java file: class Tools2D{ static int area2(Point2D A, Point2D B, Point2D C){ int x = (A.x - C.x) * (B.y - C.y) - (A.y - C.y) * (B.x - C.x); System.out.println("RESULT: "+x); return x; } static boolean insideTriangle(Point2D A, Point2D B, Point2D C, Point2D P){ boolean inside = Tools2D.area2(A, B, P) >= 0 && Tools2D.area2(B, C, P) >= 0 && Tools2D.area2(C, A, P) >=0; System.out.println("---------------"); return inside; } } The insideTriangle(Point2D,Point2D,Point2D,Point2D) method is called from the Triangle_click class with the last point representing the coordinates of a mouse click. If the mouse click is within the bounds of the triangle then all three calls to Tools2D.area2 will return a positive value. The value of each individual call gets displayed in the Java console. You should notice that if a value is negative then no additional calls to Tools2D.area2 are made.

The math behind why this works is a little complicated, but it has to do with vectors and operations done with vectors. There is an alternative trigonometric approach to solving this problem, but it's a little complicated also. Just make sure you see how the logic of this applet works and be ready to apply it to the assignment.
- - - - - - - - - - - - - - - - - - - - - - - - - - - -
ASSIGNMENT:
Inspect this applet and recreate it. You should spend some time playing with it so you become aware of all the little things it does. Make sure that you know what happens when the triangles are red, white, and blue (in any order) or when all three are the same color.