-
Notifications
You must be signed in to change notification settings - Fork 0
/
inc.auxiliares.php
316 lines (274 loc) · 8.82 KB
/
inc.auxiliares.php
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
<?php
/**
* Convierte altitudMode en un string
*
* @param int $altitudeMode (ENTRADA)
* @return string
*/
function altitudeModetoString ($altitudeMode){
$modo = "";
switch($altitudeMode){
case 0: $modo = "clampToGround"; break; // "Subject to the ground"; break;
case 1: $modo = "clampToSeaFloor"; break; // "Subject to seabed"; break;
case 2: $modo = "relativeToGround"; break; // "Relative to soil"; break;
case 3: $modo = "relativeToSeaFloor"; break; // "Relative to the seabed"; break;
case 4: $modo = "absolute"; break;
}
return $modo;
}
function printMalla($malla, $relleno = " ") {
for($i = 0; $i < count($malla); $i++) {
for($j = 0; $j < count($malla[$i]); $j++) {
if ( $malla[$i][$j] == "0" ) {
print $relleno;
} else {
print $malla[$i][$j];
}
}
print PHP_EOL;
}
}
/*
* Desde que se utiliza conrec para los contornos, los índices son decimales
* (ahora el contorno se situa entre medias de la celda a 1 y la celda a 0
* y printContornos no funciona)
*
*/
function printContornos($contornos, $malla) {
/*
$malla = array();
for($i=0; $i<$tamMalla; $i++) {
$malla[$i] = array();
for($j=0; $j<$tamMalla; $j++) {
$malla[$i][$j] = 0;
}
}
*/
$alfa = array(
'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'
);
$i = 0;
foreach($contornos as $contorno) {
foreach($contorno as $pto) {
if ( $malla[$pto['fila']][$pto['col']] == "0" ) {
$malla[$pto['fila']][$pto['col']] = $alfa[(($i++)%count($alfa))];
} else {
print "punto repetido!" . PHP_EOL;
}
}
}
printMalla($malla);
return true;
}
/**
* Express number with a byte prefix (byte, Kb, Mb, Gb...)
* @param size float number
* @return float rounded number with prefix
*/
function convertBytes($size) {
$unit = array('b', 'Kb', 'Mb', 'Gb', 'Tb', 'Pb');
return @round($size / pow(1024, ($i = floor(log($size, 1024)))), 1) . '' . $unit[$i];
}
/**
* If the input arrays have the same string keys, then the later value
* for that key will overwrite the previous one. If, however, the arrays
* contain numeric keys, the later value will not overwrite the original
* value, but will be appended.
* Values in the input array with numeric keys will be renumbered with
* incrementing keys starting from zero in the result array.
* So this function is actually making some conditional statements. You
* can replace array merge with normal adding, consisting of the loop
* (foreach or any other) and the [] operator. You can write a function
* imitating array_merge, like(using reference to not copy the array..).
*
* @url https://stackoverflow.com/questions/23348339/optimizing-array-merge-operation
*
* @param array1 source and destination array
* @param array2 second array to copy to array1
*/
function array_merge_fast(&$array1, &$array2) {
foreach($array2 as $i) {
$array1[] = $i;
}
}
$num_to_bits = array(0, 1, 1, 2, 1, 2, 2, 3,
1, 2, 2, 3, 2, 3, 3, 4);
/*
* Count set bits by pre-storing count set bits in nibbles.
* @param int number to count bits set to '1'
* @return int number of bits set to '1'
*/
function countSetBits($num) {
global $num_to_bits;
$nibble = 0;
if (0 == $num)
return $num_to_bits[0];
// Find last nibble
$nibble = $num & 0xf;
// Use pre-stored values to find count
// in last nibble plus recursively add
// remaining nibbles.
return $num_to_bits[$nibble] +
countSetBits($num >> 4);
}
/*
* escribe la cadena enviada a stderr, añadiendo fecha
* si insert_EOL, añade fecha y salto de línea
*
*/
function logger($str, $insert_EOL = true) {
$d = new DateTime();
if ( $insert_EOL )
$ret = $d->format("Y-m-d H:i:s.v") . $str . PHP_EOL;
else
$ret = $str;
fwrite(STDERR, $ret);
return true;
}
/**
* @author Ovunc Tukenmez <[email protected]>
* version 1.0.1 - 10/26/2017
*
* @url https://github.com/ovunctukenmez/Combinations/blob/master/Combinations.php
* This class is used to generate combinations with or without repetition allowed
* as well as permutations with or without repetition allowed
*/
class Combinations
{
private $_elements = array();
public function __construct($elements)
{
$this->setElements($elements);
}
public function setElements($elements){
$this->_elements = array_values($elements);
}
public function getCombinations($length, $with_repetition = false){
$combinations = array();
foreach ($this->x_calculateCombinations($length, $with_repetition) as $value){
$combinations[] = $value;
}
return $combinations;
}
public function getPermutations($length, $with_repetition = false){
$permutations = array();
foreach ($this->x_calculatePermutations($length, $with_repetition) as $value){
$permutations[] = $value;
}
return $permutations;
}
private function x_calculateCombinations($length, $with_repetition = false, $position = 0, $elements = array()){
$items_count = count($this->_elements);
for ($i = $position; $i < $items_count; $i++){
$elements[] = $this->_elements[$i];
if (count($elements) == $length){
yield $elements;
}
else{
foreach ($this->x_calculateCombinations($length, $with_repetition, ($with_repetition == true ? $i : $i + 1), $elements) as $value2){
yield $value2;
}
}
array_pop($elements);
}
}
private function x_calculatePermutations($length, $with_repetition = false, $elements = array(), $keys = array()){
foreach($this->_elements as $key => $value){
if ($with_repetition == false){
if (in_array($key, $keys)){
continue;
}
}
$keys[] = $key;
$elements[] = $value;
if (count($elements) == $length){
yield $elements;
}
else{
foreach ($this->x_calculatePermutations($length, $with_repetition, $elements, $keys) as $value2){
yield $value2;
}
}
array_pop($keys);
array_pop($elements);
}
}
}
/*
* Convierte un número de segundos en una cadena legible para humanos con unidades
*
*/
function timer_unidades( $t ) {
$t = floor($t);
$unidad = "";
if ( $t > 24*60*60 ) {
$format = "d H:i:s";
} else if ( $t > 60*60 ) {
$format = "H:i:s";
} else if ( $t > 120 ) {
$format = "i:s";
} else {
$format = "U";
if ( $t > 1 ) {
$unidad = "segundos";
} else {
$unidad = "segundo";
}
}
$timer_string = date($format, $t);
return $timer_string . " " . $unidad;
}
/**
* Returns the area of a closed path on Earth.
* @param path A closed path.
* @return The path's area in square kilometers.
*/
function computeArea($path) {
return abs(computeSignedArea($path)/1000000.0);
}
/**
* Returns the signed area of a closed path on Earth. The sign of the area may be used to
* determine the orientation of the path.
* "inside" is the surface that does not contain the South Pole.
* @param path A closed path.
* @return The loop's area in square meters.
*/
function computeSignedArea($path) {
return computeSignedAreaP($path, RADIO_TERRESTRE);
}
/**
* Returns the signed area of a closed path on a sphere of given radius.
* The computed area uses the same units as the radius squared.
* Used by SphericalUtilTest.
*/
function computeSignedAreaP($path, $radius) {
$size = count($path);
if ($size < 3) { return 0; }
$total = 0;
$prev = $path[$size - 1];
$prevTanLat = tan((M_PI / 2 - deg2rad($prev[0])) / 2); // lat
$prevLng = deg2rad($prev[1]); //lon
// For each edge, accumulate the signed area of the triangle formed by the North Pole
// and that edge ("polar triangle").
foreach($path as $point) {
$tanLat = tan((M_PI / 2 - deg2rad($point[0])) / 2); // lat
$lng = deg2rad($point[1]); // lon
$total += polarTriangleArea($tanLat, $lng, $prevTanLat, $prevLng);
$prevTanLat = $tanLat;
$prevLng = $lng;
}
return $total * ($radius * $radius);
}
/**
* Returns the signed area of a triangle which has North Pole as a vertex.
* Formula derived from "Area of a spherical triangle given two edges and the included angle"
* as per "Spherical Trigonometry" by Todhunter, page 71, section 103, point 2.
* See http://books.google.com/books?id=3uBHAAAAIAAJ&pg=PA71
* The arguments named "tan" are tan((pi/2 - latitude)/2).
*/
function polarTriangleArea($tan1, $lng1, $tan2, $lng2) {
$deltaLng = $lng1 - $lng2;
$t = $tan1 * $tan2;
return 2 * atan2($t * sin($deltaLng), 1 + $t * cos($deltaLng));
}