-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRightTriangleswithIntegerCoordinates.java
More file actions
113 lines (96 loc) · 4.01 KB
/
RightTriangleswithIntegerCoordinates.java
File metadata and controls
113 lines (96 loc) · 4.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package dev;
public class RightTriangleswithIntegerCoordinates {
public static void main(String[] args) {
// Test with the given constraints
int maxCoord = 50;
int count = countRightTriangles(maxCoord);
System.out.println("Number of right triangles with coordinates from 0 to " + maxCoord + ": " + count);
// Test with the example from the problem (coordinates 0 to 2)
int smallCount = countRightTriangles(2);
System.out.println("Number of right triangles with coordinates from 0 to 2: " + smallCount);
}
/**
* Counts the number of right triangles that can be formed by connecting
* two points P(x1, y1) and Q(x2, y2) to the origin O(0, 0).
*
* @param maxCoord Maximum coordinate value (inclusive)
* @return Number of right triangles
*/
public static int countRightTriangles(int maxCoord) {
int count = 0;
// Iterate through all possible combinations of two points
for (int x1 = 0; x1 <= maxCoord; x1++) {
for (int y1 = 0; y1 <= maxCoord; y1++) {
for (int x2 = 0; x2 <= maxCoord; x2++) {
for (int y2 = 0; y2 <= maxCoord; y2++) {
// Skip if both points are at origin or if points are identical
if ((x1 == 0 && y1 == 0) || (x2 == 0 && y2 == 0) ||
(x1 == x2 && y1 == y2)) {
continue;
}
// Check if triangle OPQ has a right angle
if (hasRightAngle(x1, y1, x2, y2)) {
count++;
}
}
}
}
}
// Each triangle is counted twice (once for P,Q and once for Q,P)
return count / 2;
}
/**
* Checks if triangle OPQ has a right angle where O is origin (0,0),
* P is (x1, y1), and Q is (x2, y2).
*
* @param x1, y1 Coordinates of point P
* @param x2, y2 Coordinates of point Q
* @return true if triangle has a right angle
*/
private static boolean hasRightAngle(int x1, int y1, int x2, int y2) {
// Case 1: Right angle at origin O
// Vectors OP = (x1, y1) and OQ = (x2, y2)
// Dot product: x1*x2 + y1*y2
if (x1 * x2 + y1 * y2 == 0) {
return true;
}
// Case 2: Right angle at point P
// Vectors PO = (-x1, -y1) and PQ = (x2-x1, y2-y1)
// Dot product: (-x1)*(x2-x1) + (-y1)*(y2-y1) = -x1*(x2-x1) - y1*(y2-y1)
if (-x1 * (x2 - x1) - y1 * (y2 - y1) == 0) {
return true;
}
// Case 3: Right angle at point Q
// Vectors QO = (-x2, -y2) and QP = (x1-x2, y1-y2)
// Dot product: (-x2)*(x1-x2) + (-y2)*(y1-y2) = -x2*(x1-x2) - y2*(y1-y2)
if (-x2 * (x1 - x2) - y2 * (y1 - y2) == 0) {
return true;
}
return false;
}
/**
* Alternative method that shows detailed analysis for smaller coordinate ranges
*/
public static void analyzeSmallRange(int maxCoord) {
System.out.println("\nDetailed analysis for coordinates 0 to " + maxCoord + ":");
int count = 0;
for (int x1 = 0; x1 <= maxCoord; x1++) {
for (int y1 = 0; y1 <= maxCoord; y1++) {
for (int x2 = x1; x2 <= maxCoord; x2++) {
for (int y2 = (x2 == x1 ? y1 + 1 : 0); y2 <= maxCoord; y2++) {
// Skip if both points are at origin
if ((x1 == 0 && y1 == 0) || (x2 == 0 && y2 == 0)) {
continue;
}
if (hasRightAngle(x1, y1, x2, y2)) {
count++;
System.out.printf("Right triangle: O(0,0), P(%d,%d), Q(%d,%d)%n",
x1, y1, x2, y2);
}
}
}
}
}
System.out.println("Total right triangles: " + count);
}
}