Quiq/Math.pm

filter: Statementstmt 90.0% 109 / 121 Branchbran 87.5% 21 / 24 Conditioncond 81.8% 9 / 11 MC/DCmcdc 71.4% 5 / 7 Subroutinesub 83.8% 26 / 31 Podpod 100% 20 / 20 totaltotal 88.7% 190 / 214 CC 19 SCAR 10.4
CC
19
Cov
89%
geoMidpoint
6.1
geoToDegree
6.0
roundMinMax
4.0
CRAP 2.8
« Quiq/SoapWsdlServiceCgi.pm ↑ Summary Quiq/Hash/Ordered.pm »
1 # -----------------------------------------------------------------------------
2  
3 =encoding utf8
4  
5 =head1 NAME
6  
7 Quiq::Math - Mathematische Funktionen
8  
9 =head1 BASE CLASS
10  
11 L<Quiq::Object>
12  
13 =cut
14  
15 # -----------------------------------------------------------------------------
16  
17 package Quiq::Math;
18 270 ▶use base qw/Quiq::Object/;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 270 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 270 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 270 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 270 our $VERSION = '1.240';
25  
26 270 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 270 ▶use Quiq::Formatter;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 270 ▶use POSIX ();
Subroutine BEGIN: called CC=1 SCAR=0.0
29 270 ▶use Math::Trig ();
Subroutine BEGIN: called CC=1 SCAR=0.0
30 270 ▶use Quiq::Math;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 270 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 # -----------------------------------------------------------------------------
34  
35 =head1 METHODS
36  
37 =head2 Konstruktor
38  
39 =head3 new() - Instantiiere Objekt
40  
41 =head4 Synopsis
42  
43 $m = $class->new;
44  
45 =head4 Returns
46  
47 Math-Objekt
48  
49 =head4 Description
50  
51 Instantiiere ein Objekt der Klasse und liefere eine Referenz auf
52 dieses Objekt zurück. Da die Klasse ausschließlich Klassenmethoden
53 enthält, hat das Objekt nur die Funktion, eine abkürzende
54 Aufrufschreibweise zu ermöglichen.
55  
56 =cut
57  
58 # -----------------------------------------------------------------------------
59  
60 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
61 0 my $class = shift;
62 0 return bless \(my $dummy),$class;
63 }
64  
65 # -----------------------------------------------------------------------------
66  
67 =head2 Konstanten
68  
69 =head3 pi() - Liefere PI
70  
71 =head4 Synopsis
72  
73 $pi = $this->pi;
74  
75 =cut
76  
77 # -----------------------------------------------------------------------------
78  
79 75 ▶sub pi {
Subroutine pi: called CC=1 SCAR=0.0
Pod documented
80 75 return 4*CORE::atan2(1,1);
81 }
82  
83 # -----------------------------------------------------------------------------
84  
85 =head2 Rundung
86  
87 =head3 roundTo() - Runde Zahl auf n Nachkommastellen
88  
89 =head4 Synopsis
90  
91 $y = $this->roundTo($x,$n);
92 $y = $this->roundTo($x,$n,$normalize);
93  
94 =head4 Description
95  
96 Runde $x auf $n Nachkommastellen und liefere das Resultat zurück.
97  
98 Ist $normalize "wahr", wird die Zahl nach der Rundung mit
99 normalizeNumber() normalisiert.
100  
101 Bei $n > 0 rundet die Methode mittels
102  
103 $y = sprintf '%.*f',$n,$x;
104  
105 bei $n == 0 mittels roundToInt().
106  
107 =cut
108  
109 # -----------------------------------------------------------------------------
110  
111 20 ▶sub roundTo {
Subroutine roundTo: called CC=3 SCAR=11.2
Pod documented
112 20 my ($this,$x,$n,$normalize) = @_;
113  
114 20 ▶ if ($n == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n == 0) 0 20 20
115 0 return $this->roundToInt($x);
116 }
117  
118 20 $x = sprintf '%.*f',$n,$x;
119 20 ▶ if ($normalize) {
Branch: 1 branch
BranchTrueFalseTotal
if ($normalize) 1 19 20
120 1 $x = Quiq::Formatter->normalizeNumber($x);
121 }
122  
123 20 return $x;
124 }
125  
126 # -----------------------------------------------------------------------------
127  
128 =head3 roundToInt() - Runde Zahl zu Ganzer Zahl (Integer)
129  
130 =head4 Synopsis
131  
132 $n = $this->roundToInt($x);
133  
134 =head4 Description
135  
136 Runde Zahl $x zu ganzer Zahl und liefere das Resultat zurück, nach
137 folgender Regel:
138  
139 Für Nachkommastellen < .5 runde ab, für Nachkommastellen >= .5 runde auf.
140 Für negative $x ist es umgekehrt.
141  
142 Folgender Ansatz funktioniert nicht
143  
144 $n = sprintf '%.0f',$x;
145  
146 denn dieser gibt inkonsistente Ergebnisse
147  
148 0.5 => 0
149 1.5 => 2
150 2.5 => 2
151  
152 =cut
153  
154 # -----------------------------------------------------------------------------
155  
156 9 ▶sub roundToInt {
Subroutine roundToInt: called CC=2 SCAR=6.9
Pod documented
157 9 ▶ return $_[1] > 0? int($_[1]+0.5): -int(-$_[1]+0.5);
Branch: 1 branch
BranchTrueFalseTotal
$_[1] > 0 ? : 7 2 9
158 }
159  
160 # -----------------------------------------------------------------------------
161  
162 =head3 roundMinMax() - Runde Breichsgrenzen auf nächsten geeigneten Wert
163  
164 =head4 Synopsis
165  
166 ($minRounded,$maxRounded) = $this->roundMinMax($min,$max);
167  
168 =head4 Description
169  
170 Die Methode rundet $min ab und $max auf, so dass geeignete
171 Bereichsgrenzen für eine Diagrammskala entstehen.
172  
173 Sind $min und $max gleich, schaffen wir einen künstlichen Bereich
174 ($min-1,$max+1).
175  
176 Die Rundungsstelle leitet sich aus der Größe des Bereichs
177 $max-$min her.
178  
179 =head4 Examples
180  
181 8.53, 8.73 -> 8.5, 8.8
182  
183 8.53, 8.53 -> 7, 10
184  
185 =cut
186  
187 # -----------------------------------------------------------------------------
188  
189 3 ▶sub roundMinMax {
Subroutine roundMinMax: called CC=4 SCAR=13.9
Pod documented
190 3 my ($this,$min,$max) = @_;
191  
192 3 ▶ if ($min == $max) {
Branch: 1 branch
BranchTrueFalseTotal
if ($min == $max) 1 2 3
193 # Sind Minimum und Maximum gleich, schaffen wir einen
194 # künstlichen Bereich
195  
196 1 $min -= 1;
197 1 $max += 1;
198 }
199  
200 3 my $delta = $max-$min;
201 3 for (0.0001,0.001,0.01,0.1,1,10,100,1_000,10_000,100_000) {
202 17 ▶ if ($delta < $_) {
Branch: 1 branch
BranchTrueFalseTotal
if ($delta < $_) 3 14 17
203 3 my $step = $_/10;
204 3 $min = POSIX::floor($min/$step)*$step;
205 3 $max = POSIX::ceil($max/$step)*$step;
206 3 last;
207 }
208 }
209  
210 3 return ($min,$max)
211 }
212  
213 # -----------------------------------------------------------------------------
214  
215 =head2 Größter gemeinsamer Teiler
216  
217 =head3 gcd() - Größter gemeinsamer Teiler
218  
219 =head4 Synopsis
220  
221 $gcd = $this->gcd($a,b);
222  
223 =head4 Description
224  
225 Berechne den größten gemeinsamen Teiler (greatest common divisor)
226 der beiden natürlichen Zahlen $a und $b und liefere diesen
227 zurück. Die Methode ist nach dem L<Euklidschen Algorithmus|https://de.wikipedia.org/wiki/Euklidischer_Algorithmus#Rekursive_Variante> implementiert.
228  
229 =cut
230  
231 # -----------------------------------------------------------------------------
232  
233 83 ▶sub gcd {
Subroutine gcd: called CC=2 SCAR=6.9
Pod documented
234 83 my ($this,$a,$b) = @_;
235 83 ▶ return $b == 0? $a: $this->gcd($b,$a%$b);
Branch: 1 branch
BranchTrueFalseTotal
$b == 0 ? : 36 47 83
236 }
237  
238 # -----------------------------------------------------------------------------
239  
240 =head2 Bogenmaß
241  
242 =head3 degreeToRad() - Wandele Grad in Bogenmaß (rad)
243  
244 =head4 Synopsis
245  
246 $rad = $this->degreeToRad($degree);
247  
248 =cut
249  
250 # -----------------------------------------------------------------------------
251  
252 56 ▶sub degreeToRad {
Subroutine degreeToRad: called CC=1 SCAR=0.0
Pod documented
253 56 my ($this,$degree) = @_;
254 56 return $degree*$this->pi/180;
255 }
256  
257 # -----------------------------------------------------------------------------
258  
259 =head3 radToDegree() - Wandele Bogenmaß (rad) in Grad
260  
261 =head4 Synopsis
262  
263 $degree = $this->radToDegree($rad);
264  
265 =cut
266  
267 # -----------------------------------------------------------------------------
268  
269 17 ▶sub radToDegree {
Subroutine radToDegree: called CC=1 SCAR=0.0
Pod documented
270 17 my ($this,$rad) = @_;
271 17 return 180/$this->pi*$rad;
272 }
273  
274 # -----------------------------------------------------------------------------
275  
276 =head2 Geo-Koordinaten
277  
278 =head3 geoMidpoint() - Mittelpunkt von Geo-Postionen
279  
280 =head4 Synopsis
281  
282 ($latitude,$longitude) = $this->geoMidpoint(\@coordinates);
283  
284 =head4 Arguments
285  
286 =over 4
287  
288 =item @coordinates
289  
290 Array von Geo-Koordinaten. Eine einzelne Geo-Koordinate ist ein
291 Tipel [$latitude,$logitude,$weight], wobei die Gewichtung $weight
292 optional ist. Fehlt die Gewichtung, wird als Wert 1 angenommen.
293  
294 =back
295  
296 =head4 Returns
297  
298 Breite und Länge des geografischen Mittelpunkts
299  
300 =head4 Description
301  
302 Berechne den geografischen Mittelpunkt der Geo-Koordination (plus
303 optionaler Gewichtung) und liefere diesen zurück.
304  
305 Beschreibung des Alogrithmus siehe
306 L<http://www.geomidpoint.com/example.html>
307  
308 =cut
309  
310 # -----------------------------------------------------------------------------
311  
312 3 ▶sub geoMidpoint {
Subroutine geoMidpoint: called CC=6 SCAR=18.1
Pod documented
313 3 my ($this,$coordinateA) = @_;
314  
315 3 my $x = 0;
316 3 my $y = 0;
317 3 my $z = 0;
318 3 my $w = 0;
319  
320 3 for (@$coordinateA) {
321 9 my ($latitude,$longitude,$weight) = @$_;
322  
323 9 ▶ if (!defined($latitude) || !defined($longitude)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $latitude and defined $longitude) 0 9 9
Condition33%1/3
defined $latitude and defined $longitude
!ll&&!rl&&r
009
MC/DC0%0/2
defined $latitude and defined $longitude
defined $latitude defined $longitude
Truth table33%1/3
A and B
A= defined $latitudeB= defined $longitude
ABresult
0X0
100
111
324 # Undefiniert Ortskoordinaten übergehen wir
325 0 next;
326 }
327
328 9 $latitude = $this->degreeToRad($latitude);
329 9 $longitude = $this->degreeToRad($longitude);
330 9 ▶ $weight //= 1;
Condition100%2/2
$weight //= 1
l!l
36
MC/DC100%1/1
$weight //= 1
$weight
Truth table100%2/2
A
A= $weight
Aresult
11
00
331  
332 9 $x += cos($latitude) * cos($longitude) * $weight;
333 9 $y += cos($latitude) * sin($longitude) * $weight;
334 9 $z += sin($latitude) * $weight;
335 9 $w += $weight;
336 }
337  
338 3 my ($midLongitude,$midLatitude);
339 3 ▶ if ($w) {
Branch: 1 branch
BranchTrueFalseTotal
if ($w) 3 0 3
340 3 $x /= $w;
341 3 $y /= $w;
342 3 $z /= $w;
343  
344 3 $midLongitude = $this->radToDegree(atan2($y,$x));
345 3 my $hyp = sqrt($x*$x+$y*$y);
346 3 $midLatitude = $this->radToDegree(atan2($z,$hyp));
347 }
348
349 3 return ($midLatitude,$midLongitude);
350 }
351  
352 # -----------------------------------------------------------------------------
353  
354 =head3 geoToDegree() - Wandele Geo-Ortskoordinate in dezimale Gradangabe
355  
356 =head4 Synopsis
357  
358 $dezDeg = $this->geoToDegree($deg,$min,$sec,$dir);
359  
360 =head4 Description
361  
362 Wandele eine geographische Ortsangabe in Grad, Minuten, Sekunden,
363 Himmelsrichtung in eine dezimale Gradzahl und liefere diese zurück.
364  
365 =head4 Example
366  
367 50 6 44 N -> 50.11222
368 50 6 44 S -> -50.11222
369  
370 =cut
371  
372 # -----------------------------------------------------------------------------
373  
374 7 ▶sub geoToDegree {
Subroutine geoToDegree: called CC=6 SCAR=17.9
Pod documented
375 7 my ($this,$deg,$min,$sec,$dir) = @_;
376  
377 7 $deg = $deg + $min/60 + $sec/3600;
378  
379 7 ▶ if ($dir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir) 5 2 7
380 5 ▶ if ($dir eq 'N' || $dir eq 'E') {
Branch: 2 branches
BranchTrueFalseTotal
if ($dir eq 'N' or $dir eq 'E') { } 2 3 5
elsif ($dir eq 'S' or $dir eq 'W') { } 2 1 3
Condition100%3/3
$dir eq "N" or $dir eq "E"
l!l&&r!l&&!r
113
MC/DC100%2/2
$dir eq "N" or $dir eq "E"
$dir eq "N" $dir eq "E"
Truth table100%3/3
A or B
A= $dir eq "N"B= $dir eq "E"
ABresult
1X1
011
000
381 # nixtun
382 }
383 ▶ elsif ($dir eq 'S' || $dir eq 'W') {
Condition100%3/3
$dir eq "S" or $dir eq "W"
l!l&&r!l&&!r
111
MC/DC100%2/2
$dir eq "S" or $dir eq "W"
$dir eq "S" $dir eq "W"
Truth table100%3/3
A or B
A= $dir eq "S"B= $dir eq "W"
ABresult
1X1
011
000
384 2 $deg = -$deg;
385 }
386 else {
387 1 $this->throw(
388 'MATH-00001: Unbekannte Himmelsrichtung',
389 Direction => $dir,
390 );
391 }
392 }
393  
394 6 return $deg;
395 }
396  
397 # -----------------------------------------------------------------------------
398  
399 =head3 geoDistance() - Entfernung zw. zwei Punkten auf der Erdoberfäche
400  
401 =head4 Synopsis
402  
403 $km = $this->geoDistance($lat1,$lon1,$lat2,$lon2);
404  
405 =head4 Description
406  
407 Berechne die Entfernung zwischen den beiden Geokoordinaten ($lat1,$lon1)
408 und (lat2,$lon2) und liefere die Distanz in Kilometern zurück. Die Angabe
409 der Geokoordinaten ist in Grad.
410  
411 Der Berechnung liegt die Formel zugrunde:
412  
413 km = 1.852*60*180/pi*acos(
414 sin($lat1*pi/180)*sin($lat2*pi/180)+
415 cos($lat1*pi/180)*cos($lat2*pi/180)*cos(($lon2-$lon1)*pi/180)
416 )
417  
418 =head4 See Also
419  
420 =over 2
421  
422 =item *
423  
424 L<Prof. Dirk Reichhardt - Hinweise zur Berechnung von Abständen|http://wwwlehre.dhbw-stuttgart.de/~reichard/content/vorlesungen/lbs/uebungen/abstandsberechnung.pdf>
425  
426 =item *
427  
428 L<Blog Martin Kompf - Entfernungsberechnung|http://www.kompf.de/gps/distcalc.html>
429  
430 =back
431  
432 =head4 Examples
433  
434 Abstand zw. zwei Längengraden (359. und 360.) am Äquator:
435  
436 sprintf '%.2f',Quiq::Math->geoDistance(0,359,0,360);
437 # -> 111.12
438  
439 Abstand zw. zwei Längengraden am Pol:
440  
441 Quiq::Math->geoDistance(90,359,90,360);
442 # -> 0
443  
444 =cut
445  
446 # -----------------------------------------------------------------------------
447  
448 6 ▶sub geoDistance {
Subroutine geoDistance: called CC=1 SCAR=0.0
Pod documented
449 6 my ($this,$lat1,$lon1,$lat2,$lon2) = @_;
450  
451 # Wir rechnen im Bogenmaß
452  
453 6 $lat1 = $this->degreeToRad($lat1);
454 6 $lon1 = $this->degreeToRad($lon1);
455 6 $lat2 = $this->degreeToRad($lat2);
456 6 $lon2 = $this->degreeToRad($lon2);
457  
458 6 return 1.852*60*$this->radToDegree(Math::Trig::acos(
459 sin($lat1)*sin($lat2)+cos($lat1)*cos($lat2)*cos($lon2-$lon1)));
460  
461 # 6371
462 # 6366.7
463 #return 6371*Math::Trig::acos(sin($lat1)*sin($lat2)+
464 # cos($lat1)*cos($lat2)*cos($lon2-$lon1));
465 }
466  
467 # -----------------------------------------------------------------------------
468  
469 =head3 latitudeDistance() - Abstand zwischen zwei Längengraden
470  
471 =head4 Synopsis
472  
473 $km = $this->latitudeDistance($lat);
474  
475 =head4 Description
476  
477 Liefere den Abstand zwischen zwei Längengraden bei Breitengrad $lat.
478 Die Methode ist eigentlich nicht nötig, da sie einen Spezialfall der
479 Mehode geoDistance() behandelt. Die Formel stammt von
480 Wilhelm Petersen.
481  
482 =cut
483  
484 # -----------------------------------------------------------------------------
485  
486 4 ▶sub latitudeDistance {
Subroutine latitudeDistance: called CC=1 SCAR=0.0
Pod documented
487 4 my ($this,$lat) = @_;
488  
489 4 $lat = $this->degreeToRad($lat); # Breite im Bogenmaß
490 4 my $d = $this->degreeToRad(1); # 1 Grad im Bogenmaß
491  
492 4 return 1.852*60*$this->radToDegree(
493 Math::Trig::acos((sin($lat)**2+cos($lat)**2*cos($d))));
494 }
495  
496 # -----------------------------------------------------------------------------
497  
498 =head2 Welt/Pixel-Koordinaten
499  
500 =head3 valueToPixelFactor() - Umrechnungsfaktor Wertebereich in Pixelkoordinaten
501  
502 =head4 Synopsis
503  
504 $factor = $this->valueToPixelFactor($length,$min,$max)
505  
506 =head4 Returns
507  
508 Faktor
509  
510 =head4 Description
511  
512 Liefere den Faktor für die Umrechung von Wertebereich in Pixelkoordinaten.
513 Die Werte werden transformiert auf einen Bildschirmabschnitt
514 der Länge $length, dessen Randpunkte den Werten $min und $max
515 entsprechen.
516  
517 =cut
518  
519 # -----------------------------------------------------------------------------
520  
521 2 ▶sub valueToPixelFactor {
Subroutine valueToPixelFactor: called CC=1 SCAR=0.0
Pod documented
522 2 my ($this,$size,$min,$max) = @_;
523 2 return ($size-1)/($max-$min);
524 }
525  
526 # -----------------------------------------------------------------------------
527  
528 =head3 pixelToValueFactor() - Umrechnungsfaktor von Pixel in Wertebereich
529  
530 =head4 Synopsis
531  
532 $factor = $this->pixelToValueFactor($length,$min,$max);
533  
534 =head4 Returns
535  
536 Faktor (Float)
537  
538 =head4 Description
539  
540 Liefere den Faktor für die Umrechung von Pixel in Werte
541 entlang eines Bildschirmabschnitts der Länge $length, dessen Randpunkt
542 dem Werteberich $min und $max entsprechen.
543  
544 =cut
545  
546 # -----------------------------------------------------------------------------
547  
548 0 ▶sub pixelToValueFactor {
Subroutine pixelToValueFactor: not called CC=1 SCAR=6.9
Pod documented
549 0 my ($this,$length,$min,$max) = @_;
550 0 return 1/Quiq::Math->valueToPixelFactor($length,$min,$max);
551 }
552  
553 # -----------------------------------------------------------------------------
554  
555 =head3 valueToPixelX() - Transformiere Wert in X-Pixelkoordinate
556  
557 =head4 Synopsis
558  
559 $x = $this->valueToPixelX($width,$xMin,$xMax,$xVal);
560  
561 =head4 Alias
562  
563 valueToPixel()
564  
565 =head4 Description
566  
567 Transformiere Wert $xVal in eine Pixelkoordinate auf einer X-Pixelachse
568 der Breite $width. Das Minimum des Wertebereichs ist $xMin, Maximum
569 ist $xMax. Die gelieferten Werte liegen im Bereich 0 .. $width-1.
570  
571 =cut
572  
573 # -----------------------------------------------------------------------------
574  
575 0 ▶sub valueToPixelX {
Subroutine valueToPixelX: not called CC=1 SCAR=6.9
Pod documented
576 0 my ($this,$width,$xMin,$xMax,$xVal) = @_;
577 0 return Quiq::Math->roundToInt(($xVal-$xMin)*($width-1)/($xMax-$xMin));
578 }
579  
580 {
581 270 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
582 270 *valueToPixel = \&valueToPixelX;
583 }
584  
585 # -----------------------------------------------------------------------------
586  
587 =head3 valueToPixelY() - Transformiere Wert in Y-Pixelkoordinate
588  
589 =head4 Synopsis
590  
591 $y = $this->valueToPixelY($height,$yMin,$yMax,$yVal);
592  
593 =head4 Description
594  
595 Transformiere Wert $yVal in eine Pixelkoordinate auf einer Y-Pixelachse
596 der Höhe $height. Das Minimum des Wertebereichs ist $yMin, das Maximum
597 $yMax. Die gelieferten Werte liegen im Bereich $height-1 .. 0.
598 Diese Methode geht von einem Kartesischen Koordinatensystem,
599 also von einem Ursprung I<unten links> aus.
600  
601 =cut
602  
603 # -----------------------------------------------------------------------------
604  
605 0 ▶sub valueToPixelY {
Subroutine valueToPixelY: not called CC=1 SCAR=6.9
Pod documented
606 0 my ($this,$height,$yMin,$yMax,$yVal) = @_;
607 0 return Quiq::Math->roundToInt($height-1-($yVal-$yMin)*
608 ($height-1)/($yMax-$yMin));
609 }
610  
611 # -----------------------------------------------------------------------------
612  
613 =head3 valueToPixelYTop() - Transformiere Wert in Y-Pixelkoordinate
614  
615 =head4 Synopsis
616  
617 $y = $this->valueToPixelYTop($height,$yMin,$yMax,$yVal);
618  
619 =head4 Description
620  
621 Transformiere Wert $yVal in eine Pixelkoordinate auf einer Y-Pixelachse
622 der Höhe $height. Das Minimum des Wertebereichs ist $yMin und das Maximum
623 ist $yMax. Die gelieferten Werte liegen im Bereich $height-1 .. 0.
624  
625 =cut
626  
627 # -----------------------------------------------------------------------------
628  
629 0 ▶sub valueToPixelYTop {
Subroutine valueToPixelYTop: not called CC=1 SCAR=6.9
Pod documented
630 0 my ($this,$height,$yMin,$yMax,$yVal) = @_;
631 0 return Quiq::Math->roundToInt(($yVal-$yMin)*($height-1)/($yMax-$yMin));
632 }
633  
634 # -----------------------------------------------------------------------------
635  
636 =head2 Interpolation
637  
638 =head3 interpolate() - Ermittele Wert durch lineare Interpolation
639  
640 =head4 Synopsis
641  
642 $y = $this->interpolate($x0,$y0,$x1,$y1,$x);
643  
644 =head4 Returns
645  
646 Float
647  
648 =head4 Description
649  
650 Berechne durch lineare Interpolation den Wert y=f(x) zwischen
651 den gegebenen Punkten y0=f(x0) und y1=f(x1) und liefere diesen zurück.
652  
653 Siehe: L<http://de.wikipedia.org/wiki/Interpolation_%28Mathematik%29#Lineare_Interpolation>
654  
655 =cut
656  
657 # -----------------------------------------------------------------------------
658  
659 3 ▶sub interpolate {
Subroutine interpolate: called CC=1 SCAR=0.0
Pod documented
660 3 my ($this,$x0,$y0,$x1,$y1,$x) = @_;
661 3 return $y0+($y1-$y0)/($x1-$x0)*($x-$x0);
662 }
663  
664 # -----------------------------------------------------------------------------
665  
666 =head2 Zahlen
667  
668 =head3 isNumber() - Prüfe, ob Skalar eine Zahl darstellt
669  
670 =head4 Synopsis
671  
672 $bool = $this->isNumber($str);
673  
674 =cut
675  
676 # -----------------------------------------------------------------------------
677  
678 14 ▶sub isNumber {
Subroutine isNumber: called CC=2 SCAR=6.9
Pod documented
679 14 ▶ return Scalar::Util::looks_like_number($_[1])? 1: 0;
Branch: 1 branch
BranchTrueFalseTotal
&Scalar::Util::looks_like_number($_[1]) ? : 8 6 14
680 }
681  
682 # -----------------------------------------------------------------------------
683  
684 =head2 Spike Test
685  
686 =head3 spikeValue() - Berechne Spike-Wert
687  
688 =head4 Synopsis
689  
690 $val = $this->spikeValue($v1,$v2,$v3,$t1,$t3);
691  
692 =head4 Description
693  
694 Berechnung gemäß der Mail von Wilhelm Petersen vom 2017-05-03:
695  
696 $v = (abs($v2-($v3+$v1)/2)-abs(($v3-$v1)/2))/($t3/60-$t1/60);
697  
698 Die Parameter $t1 und $t2 werden in Sekunden angeben, die
699 Funktion rechnet jedoch in Minuten, daher die Division durch 60.
700  
701 =cut
702  
703 # -----------------------------------------------------------------------------
704  
705 39 ▶sub spikeValue {
Subroutine spikeValue: called CC=1 SCAR=0.0
Pod documented
706 39 my ($this,$v1,$v2,$v3,$t1,$t3) = @_;
707 39 return (abs($v2-($v3+$v1)/2)-abs(($v3-$v1)/2))/($t3/60-$t1/60);
708 }
709  
710 # -----------------------------------------------------------------------------
711  
712 =head1 VERSION
713  
714 1.240
715  
716 =head1 AUTHOR
717  
718 Frank Seitz, L<http://fseitz.de/>
719  
720 =head1 COPYRIGHT
721  
722 Copyright (C) 2026 Frank Seitz
723  
724 =head1 LICENSE
725  
726 This code is free software; you can redistribute it and/or modify
727 it under the same terms as Perl itself.
728  
729 =cut
730  
731 # -----------------------------------------------------------------------------
732  
733 270 1;
734  
735 # eof
« Quiq/SoapWsdlServiceCgi.pm ↑ Summary Quiq/Hash/Ordered.pm »