How to Find a Triangle Within a Triangle Using Java Code
By calculating the midpoint of the sides of a triangle, you can connect those midpoints to determine smaller sub-triangles. You accomplish this in Java by beginning with the three coordinates of a triangle, calculating the differences of those coordinates to find the midpoints of all the sides, and then constructing a new triangle out of those midpoints.
Instructions
-
-
1
Create a class that represents a triangle, storing three points representing x and y coordinates. These points will represent the basic triangle (Source 1):
class T{
public static void main(String[] args){
int[] s1 = {10, 6};
int[] s2 = {16, 16};
int[] s3 = {4, 0};}
} -
2
Determine the midpoint of two different sides (Source 1):
int diff_side1x = s2[0] - s1[0];
int diff_side1y = s2[1] - s1[1];
int[] mid_side1 = {diff_side1x + s1[0], diff_side1y + s1[1]};int diff_side2x = s2[0] - s3[0];
int diff_side2y = s2[1] - s3[1];
int[] mid_side2 = {diff_side2x + s3[0], diff_side2y + s3[1]}; -
-
3
Connect the two midpoints to one of the already existing points. These three points will form a triangle inscribed in the original (Source 1):
//original triangle
s1;
s2;
s3;//internal triangle
s3;
mid_side1;
mid_side2;//internal triangle
s2;
mid_side1;
mid_side2;
-
1