-
Notifications
You must be signed in to change notification settings - Fork 0
/
gauss.js
145 lines (108 loc) · 3.13 KB
/
gauss.js
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
function init()
{
sessionStorage.setItem("ptnum", "0");
}
function addPoint()
{
let table = document.getElementById("table");
// update pts counter
let ptnum = Number(sessionStorage.getItem("ptnum"));
ptnum++;
sessionStorage.setItem("ptnum", ptnum);
// remove last row (button)
table.deleteRow(ptnum);
// add a new row to the table for the new point
let new_row = document.createElement("tr");
let cell1 = document.createElement("td");
let cell2 = document.createElement("td");
let cell3 = document.createElement("td");
cell1.innerHTML = "lato " + ptnum;
if(ptnum > 1)
cell2.innerHTML = "<center><input id='inang' type='text' style='width:80%'></center>";
else
cell2.innerHTML = "<center>---</center>";
cell3.innerHTML = "<center><input id='inlat' type='text' style='width:80%'></center>";
new_row.appendChild(cell1);
new_row.appendChild(cell2);
new_row.appendChild(cell3);
table.appendChild(new_row);
// create the new button
let but_row = document.createElement("tr");
but_row.colspan = "3";
but_row.innerHTML = "<input id='b' type='button' onclick='confirm()' value='OK' style='width:100%'>";
table.appendChild(but_row);
}
function confirm()
{
let ptnum = Number(sessionStorage.getItem("ptnum"));
let table = document.getElementById("table");
if(ptnum > 1)
{
var ang = document.getElementById("inang").value;
ang = ang.replace(",", ".");
sessionStorage.setItem("ang"+ptnum, ang);
}
let lat = document.getElementById("inlat").value;
lat = lat.replace(",", ".");
sessionStorage.setItem("lat"+ptnum, lat);
// remove old rows
table.deleteRow(ptnum+1);
table.deleteRow(ptnum);
// create new point row
let new_row = document.createElement("tr");
let cell1 = document.createElement("td");
let cell2 = document.createElement("td");
let cell3 = document.createElement("td");
cell1.innerHTML = "lato " + ptnum;
if(ptnum > 1)
{
cell2.innerHTML = "<center>" + ang + "</center>";
}
else
{
cell2.innerHTML = "<center>---</center>";
}
cell3.innerHTML = "<center>" + lat + "</center>";
new_row.appendChild(cell1);
new_row.appendChild(cell2);
new_row.appendChild(cell3);
table.appendChild(new_row);
// create new button
let but_row = document.createElement("tr");
but_row.colspan = "3";
but_row.innerHTML = "<input id='b' type='button' onclick='addPoint()' value='Aggiungi lato' style='width:100%'>";
table.appendChild(but_row);
updateArea();
}
function updateArea()
{
let ptnum = Number(sessionStorage.getItem("ptnum"));
let A = 0;
let jump;
let j;
let z;
let sign = 1;
// per ogni fila
for(jump=1; jump<=ptnum-1; jump++)
{
// per ogni elemento della fila
for(j=1; j<=ptnum-jump; j++)
{
let ang_sum = 0;
for(z=j+1; z<=j+jump; z++)
{
ang_sum += Number(sessionStorage.getItem("ang"+z));
}
console.log(ang_sum);
// convert to radiant
ang_sum *= Math.PI / 200;
console.log(ang_sum);
A += sessionStorage.getItem("lat"+j) * sessionStorage.getItem("lat"+(j+jump)) * (Math.sin(ang_sum)) * sign;
}
sign = -sign;
}
A /= 2;
if(A < 0)
A = -A;
document.getElementById("area").innerHTML = "AREA: " + A;
}