Lab #3: Computing the Area of Traingles

The goal of this lab is to write a class Triangles that computes the area for all triangles ABC, whose corners A, B, and C fall on the perimeter of the circle of radius one with the origin O = (0,0) as the center satsifying the following conditions:

The Conditions  To make the coding simple, the corner A will always be positioned on (1,0). The angle ∠BOA, that is, the angle that the line BO and the line AO form, when measured counter-clockwise from AO is equal to iπ/N for some fixed positive integer N and some integer i between 1 and 2N-1; that is, the angle is a multiple of π/N. The angle ∠COA, measured in a similar manner, is equal to jπ/N for some integer j between 1 and 2N-1. We assume that the three points A, B, and C do not coincide with each other and that the angle ∠COA is greater than the angle ∠BOA. This means that 1 <= i < j <= 2N-1.

For example, if the integer N is 2, then the possible combinations of the two angles (∠BOA,∠COA) are:
(π/2, π)
(π/2, 3π/2)
(π, 3π/2)

Also, if the integer N is 3, then the possible combinations of the two angles (∠BOA,∠COA) are:
(π/3, 2π/3)
(π/3, π)
(π/3, 4π/3)
(π/3, 5π/3)
(2π/3, π)
(2π/3, 4π/3)
(2π/3, 5π/3)
(π, 4π/3)
(π, 5π/3)
(4π/3, 5π/3)

Since we are assuming that B and C are on the circle of radius one with the origin O as the center, if B or C is at an angle θ, then its xy-coordinates are (cos(θ),sin(θ)).

Suppose we use variables x1, y1, x2, and y2 to represent B and C as B = (x1,y1) and C = (x2,y2). With the positive N fixed, we can use a double for-loop to generate all the angle combinations for B and C: if we look at the multiplicative factors, i and j, to π/N to the angles, the combinations are:
(1, 2)
(1, 3)
...
(1, 2N-1)
(2, 3)
...
(2, 2N-1)
...
(2N-2, 2N-1)

Once a double for-loop to generate the angle combinations has been established, by using the above mapping from the angles to the coordinates, for each angle combination we can obtain the coordinates of B and C.

Once the coordinates are given, the are of the triangle formed by A, B, and C can be computed by obtaining the side lengths, d1, d2, and d3, and then use Heron's formula

the square root of s(s-d1)(s-d2)(s-d3).

Here s = (d1 + d2 + d3)/2.

Your code should consist of an assignment to a variable corresponding to N, a double for loop, and inside the loop calculation of the coordinates and the area in the manner described in the above. Also, your code must produce on screen the coordinates and the area.

Here is a sample output for N=4

number=4
x1=0.7071067811865476, y1=0.7071067811865475, x2=6.123233995736766E-17, y2=1.0, area=0.20710678118654743
x1=0.7071067811865476, y1=0.7071067811865475, x2=-0.7071067811865475, y2=0.7071067811865476, area=0.4999999999999991
x1=0.7071067811865476, y1=0.7071067811865475, x2=-1.0, y2=1.2246467991473532E-16, area=0.7071067811865477
x1=0.7071067811865476, y1=0.7071067811865475, x2=-0.7071067811865477, y2=-0.7071067811865475, area=0.7071067811865476
x1=0.7071067811865476, y1=0.7071067811865475, x2=-1.8369701987210297E-16, y2=-1.0, area=0.4999999999999996
x1=0.7071067811865476, y1=0.7071067811865475, x2=0.7071067811865474, y2=-0.7071067811865477, area=0.20710678118654746
x1=6.123233995736766E-17, y1=1.0, x2=-0.7071067811865475, y2=0.7071067811865476, area=0.5
x1=6.123233995736766E-17, y1=1.0, x2=-1.0, y2=1.2246467991473532E-16, area=0.9999999999999998
x1=6.123233995736766E-17, y1=1.0, x2=-0.7071067811865477, y2=-0.7071067811865475, area=1.2071067811865481
x1=6.123233995736766E-17, y1=1.0, x2=-1.8369701987210297E-16, y2=-1.0, area=0.9999999999999997
x1=6.123233995736766E-17, y1=1.0, x2=0.7071067811865474, y2=-0.7071067811865477, area=0.5000000000000007
x1=-0.7071067811865475, y1=0.7071067811865476, x2=-1.0, y2=1.2246467991473532E-16, area=0.7071067811865477
x1=-0.7071067811865475, y1=0.7071067811865476, x2=-0.7071067811865477, y2=-0.7071067811865475, area=1.2071067811865472
x1=-0.7071067811865475, y1=0.7071067811865476, x2=-1.8369701987210297E-16, y2=-1.0, area=1.2071067811865475
x1=-0.7071067811865475, y1=0.7071067811865476, x2=0.7071067811865474, y2=-0.7071067811865477, area=0.7071067811865477
x1=-1.0, y1=1.2246467991473532E-16, x2=-0.7071067811865477, y2=-0.7071067811865475, area=0.7071067811865475
x1=-1.0, y1=1.2246467991473532E-16, x2=-1.8369701987210297E-16, y2=-1.0, area=0.9999999999999996
x1=-1.0, y1=1.2246467991473532E-16, x2=0.7071067811865474, y2=-0.7071067811865477, area=0.7071067811865485
x1=-0.7071067811865477, y1=-0.7071067811865475, x2=-1.8369701987210297E-16, y2=-1.0, area=0.4999999999999996
x1=-0.7071067811865477, y1=-0.7071067811865475, x2=0.7071067811865474, y2=-0.7071067811865477, area=0.5000000000000007
x1=-1.8369701987210297E-16, y1=-1.0, x2=0.7071067811865474, y2=-0.7071067811865477, area=0.20710678118654746

Challenge Problem If you are interested, you can trackle the following beautification problem:

The output shown in the above is unwieldy since so many digits are printed for each number. This is the effect of using the plain System.out.print by supplying plainly a double variable. You can avoid this problem by dropping all the digits below a certain digit below decimal point. For a double variable, say x, instead of the plain version System.out.print( x ), if you execute

System.out.print( Math.round( x * 10000.0) / 10000.0 );

the value of x is multiplied by ten thousands, rounded to the closest integer, and then divided by ten thousand again. The use of 10000.0 as the dividor has the effect of the last division calculated as the division of real numbers, not the division of integers.

Using this technique, the output can look like:

number=4
x1=0.7071, y1=0.7071, x2=0.0, y2=1.0, area=0.2071
x1=0.7071, y1=0.7071, x2=-0.7071, y2=0.7071, area=0.5
x1=0.7071, y1=0.7071, x2=-1.0, y2=0.0, area=0.7071
x1=0.7071, y1=0.7071, x2=-0.7071, y2=-0.7071, area=0.7071
x1=0.7071, y1=0.7071, x2=0.0, y2=-1.0, area=0.5
x1=0.7071, y1=0.7071, x2=0.7071, y2=-0.7071, area=0.2071
x1=0.0, y1=1.0, x2=-0.7071, y2=0.7071, area=0.5
x1=0.0, y1=1.0, x2=-1.0, y2=0.0, area=1.0
x1=0.0, y1=1.0, x2=-0.7071, y2=-0.7071, area=1.2071
x1=0.0, y1=1.0, x2=0.0, y2=-1.0, area=1.0
x1=0.0, y1=1.0, x2=0.7071, y2=-0.7071, area=0.5
x1=-0.7071, y1=0.7071, x2=-1.0, y2=0.0, area=0.7071
x1=-0.7071, y1=0.7071, x2=-0.7071, y2=-0.7071, area=1.2071
x1=-0.7071, y1=0.7071, x2=0.0, y2=-1.0, area=1.2071
x1=-0.7071, y1=0.7071, x2=0.7071, y2=-0.7071, area=0.7071
x1=-1.0, y1=0.0, x2=-0.7071, y2=-0.7071, area=0.7071
x1=-1.0, y1=0.0, x2=0.0, y2=-1.0, area=1.0
x1=-1.0, y1=0.0, x2=0.7071, y2=-0.7071, area=0.7071
x1=-0.7071, y1=-0.7071, x2=0.0, y2=-1.0, area=0.5
x1=-0.7071, y1=-0.7071, x2=0.7071, y2=-0.7071, area=0.5
x1=0.0, y1=-1.0, x2=0.7071, y2=-0.7071, area=0.2071

You may use the template Triangles.java

Go back to the lab main page

Go back to the home page