/* Copyright (C) 2003 - 2014 A.Vontobel
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* The license is in LICENCE-GPL.txt.
* If not, see http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
*/
package net.sf.drawj2d.cmd;
public final class Fkt {
/** Creates a new instance of Funktionen */
public Fkt() {
}
/** Rundet die Zahl auf die gewünschte Anzahl Stellen.
* Für formatierten Text stattdessen Fkt.nf() verwenden.
* @see Fkt.nf() */
public static double fix(double zahl, int anzStellen) {
double erweitern = Math.pow(10d, anzStellen);
return Math.round(erweitern * zahl) / erweitern;
}
public static double max(double[] liste) {
double maximal = liste[0];
for (int i=0 ; i < liste.length; i++) {
if (liste[i] > maximal) maximal = liste[i];
}
return maximal;
}
public static double max(double[][] liste) {
double maximal = liste[0][0];
for (int i=0 ; i < liste.length; i++) {
for (int j = 0; j < liste[i].length; j++) {
if (liste[i][j] > maximal) maximal = liste[i][j];
}
}
return maximal;
}
public static double min(double[] liste) {
double minimal = liste[0];
for (int i=0 ; i < liste.length; i++) {
if (liste[i] < minimal) minimal = liste[i];
}
return minimal;
}
/**
* löst das 2x2 GLS
* ax1 * s1 + ax2 * s2 = bx
* az1 * s1 + az2 * s2 = bz
* nach [s1,s2] auf.
*
* Vor Gebrauch nötigenfalls überprüfen, dass Determinante != 0, siehe Fkt.det2x2(...)
*/
public static double[] GLS2x2(double ax1, double ax2, double bx, double az1, double az2, double bz) {
if (det2x2(ax1, ax2 ,az1, az2) == 0d) {
System.err.println("Warnung: Determinante = 0, eventuell falsche Resultate!");
}
//assert det2x2(ax1, ax2 ,az1, az2) != 0: "Determinante = 0";
double[] s = new double[2];
if (Math.abs(ax1) > Math.abs(az1)) {
s[1] = (bz - bx* az1/ax1) / (az2 - ax2* az1/ax1);
s[0] = (bx - ax2 * s[1]) / ax1;
return s;
}
else {
s[1] = (bx - bz* ax1/az1) / (ax2 - az2* ax1/az1);
s[0] = (bz - az2 * s[1]) / az1;
return s;
}
}/** Berechnet die Determinante einer 2x2-Matrix */
public static double det2x2(double ax1, double ax2, double az1, double az2) {
return ax1 * az2 - ax2 * az1;
}
/** Berechnet das Kreuzprodukt (Vektor) zweier Vektoren
@return Vektor */
public static double[] kreuzprodukt(double ux, double uy, double uz, double vx, double vy, double vz) {
double[] vektor = new double[3];
vektor[0] = det2x2(uy, vy, uz, vz);
vektor[1] = det2x2(uz, vz, ux, vx);
vektor[2] = det2x2(ux, vx, uy, vy);
return vektor;
}
/** Berechnet den Betrag (die Länge) eines Vektors
@return Länge */
public static double vektorbetrag(double[] vektor) {
assert vektor.length > 0;
double länge = 0;
for (int i = 0; i < vektor.length; i++) {
länge += vektor[i]*vektor[i];
}
return Math.sqrt(länge);
}
/** Normiert einen Vektor, der ursprüngliche Vektor bleibt unverändert
@return normierter Vektor */
public static double[] normiere(double[] vektor) {
assert vektor.length > 0;
double nenner = 0;
for (int i = 0; i < vektor.length; i++) {
nenner += vektor[i]*vektor[i];
}
nenner = Math.sqrt(nenner);
double[] normiert = new double[vektor.length];
for (int i = 0; i < vektor.length; i++) {
normiert[i] = vektor[i] / nenner;
}
return normiert;
}
/** Gibt einen Zahlenwert als formatierten String zurück.
*/
public static String nf(int zahl, int minimaleAnzahlStellen) {
if (minimaleAnzahlStellen < 1) {
System.err.print("Formatierungsfehler: minimale Anzahlstellen: ");
System.err.println(minimaleAnzahlStellen + " < 1");
}
StringBuffer ausg = new StringBuffer();
ausg.append(zahl);
while (ausg.length() < minimaleAnzahlStellen) {
ausg.insert(0, " ");
}
return ausg.toString();
}
/** Gibt einen Zahlenwert als formatierten String zurück.
*/
public static String nf(double zahl, int AnzNachkommaStellen) {
if (Double.isNaN(zahl)) return "NaN";
if (Double.isInfinite(zahl)) {
if (zahl == Double.NEGATIVE_INFINITY) return "-oo";
else return "+oo";
}
StringBuffer ausg = new StringBuffer();
if (AnzNachkommaStellen < 0) {
System.err.print("Formatierungsfehler: Anzahl Nachkommastellen: ");
System.err.println(AnzNachkommaStellen + " < 0");
ausg.append(zahl);
return ausg.toString();
}
double gerundet = fix(zahl, AnzNachkommaStellen);
if (AnzNachkommaStellen == 0) ausg.append((int) gerundet);
else {
ausg.append(gerundet);
int vorhNkst = ausg.length() - 1 - ausg.indexOf(".");
if (ausg.indexOf("E") < 0) { // nur wenn Zahl nicht in der Form -6.5E-4
for (int i = vorhNkst; i < AnzNachkommaStellen; i++) {
ausg.append("0");
}
}
}
return ausg.toString();
}
/** Gibt einen Zahlenwert als formatierten String zurück.
*/
public static String nf(double zahl, int AnzNachkommaStellen, int minAnzStellenVorKomma) {
StringBuffer ausg = new StringBuffer();
if (AnzNachkommaStellen < 0 || minAnzStellenVorKomma < 0) {
System.err.println("Formatierungsfehler");
ausg.append(zahl);
return ausg.toString();
}
ausg.append(nf(zahl, AnzNachkommaStellen));
while (ausg.length() < (minAnzStellenVorKomma + 1 + AnzNachkommaStellen)) {
ausg.insert(0, " ");
}
return ausg.toString();
}
/**
* Returns the sign of the argument as follows:
* - If a is greater than zero, the result is 1.0.
* - If a is less than zero, the result is -1.0.
* - If a is NaN, the result is NaN.
* - If a is positive or negative zero, the result is the
* same.
* (von GNU classpath übernommen).
*
* @param a the numeric argument.
* @return the sign of the argument.
* @since 1.5.
*/
public static double signum(double a)
{
if (Double.isNaN(a))
return Double.NaN;
if (a > 0)
return 1.0;
if (a < 0)
return -1.0;
return a;
}
}
Ads help maintain this website.