Skip to content

Commit 0aea5c7

Browse files
committed
es trian
1 parent 004e27d commit 0aea5c7

17 files changed

+2845
-1105
lines changed

.github/scripts/generate-site.py

Lines changed: 544 additions & 277 deletions
Large diffs are not rendered by default.

Triangolo/bin/Punto.class

1.41 KB
Binary file not shown.

Triangolo/bin/Triangolo.class

4.31 KB
Binary file not shown.

Triangolo/src/Punto.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
public class Punto {
2+
private static final double EPSILON = 1e-9;
3+
private double x;
4+
private double y;
5+
6+
public Punto(double x, double y) {
7+
this.x = x;
8+
this.y = y;
9+
}
10+
11+
public Punto(Punto p) {
12+
this(p.x, p.y);
13+
}
14+
15+
public void setX(double x) {
16+
this.x = x;
17+
}
18+
19+
public void setY(double y) {
20+
this.y = y;
21+
}
22+
23+
public double getX() {
24+
return x;
25+
}
26+
27+
public double getY() {
28+
return y;
29+
}
30+
31+
public double distanza(Punto p) {
32+
double dx = this.x - p.x;
33+
double dy = this.y - p.y;
34+
return Math.sqrt(dx * dx + dy * dy);
35+
}
36+
37+
public boolean equals(Punto p) {
38+
if (p == null) {
39+
return false;
40+
}
41+
return Math.abs(this.x - p.x) < EPSILON && Math.abs(this.y - p.y) < EPSILON;
42+
}
43+
44+
@Override
45+
public String toString() {
46+
return "(" + x + ", " + y + ")";
47+
}
48+
}

Triangolo/src/Triangolo.java

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
public class Triangolo {
2+
private Punto p1;
3+
private Punto p2;
4+
private Punto p3;
5+
6+
public Triangolo(Punto p1, Punto p2, Punto p3) {
7+
points(p1, p2, p3);
8+
}
9+
10+
public Triangolo(Triangolo t) {
11+
this(t.p1, t.p2, t.p3);
12+
}
13+
14+
public void setP1(Punto p1) {
15+
points(p1, this.p2, this.p3);
16+
}
17+
18+
public void setP2(Punto p2) {
19+
points(this.p1, p2, this.p3);
20+
}
21+
22+
public void setP3(Punto p3) {
23+
points(this.p1, this.p2, p3);
24+
}
25+
26+
public Punto getP1() {
27+
return new Punto(this.p1);
28+
}
29+
30+
public Punto getP2() {
31+
return new Punto(this.p2);
32+
}
33+
34+
public Punto getP3() {
35+
return new Punto(this.p3);
36+
}
37+
38+
public double perim() {
39+
double lato1 = len(this.p1, this.p2);
40+
double lato2 = len(this.p2, this.p3);
41+
double lato3 = len(this.p3, this.p1);
42+
return lato1 + lato2 + lato3;
43+
}
44+
45+
public double area() {
46+
double lato1 = len(this.p1, this.p2);
47+
double lato2 = len(this.p2, this.p3);
48+
double lato3 = len(this.p3, this.p1);
49+
double semiP = (lato1 + lato2 + lato3) / 2.0;
50+
double areaQuad = semiP
51+
* (semiP - lato1)
52+
* (semiP - lato2)
53+
* (semiP - lato3);
54+
55+
return Math.sqrt(Math.max(areaQuad, 0.0));
56+
}
57+
58+
public boolean equals(Triangolo t) {
59+
if (t == null) {
60+
return false;
61+
}
62+
return this.p1.equals(t.p1)
63+
&& this.p2.equals(t.p2)
64+
&& this.p3.equals(t.p3);
65+
}
66+
67+
public boolean equivale(Triangolo t) {
68+
if (t == null) {
69+
return false;
70+
}
71+
72+
double[] latiMiei = {len(this.p1, this.p2), len(this.p2, this.p3), len(this.p3, this.p1)};
73+
double[] latiSuoi = {len(t.p1, t.p2), len(t.p2, t.p3), len(t.p3, t.p1)};
74+
75+
sort(latiMiei);
76+
sort(latiSuoi);
77+
78+
for (int i = 0; i < latiMiei.length; i++) {
79+
if (latiMiei[i] != latiSuoi[i]) {
80+
return false;
81+
}
82+
}
83+
return true;
84+
}
85+
86+
@Override
87+
public String toString() {
88+
return "Tri{" +
89+
"p1=" + this.p1 +
90+
", p2=" + this.p2 +
91+
", p3=" + this.p3 +
92+
'}';
93+
}
94+
95+
private void points(Punto nuovoP1, Punto nuovoP2, Punto nuovoP3) {
96+
97+
this.p1 = new Punto(nuovoP1);
98+
this.p2 = new Punto(nuovoP2);
99+
this.p3 = new Punto(nuovoP3);
100+
}
101+
102+
private boolean stannoInRiga(Punto pt1, Punto pt2, Punto pt3) {
103+
double areaDoppia = pt1.getX() * (pt2.getY() - pt3.getY())
104+
+ pt2.getX() * (pt3.getY() - pt1.getY())
105+
+ pt3.getX() * (pt1.getY() - pt2.getY());
106+
return areaDoppia == 0;
107+
}
108+
109+
private double len(Punto pt1, Punto pt2) {
110+
return pt1.distanza(pt2);
111+
}
112+
113+
private void sort(double[] lati) {
114+
for (int i = 0; i < lati.length - 1; i++) {
115+
for (int j = i + 1; j < lati.length; j++) {
116+
if (lati[i] > lati[j]) {
117+
double tmp = lati[i];
118+
lati[i] = lati[j];
119+
lati[j] = tmp;
120+
}
121+
}
122+
}
123+
}
124+
125+
public static void main(String[] args) {
126+
Punto p1 = new Punto(0, 0);
127+
Punto p2 = new Punto(6, 0);
128+
Punto p3 = new Punto(2, 5);
129+
130+
Triangolo tri = new Triangolo(p1, p2, p3);
131+
System.out.println("Tri inizl: " + tri);
132+
System.out.println("Perimetr: " + tri.perim());
133+
System.out.println("Are: " + tri.area());
134+
135+
Triangolo copia = new Triangolo(tri);
136+
System.out.println("\nCopi: " + copia);
137+
System.out.println("equals(copi) -> " + tri.equals(copia));
138+
139+
Triangolo stessiPunti = new Triangolo(
140+
new Punto(2, 5),
141+
new Punto(0, 0),
142+
new Punto(6, 0)
143+
);
144+
System.out.println("\nStessi pnti, ordin divers: " + stessiPunti);
145+
System.out.println("equals(stessiPnti) -> " + tri.equals(stessiPunti));
146+
System.out.println("equivale(stessiPnti) -> " + tri.equivale(stessiPunti));
147+
148+
Triangolo triSimile = new Triangolo(
149+
new Punto(0, 0),
150+
new Punto(3, 0),
151+
new Punto(1, 2.5)
152+
);
153+
System.out.println("\nLati in rapprt: " + triSimile);
154+
System.out.println("equivale(triSimile) -> " + tri.equivale(triSimile));
155+
}
156+
}

0 commit comments

Comments
 (0)