This commit is contained in:
louiscklaw
2025-02-01 02:02:19 +08:00
parent e3da66c7ba
commit 5aaa313cb2
35 changed files with 1423 additions and 0 deletions

View File

@@ -0,0 +1,26 @@
# Program code of your java file, including detailed comments
```
./main.java
```
# Input file to show your test cases
```
./single.txt
./two_line.txt
./three_line.txt
./square.txt
./hsbc.txt
```
```bash
# test
# java main.java single.txt
# java main.java two_line.txt
# java main.java three_line.txt
# java main.java square.txt
# java main.java hsbc.txt
# formal run
java main.java vertices.txt
```

View File

@@ -0,0 +1,70 @@
import java.io.*;
import java.io.File; // Import the File class
import java.io.FileNotFoundException; // Import this class to handle errors
import java.nio.file.*;
import java.nio.file.Files;
import java.util.Scanner; // Import the Scanner class to read text files
public class ReadFile {
public static int[][] ReadVerticsFile(String file_in) {
int number_of_lines = 0;
int temp[][] = new int[999][2];
int end_pattern[] = { 999, 999 };
for (int i = 0; i < temp.length; i++) {
temp[i] = end_pattern;
}
try {
File myObj = new File(file_in);
Scanner myReader = new Scanner(myObj);
while (myReader.hasNextLine()) {
String data = myReader.nextLine();
String s[] = data.split(" ", 2);
// System.out.println(s[0]);
int test_t[] = { Integer.parseInt(s[0]), Integer.parseInt(s[1]) };
temp[number_of_lines] = test_t;
number_of_lines += 1;
}
myReader.close();
System.out.println("done");
} catch (FileNotFoundException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
return temp;
}
public static int countVertics(int[][] test_vertics) {
int i = 0;
while (test_vertics[i][0] != 999 && test_vertics[i][1] != 999) {
System.out.print(test_vertics[i][0]);
System.out.print(",");
System.out.print(test_vertics[i][1]);
System.out.println();
i += 1;
}
return i;
}
public static void main(String[] args) {
String file_in = "vertices.txt";
int test_vertics[][] = new int[999][];
test_vertics = ReadVerticsFile(file_in);
int num_of_vertics = countVertics(test_vertics);
System.out.println(num_of_vertics);
}
}

View File

@@ -0,0 +1,30 @@
// Java Program to illustrate Reading from FileReader
// using BufferedReader Class
// Importing input output classes
import java.io.*;
public class Main {
public static void main(String[] args) {
int[] numbers = { 2, -9, 0, 5, 12, -25, 22, 9, 8, 12 };
int sum = 0;
Double average;
// access all elements using for each loop
// add each element in sum
for (int number : numbers) {
sum += number;
}
// get the total number of elements
int arrayLength = numbers.length;
// calculate the average
// convert the average from int to double
average = ((double) sum / (double) arrayLength);
System.out.println("Sum = " + sum);
System.out.println("Average = " + average);
}
}

View File

@@ -0,0 +1,15 @@
#!/usr/bin/env bash
# set -ex
sleep 1
# test
# java main.java single.txt
# java main.java two_line.txt
# java main.java three_line.txt
# java main.java square.txt
# java main.java hsbc.txt
# formal run
# java main.java vertices.txt

View File

@@ -0,0 +1,7 @@
-5 2
0 2
8 2
8 -11
4 2
4 5
999 999

View File

@@ -0,0 +1,7 @@
20 0
40 0
60 20
40 40
20 40
0 20
999 999

View File

@@ -0,0 +1,12 @@
// Java Program to illustrate Reading from FileReader
// using BufferedReader Class
// Importing input output classes
import java.io.*;
public class Main {
public static void main(String[] args) {
System.out.println("Hello World 123321");
}
}

View File

@@ -0,0 +1,706 @@
/********************************************************
Name of Source File: xxxxx.java
Student Name (ID): Chan Tai Man (141414141)
Course Name (Code): HD in MAD (IT114112)
Program Description:
This program try to extract the triangle embedded in
a complex polygon. It will try to define triangle's
properties and it's areas.
********************************************************/
import java.io.*;
import java.io.File; // Import the File class
import java.io.FileNotFoundException; // Import this class to handle errors
import java.nio.file.*;
import java.nio.file.Files;
import java.util.*;
import java.util.Arrays;
import java.util.Scanner; // Import the Scanner class to read text files
public class Main {
static double COMPARE_NOT_DEFINED = -3;
static double AREA_NOT_VALID = -2;
static int INVALID_TRIANGLE = -1;
static int VALID_TRIANGLE = -2;
static int RIGHT_ANGLED_TRIANGLE = -1;
static int INVALID_RIGHT_ANGLED_TRIANGLE = -2;
static int SCALENE = -1;
static int INVALID_SCALENE = -2;
static int ISOSCELES = -1;
static int INVALID_ISOSCELES = -2;
static int MAX_AREA = 1;
static int MIN_AREA = 0;
// main table length
static int LIST_LENGTH = 999;
static int NOT_FOUND = -1;
static double[] length_a = new double[LIST_LENGTH];
static double[] length_b = new double[LIST_LENGTH];
static double[] length_c = new double[LIST_LENGTH];
public static int CountVertics(double[][] test_vertics) {
// count length of input vertics
// input : list of input vertics
// output: number of valid vertics
int i = 0;
while (test_vertics[i][0] != 999 && test_vertics[i][1] != 999) {
i += 1;
}
return i;
}
public static boolean VerticsCalculatedBefore(double[][] test_vertics) {
// to check if the input vertics calculated in the past
// if not calculated before, add them into the cache
// input : list of input vertics
// output:
// true if test_vertics calculated in the past
// false if test_vertics is a fresh input
String x0 = Double.toString(test_vertics[0][0]);
String y0 = Double.toString(test_vertics[0][1]);
String p0 = x0 + y0;
String x1 = Double.toString(test_vertics[1][0]);
String y1 = Double.toString(test_vertics[1][1]);
String p1 = x1 + y1;
String x2 = Double.toString(test_vertics[2][0]);
String y2 = Double.toString(test_vertics[2][1]);
String p2 = x2 + y2;
String[] a = { p0, p1, p2 };
Arrays.sort(a);
String test = (a[0] + "," + a[1] + "," + a[2]);
for (int i = 0; i < calculated_length; i++) {
if (calculated[i].equals(test)) {
return true;
}
}
calculated[calculated_length] = test;
calculated_length += 1;
return false;
}
public static boolean ContainsTrue(boolean[] array) {
// check if true value found in a boolean array
// input : boolean array
// output: true if any element true, false if all element are false
for (boolean val : array) {
if (val) return true;
}
return false;
}
public static boolean ContainsFalse(boolean[] array) {
// check if false value found in a boolean array
// input : boolean array
// output: true if any element false, false if all element are true
for (boolean val : array) {
if (!val) return true;
}
return false;
}
public static void PrintLine(int line_length) {
// print a dash "-" with defined length
// input : line_length to define the length wanted
for (int i = 0; i < line_length; i++) {
System.out.print("-");
}
System.out.println();
return;
}
public static double AreaOfTriangle(double a, double b, double c) {
// to get the area of the triangle by its side length :
// Area = √𝑠(𝑠 𝑎)(𝑠 𝑏)(𝑠 𝑐) and s = (a+b+c)/2
// constrains:
// the sum of either two sides cannot be the same as the rest side:
// i.e.:
// a = 1, b = 2, c = 3, 1+2=3 => is a invalid input
// a = 2, b = 2, c = 3, 2+2 > 3 => is a valid input
// input :
// a => length of side a
// b => length of side b
// c => length of side c
// output:
// area of triangle by formula
double var_s = (a + b + c) / 2;
double s_minus_a = var_s - a;
double s_minus_b = var_s - b;
double s_minus_c = var_s - c;
// constrain: the sum of first 2 side has to be greater than the third side
if (s_minus_a == 0 || s_minus_b == 0 || s_minus_c == 0) {
return AREA_NOT_VALID;
}
return Math.sqrt(var_s * s_minus_a * s_minus_b * s_minus_c);
}
public static boolean TestRightAngledTriangle(double a, double b, double c) {
// to see if the parameters can form a right angle triangle
// input :
// a => length of side a
// b => length of side b
// c => length of side c
// output:
// true => right angled triangle,
// false => not right angled triangle
double[] sorted_input = { a, b, c };
Arrays.sort(sorted_input);
double a_square = sorted_input[0] * sorted_input[0];
double b_square = sorted_input[1] * sorted_input[1];
double c_square = sorted_input[2] * sorted_input[2];
double a_and_b_square = a_square + b_square;
// NOTE: loosen maths to relax right-angle triangle checking
return Math.abs(c_square - a_and_b_square) < 0.000001;
// return c_square == a_and_b_square;
}
public static boolean TestValidIsoscelesTriangle(
double a,
double b,
double c
) {
// to see if the parameters form a isosceles triangle
// definition:
// a = b or b = c or c = a
// input :
// a => length of side a
// b => length of side b
// c => length of side c
// output:
// true => isosceles triangle,
// false => not isosceles triangle
boolean a_equal_b = a == b;
boolean b_equal_c = b == c;
boolean c_equal_a = c == a;
boolean[] test_array = { a_equal_b, b_equal_c, c_equal_a };
return ContainsTrue(test_array);
}
public static boolean TestValidScaleneTriangle(double a, double b, double c) {
// to see if the parameters form a scalene triangle
// definition:
// a ≠ b ≠ c
// input :
// a => length of side a
// b => length of side b
// c => length of side c
// output:
// true => scalene triangle,
// false => not scalene triangle
boolean a_equal_b = a != b;
boolean b_equal_c = b != c;
boolean c_equal_a = c != a;
boolean[] test_array = { a_equal_b, b_equal_c, c_equal_a };
// output valid only if no false found => thus valid scalene triangle
return !ContainsFalse(test_array);
}
public static String formatPoint(double[] point_in) {
// prepare string to print for point input
// input :
// point_in => {x, y}
// output:
// formatted string
return String.format("(%1$3s,%2$3s)", point_in[0], point_in[1]);
}
public static String formatTriangleArea(double area_in) {
// prepare string to print for area output
// input :
// area_in => 1
// output:
// formatted string 1.00
return String.format("%.2f", area_in);
}
public static double TestLengthOfEachSide(double[] x1_y1, double[] x2_y2) {
// to get the length by 2 points
// input :
// x1_y1 => point start
// x2_y2 => point end
// output:
// length
double x1 = x1_y1[0];
double x2 = x2_y2[0];
double y1 = x1_y1[1];
double y2 = x2_y2[1];
double x_length = x2 - x1;
double y_length = y2 - y1;
double x_square = x_length * x_length;
double y_square = y_length * y_length;
double length = Math.sqrt(x_square + y_square);
return Math.abs(length);
}
public static boolean TestValidTriangle(double a, double b, double c) {
// check if triangle valid
// definition:
// a > 0, b > 0, c > 0
// a + b > c
// b + c > a
// c + a > b
// input :
// a,b,c => point input i.e. {x,y}
// output:
// true if points a,b,c found valid triangle, false if not invalid
double a_and_b = a + b;
double b_and_c = b + c;
double c_and_a = c + a;
// rule set
boolean a_gt_zero = a > 0;
boolean b_gt_zero = b > 0;
boolean c_gt_zero = c > 0;
boolean a_and_b_gtc = a_and_b > c;
boolean b_and_c_gta = b_and_c > a;
boolean c_and_a_gtc = c_and_a > b;
boolean[] test_arrays = {
a_gt_zero,
b_gt_zero,
c_gt_zero,
a_and_b_gtc,
b_and_c_gta,
c_and_a_gtc,
};
return !ContainsFalse(test_arrays);
}
public static int[] GetExtremeTriangleArea(
double[] area_list,
int extreme_type,
int area_list_length
) {
// get the max/min (depends) area from area_list
// input :
// area_list: list of area of triangle
// extreme_type: the extreme wanted MAX_AREA/MINAREA
// area_list_length: length of area list
// output:
// list containing the extreme found
double compare = COMPARE_NOT_DEFINED;
int out[] = new int[LIST_LENGTH];
int cursor = 0;
// init list before search
for (int i = 0; i < LIST_LENGTH; i++) {
out[i] = NOT_FOUND;
}
if (extreme_type == MIN_AREA) {
double min_value = COMPARE_NOT_DEFINED;
// get min area from list
for (int i = 0; i < area_list_length; i++) {
double tested_value = area_list[i];
if (min_value == COMPARE_NOT_DEFINED) {
if (tested_value != AREA_NOT_VALID) {
min_value = tested_value;
} else {
// invalid area, skipping
}
} else {
if (tested_value != AREA_NOT_VALID) {
if (tested_value < min_value) {
min_value = tested_value;
}
}
}
}
for (int i = 0; i < area_list_length; i++) {
double tested_value = area_list[i];
if (tested_value == min_value) {
out[cursor] = i;
cursor += 1;
}
}
} else {
double max_value = 0;
for (int i = 0; i < area_list_length; i++) {
double tested_value = area_list[i];
if (tested_value > max_value) {
max_value = tested_value;
}
}
// literate the list by the value
for (int i = 0; i < area_list_length; i++) {
double tested_value = area_list[i];
if (tested_value == max_value) {
out[cursor] = i;
cursor += 1;
}
}
}
return out;
}
public static void PrintOutput(
double[][][] point_set,
double[] area_list,
int point_length,
int[] max_area_idx,
int[] min_area_idx,
int[] valid_triangle,
int[] right_angle_triangle,
int[] scalene_triangle,
int[] isosceles_triangle
) {
// main printing function
// input :
// point_set: point list forming triangle
// area_list: area referring the point
// point_length: length of valid data
// max_area_idx: list contain index with max area
// min_area_idx: list contain index with min area
// valid_triangle: list with result of valid triangle check
// right_angle_triangle: list with result of right angle triangle check
// scalene_triangle: list with result of scalene triangle check
// isosceles_triangle: list with result of isosceles triangle check
double max_area = NOT_FOUND;
double min_area = NOT_FOUND;
if (max_area_idx[0] != NOT_FOUND) {
max_area = area_list[max_area_idx[0]];
}
if (min_area_idx[0] != NOT_FOUND) {
min_area = area_list[min_area_idx[0]];
}
PrintLine(120);
System.out.println(String.format("%1$60s", "Types of Triangles"));
PrintLine(120);
System.out.println(
String.format(
"%1$-15s %2$-15s %3$-15s %4$-33s %5$-10s",
"Point 1",
"Point 2",
"Point 3",
"Types of Triangle",
"area"
)
);
PrintLine(120);
for (int y = 0; y < point_length; y++) {
double[] point_x = point_set[y][0];
double[] point_y = point_set[y][1];
double[] point_z = point_set[y][2];
length_a[y] = TestLengthOfEachSide(point_x, point_y);
length_b[y] = TestLengthOfEachSide(point_y, point_z);
length_c[y] = TestLengthOfEachSide(point_z, point_x);
String type_of_triangle = "";
boolean test_valid_triangle = TestValidTriangle(
length_a[y],
length_b[y],
length_c[y]
);
if (valid_triangle[y] == VALID_TRIANGLE) {
// triangle, test properties
type_of_triangle +=
right_angle_triangle[y] == RIGHT_ANGLED_TRIANGLE
? "Right-angled "
: "";
type_of_triangle += scalene_triangle[y] == SCALENE ? "Scalene " : "";
// TestValidIsoscelesTriangle
type_of_triangle +=
isosceles_triangle[y] == ISOSCELES ? "Isosceles " : "";
} else {
// non-triangle, no need test the rest
type_of_triangle += "Non-triangle ";
}
System.out.print(
String.format(
"%1$-15s %2$-15s %3$-15s %4$-33s %5$-10s",
formatPoint(point_set[y][0]),
formatPoint(point_set[y][1]),
formatPoint(point_set[y][2]),
type_of_triangle,
valid_triangle[y] == VALID_TRIANGLE
? area_list[y]
: "no area calculated"
)
);
System.out.println();
}
System.out.println();
// NOTE:
// The format of the maximum and minimum area is having 2 decimal places only.
double[] t = { 1, 2 };
System.out.println();
PrintLine(120);
System.out.println();
if (max_area != min_area) {
if (max_area == NOT_FOUND) {
System.out.println("cannot find a valid max area");
} else {
System.out.println(
String.format("Maximum area %1$10s", formatTriangleArea(max_area))
);
}
for (int i : max_area_idx) {
if (i != NOT_FOUND) {
System.out.println(
String.format(
"points: %1$10s %2$10s %3$10s",
formatPoint(point_set[i][0]),
formatPoint(point_set[i][1]),
formatPoint(point_set[i][2])
)
);
}
}
System.out.println();
PrintLine(120);
System.out.println();
if (min_area == NOT_FOUND) {
System.out.println("cannot find a valid min area");
} else {
System.out.println(
String.format("Minimum area %1$10s", formatTriangleArea(min_area))
);
}
for (int i : min_area_idx) {
if (i != NOT_FOUND) {
System.out.println(
String.format(
"points: %1$10s %2$10s %3$10s",
formatPoint(point_set[i][0]),
formatPoint(point_set[i][1]),
formatPoint(point_set[i][2])
)
);
}
}
} else {
System.out.println("sorry but the area are the same");
System.out.println();
}
PrintLine(120);
}
public static void PrintError(String error) {
System.out.println();
System.out.println(error);
System.out.println();
}
public static double[][] ReadVerticsFile(String file_in) {
int number_of_lines = 0;
double temp[][] = new double[999][2];
double end_pattern[] = { 999, 999 };
for (int i = 0; i < temp.length; i++) {
temp[i] = end_pattern;
}
try {
System.out.println();
System.out.println("file input: " + file_in);
System.out.println();
File myObj = new File(file_in);
Scanner myReader = new Scanner(myObj);
while (myReader.hasNextLine()) {
String data = myReader.nextLine();
String s[] = data.split(" ", 2);
double test_t[] = {
Double.parseDouble(s[0]),
Double.parseDouble(s[1]),
};
temp[number_of_lines] = test_t;
number_of_lines += 1;
}
myReader.close();
} catch (FileNotFoundException e) {
PrintError("An error occurred during reading points list");
System.exit(-1);
}
return temp;
}
static String[] calculated = new String[999];
static int calculated_length = 0;
public static void main(String[] args) {
String file_in = args[0];
double point_list[][] = new double[LIST_LENGTH][];
point_list = ReadVerticsFile(file_in);
int num_of_vertics = CountVertics(point_list);
if (num_of_vertics < 3) {
System.out.println("number of vertics: " + num_of_vertics);
PrintError("sorry, at least 3 points to carry on");
System.exit(-1);
}
double vertics_list[][][] = new double[LIST_LENGTH][3][2];
int vertics_list_length = 0;
for (int i_a = 0; i_a < num_of_vertics; i_a++) {
for (int i_b = 0; i_b < num_of_vertics; i_b++) {
double[] xy_a = point_list[i_a];
double[] xy_b = point_list[i_b];
double[][] list = { xy_a, xy_b };
long t = Arrays.stream(list).distinct().count();
if (t >= 2) {
for (int i_c = 0; i_c < num_of_vertics; i_c++) {
double[] xy_c = point_list[i_c];
double[][] list_3 = { xy_a, xy_b, xy_c };
long t_3 = Arrays.stream(list_3).distinct().count();
if (t_3 >= 3 && !VerticsCalculatedBefore(list_3)) {
vertics_list[vertics_list_length] = list_3;
vertics_list_length += 1;
} else {
// System.out.println("a and b and c are same");
}
}
} else {
// System.out.println("a and b are same");
}
}
}
double[] area_of_triangle = new double[LIST_LENGTH];
// sorted the list to be calculated
for (int j = 0; j < vertics_list_length; j++) {
double[] point_x = vertics_list[j][0];
double[] point_y = vertics_list[j][1];
double[] point_z = vertics_list[j][2];
double length_a = TestLengthOfEachSide(point_x, point_y);
double length_b = TestLengthOfEachSide(point_y, point_z);
double length_c = TestLengthOfEachSide(point_z, point_x);
area_of_triangle[j] = AreaOfTriangle(length_a, length_b, length_c);
}
int max_area_idx[] = GetExtremeTriangleArea(
area_of_triangle,
MAX_AREA,
vertics_list_length
);
int min_area_idx[] = GetExtremeTriangleArea(
area_of_triangle,
MIN_AREA,
vertics_list_length
);
// prepare output list
int[] valid_triangle = new int[LIST_LENGTH];
int[] right_angle_triangle = new int[LIST_LENGTH];
int[] scalene_triangle = new int[LIST_LENGTH];
int[] isosceles_triangle = new int[LIST_LENGTH];
for (int i = 0; i < LIST_LENGTH; i++) {
valid_triangle[i] = INVALID_TRIANGLE;
right_angle_triangle[i] = INVALID_RIGHT_ANGLED_TRIANGLE;
scalene_triangle[i] = INVALID_SCALENE;
isosceles_triangle[i] = INVALID_ISOSCELES;
}
for (int y = 0; y < vertics_list_length; y++) {
double[] point_x = vertics_list[y][0];
double[] point_y = vertics_list[y][1];
double[] point_z = vertics_list[y][2];
length_a[y] = TestLengthOfEachSide(point_x, point_y);
length_b[y] = TestLengthOfEachSide(point_y, point_z);
length_c[y] = TestLengthOfEachSide(point_z, point_x);
valid_triangle[y] =
TestValidTriangle(length_a[y], length_b[y], length_c[y])
? VALID_TRIANGLE
: INVALID_TRIANGLE;
right_angle_triangle[y] =
TestRightAngledTriangle(length_a[y], length_b[y], length_c[y])
? RIGHT_ANGLED_TRIANGLE
: INVALID_RIGHT_ANGLED_TRIANGLE;
scalene_triangle[y] =
TestValidScaleneTriangle(length_a[y], length_b[y], length_c[y])
? SCALENE
: INVALID_SCALENE;
isosceles_triangle[y] =
TestValidIsoscelesTriangle(length_a[y], length_b[y], length_c[y])
? ISOSCELES
: INVALID_ISOSCELES;
}
PrintOutput(
vertics_list,
area_of_triangle,
vertics_list_length,
max_area_idx,
min_area_idx,
valid_triangle,
right_angle_triangle,
scalene_triangle,
isosceles_triangle
);
}
}

View File

@@ -0,0 +1,46 @@
// Java Program to illustrate Reading from FileReader
// using BufferedReader Class
// Importing input output classes
import java.io.*;
public class Main {
public static void main(String[] args) {
// int[] numbers = {2, -9, 0, 5, 12, -25, 22, 9, 8, 12};
// int sum = 0;
// Double average;
// // access all elements using for each loop
// // add each element in sum
// for (int number: numbers) {
// sum += number;
// }
// // get the total number of elements
// int arrayLength = numbers.length;
// // calculate the average
// // convert the average from int to double
// average = ((double)sum / (double)arrayLength);
// System.out.println("Sum = " + sum);
// System.out.println("Average = " + average);
double[][] matrix = {
{ -5, 2 },
{ 0, 2 },
{ 8, 2 },
{ 8, -11 },
{ 4, 2 },
{ 4, 5 },
};
for (double[] test : matrix) {
for (double tt : test) {
System.out.print(tt);
}
System.out.println("");
}
}
}

View File

@@ -0,0 +1,2 @@
20 0
999 999

View File

@@ -0,0 +1,5 @@
0 0
10 0
0 10
10 10
999 999

View File

@@ -0,0 +1,74 @@
// Java Program to illustrate Reading from FileReader
// using BufferedReader Class
// Importing input output classes
import java.io.*;
import java.util.Arrays;
public class Main {
public static boolean containsTrue(boolean[] array) {
for (boolean val : array) {
if (val) return true;
}
return false;
}
public static boolean checkAnySamePoint(double[] a, double[] b, double[] c) {
boolean a_bSame = a == b;
boolean a_cSame = a == c;
boolean b_cSame = b == c;
boolean[] testArray = { a_bSame, a_cSame, b_cSame };
return containsTrue(testArray);
}
public static void main(String[] args) {
System.out.println("test_iterate");
double[][] point_list = {
{ -5, 2 },
{ 0, 2 },
{ 8, 2 },
{ 8, -11 },
{ 4, 2 },
{ 4, 5 },
};
double[] test_d = { 1, 2, 3 };
for (double d : test_d) {
System.out.println(d);
}
// 1
for (double[] xy_a : point_list) {
for (double[] xy_b : point_list) {
// test if same point
double[][] list = { xy_a, xy_b };
long t = Arrays.stream(list).distinct().count();
// if a and b is not the same point
if (t == 2) {
for (double[] xy_c : point_list) {
// if a and b and c is not the same point
double[][] list_t = { xy_a, xy_b, xy_c };
long tt = Arrays.stream(list_t).distinct().count();
if (tt == 3) {
System.out.print(xy_a[0] + "/" + xy_a[1]);
System.out.print("," + xy_b[0] + "/" + xy_b[1]);
System.out.print("," + xy_c[0] + "/" + xy_c[1]);
System.out.print("==" + checkAnySamePoint(xy_a, xy_b, xy_c));
System.out.println("");
} else {
// skip if a and b and c is same
}
}
} else {
// skip if a and b is same
}
}
}
}
}

View File

@@ -0,0 +1,4 @@
20 0
20 0
20 0
999 999

View File

@@ -0,0 +1,141 @@
// Java Program to illustrate Reading from FileReader
// using BufferedReader Class
// Importing input output classes
import java.io.*;
import java.lang.Math;
import java.util.Arrays;
public class Main {
public static double TestLengthOfEachSide(double[] x1y1, double[] x2y2) {
System.out.println("TestLengthOfEachSide");
System.out.println(x1y1[0] + "," + x1y1[1]);
System.out.println(x2y2[0] + "," + x2y2[1]);
double x1 = x1y1[0];
double x2 = x2y2[0];
double y1 = x1y1[1];
double y2 = x2y2[1];
double x_length = x2 - x1;
double y_length = y2 - y1;
double x_square = x_length * x_length;
double y_square = y_length * y_length;
double length = Math.sqrt(x_square + y_square);
return length;
}
public static boolean containsTrue(boolean[] array) {
for (boolean val : array) {
if (val) return true;
}
return false;
}
public static boolean containsFalse(boolean[] array) {
for (boolean val : array) {
if (!val) return true;
}
return false;
}
public static boolean TestValidTriangle(double a, double b, double c) {
// true = valid triangle, false = invalid triangle
System.out.println("TestValidTriangle");
// a > 0
boolean aGtZero = a > 0;
boolean bGtZero = b > 0;
boolean cGtZero = c > 0;
double aAndb = a + b;
double bAndc = b + c;
double cAnda = c + a;
boolean aAndbGtc = aAndb > c;
boolean bAndcGta = bAndc > a;
boolean cAndaGtc = cAnda > b;
boolean[] bArrays = {
aGtZero,
bGtZero,
cGtZero,
aAndbGtc,
bAndcGta,
cAndaGtc,
};
return !containsFalse(bArrays);
}
public static boolean TestRightAngledTriangle(double a, double b, double c) {
// true => right angled triangle, false => not right angled triangle
System.out.println("TestRightAngledTriangle");
double[] arr = { a, b, c };
Arrays.sort(arr);
double largest_side = arr[2];
double a_square = arr[0] * arr[0];
double b_square = arr[1] * arr[1];
double c_square = arr[2] * arr[2];
double a_and_b_square = a_square + b_square;
return Math.abs(c_square - a_and_b_square) < 0.000001;
}
public static boolean TestValidScaleneTriangle(double a, double b, double c) {
System.out.println("TestValidScaleneTriangle");
boolean aEqualb = a != b;
boolean bEqualc = b != c;
boolean[] testArray = { aEqualb, bEqualc };
System.out.println(aEqualb);
System.out.println(bEqualc);
// either a equal b or b equal c will found true here
return containsTrue(testArray);
}
public static boolean TestValidIsoscelesTriangle(
double a,
double b,
double c
) {
boolean aEqualb = a == b;
boolean bEqualc = b == c;
boolean cEquala = c == a;
boolean[] testArray = { aEqualb, bEqualc, cEquala };
return containsTrue(testArray);
}
public static double AreaOfTriangle(double a, double b, double c) {
double var_s = (a + b + c) / 2;
double s_minus_a = var_s - a;
double s_minus_b = var_s - b;
double s_minus_c = var_s - c;
return Math.sqrt(var_s * s_minus_a * s_minus_b * s_minus_c);
}
public static void main(String[] args) {
System.out.println("triangle rules");
// i. The length of each side can be calculated via the coordinates of the two vertices (x1, y1)
// and (x2, y2) connecting the side by using the formula:
double[] a = { 1, 2 };
double[] b = { 3, 4 };
double[] c = { 5, 6 };
// System.out.println(TestLengthOfEachSide(a,b));
// System.out.println(TestValidTriangle(3,3,3));
// System.out.println(TestRightAngledTriangle(1,2,Math.sqrt(5)));
// System.out.println(TestValidIsoscelesTriangle(2,2,3));
// System.out.println(TestValidScaleneTriangle(2,1,1));
// System.out.println(AreaOfTriangle(2,1,1));
System.out.println("done");
}
}

View File

@@ -0,0 +1,3 @@
20 0
20 0
999 999

View File

@@ -0,0 +1,7 @@
-5 2
0 2
8 2
8 -11
4 2
4 5
999 999