• Skip to content
  • Skip to link menu
KDE 3.5 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

kstars

dms.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002                           dms.cpp  -  K Desktop Planetarium
00003                              -------------------
00004     begin                : Sun Feb 11 2001
00005     copyright            : (C) 2001 by Jason Harris
00006     email                : jharris@30doradus.org
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  ***************************************************************************/
00017 
00018 #include <stdlib.h>
00019 
00020 #include "dms.h"
00021 #include <kglobal.h>
00022 #include <klocale.h>
00023 #include <qregexp.h>
00024 
00025 void dms::setD( const double &x ) {
00026     D = x;
00027     scDirty = true;
00028     rDirty = true;
00029 }
00030 
00031 void dms::setD(const int &d, const int &m, const int &s, const int &ms) {
00032     D = (double)abs(d) + ((double)m + ((double)s + (double)ms/1000.)/60.)/60.;
00033     if (d<0) {D = -1.0*D;}
00034     scDirty = true;
00035     rDirty = true;
00036 }
00037 
00038 void dms::setH( const double &x ) {
00039   setD( x*15.0 );
00040 }
00041 
00042 void dms::setH(const int &h, const int &m, const int &s, const int &ms) {
00043   D = 15.0*((double)abs(h) + ((double)m + ((double)s + (double)ms/1000.)/60.)/60.);
00044   if (h<0) {D = -1.0*D;}
00045     scDirty = true;
00046     rDirty = true;
00047 }
00048 
00049 void dms::setRadians( const double &Rad ) {
00050     setD( Rad/DegToRad );
00051     Radians = Rad;
00052 }
00053 
00054 bool dms::setFromString( const QString &str, bool isDeg ) {
00055     int d(0), m(0);
00056     double s(0.0);
00057     bool checkValue( false ), badEntry( false ), negative( false );
00058     QString entry = str.stripWhiteSpace();
00059 
00060     //remove any instances of unit characters.
00061     //h, d, m, s, ', ", or the degree symbol (ASCII 176)
00062     entry.replace( QRegExp("h"), "" );
00063     entry.replace( QRegExp("d"), "" );
00064     entry.replace( QRegExp("m"), "" );
00065     entry.replace( QRegExp("s"), "" );
00066     QString sdeg;
00067     sdeg.sprintf("%c", 176);
00068     entry.replace( QRegExp(sdeg), "" );
00069     entry.replace( QRegExp("\'"), "" );
00070     entry.replace( QRegExp("\""), "" );
00071 
00072     //Account for localized decimal-point settings
00073     //QString::toDouble() requires that the decimal symbol is "."
00074     entry.replace( KGlobal::locale()->decimalSymbol(), "." );
00075  
00076     //empty entry returns false
00077     if ( entry.isEmpty() ) {
00078         setD( 0.0 );
00079         return false;
00080     }
00081 
00082     //try parsing a simple integer
00083     d = entry.toInt( &checkValue );
00084     if ( checkValue ) {
00085         if (isDeg) setD( d, 0, 0 );
00086         else setH( d, 0, 0 );
00087         return true;
00088     }
00089 
00090     //try parsing a simple double
00091     double x = entry.toDouble( &checkValue );
00092     if ( checkValue ) {
00093         if ( isDeg ) setD( x );
00094         else setH( x );
00095         return true;
00096     }
00097 
00098     //try parsing multiple fields.
00099     QStringList fields;
00100 
00101     //check for colon-delimiters or space-delimiters
00102     if ( entry.contains(':') )
00103         fields = QStringList::split( ':', entry );
00104     else fields = QStringList::split( " ", entry );
00105 
00106     //anything with one field is invalid!
00107     if ( fields.count() == 1 ) {
00108         setD(0.0);
00109         return false;
00110     }
00111 
00112     //If two fields we will add a third one, and then parse with
00113     //the 3-field code block. If field[1] is an int, add a third field equal to "0".
00114     //If field[1] is a double, convert it to integer arcmin, and convert
00115     //the remainder to integer arcsec
00116     //If field[1] is neither int nor double, return false.
00117     if ( fields.count() == 2 ) {
00118         m = fields[1].toInt( &checkValue );
00119         if ( checkValue ) fields.append( QString( "0" ) );
00120         else {
00121             double mx = fields[1].toDouble( &checkValue );
00122             if ( checkValue ) {
00123                 fields[1] = QString("%1").arg( int(mx) );
00124                 fields.append( QString("%1").arg( int( 60.0*(mx - int(mx)) ) ) );
00125             } else {
00126                 setD( 0.0 );
00127                 return false;
00128             }
00129         }
00130     }
00131 
00132     //Now have (at least) three fields ( h/d m s );
00133     //we can ignore anything after 3rd field
00134     if ( fields.count() >= 3 ) {
00135         //See if first two fields parse as integers, and third field as a double
00136         
00137         d = fields[0].toInt( &checkValue );
00138         if ( !checkValue ) badEntry = true;
00139         m = fields[1].toInt( &checkValue );
00140         if ( !checkValue ) badEntry = true;
00141         s = fields[2].toDouble( &checkValue );
00142         if ( !checkValue ) badEntry = true;
00143         
00144         //Special case: If first field is "-0", store the negative sign.  
00145         //(otherwise it gets dropped)
00146         if ( fields[0].at(0) == '-' && d == 0 ) negative = true;
00147     }
00148 
00149     if ( !badEntry ) {
00150         double D = (double)abs(d) + (double)abs(m)/60.
00151                 + (double)fabs(s)/3600.;
00152 
00153         if ( negative || d<0 || m < 0 || s<0 ) { D = -1.0*D;}
00154 
00155         if (isDeg) {
00156             setD( D );
00157         } else {
00158             setH( D );
00159         }
00160     } else {
00161         setD( 0.0 );
00162         return false;
00163     }
00164 
00165     return true;
00166 }
00167 
00168 const int dms::arcmin( void ) const {
00169     int am = int( float( 60.0*( fabs(D) - abs( degree() ) ) ) );
00170     if ( D<0.0 && D>-1.0 ) { //angle less than zero, but greater than -1.0
00171         am = -1*am; //make minute negative
00172     }
00173     return am;
00174 }
00175 
00176 const int dms::arcsec( void ) const {
00177     int as = int( float( 60.0*( 60.0*( fabs(D) - abs( degree() ) ) - abs( arcmin() ) ) ) );
00178     //If the angle is slightly less than 0.0, give ArcSec a neg. sgn.
00179     if ( degree()==0 && arcmin()==0 && D<0.0 ) {
00180         as = -1*as;
00181     }
00182     return as;
00183 }
00184 
00185 const int dms::marcsec( void ) const {
00186     int as = int( float( 1000.0*(60.0*( 60.0*( fabs(D) - abs( degree() ) ) - abs( arcmin() ) ) - abs( arcsec() ) ) ) );
00187     //If the angle is slightly less than 0.0, give ArcSec a neg. sgn.
00188     if ( degree()==0 && arcmin()==0 && arcsec()== 0 && D<0.0 ) {
00189         as = -1*as;
00190     }
00191     return as;
00192 }
00193 
00194 const int dms::minute( void ) const {
00195     int hm = int( float( 60.0*( fabs( Hours() ) - abs( hour() ) ) ) );
00196     if ( Hours()<0.0 && Hours()>-1.0 ) { //angle less than zero, but greater than -1.0
00197         hm = -1*hm; //make minute negative
00198     }
00199     return hm;
00200 }
00201 
00202 const int dms::second( void ) const {
00203     int hs = int( float( 60.0*( 60.0*( fabs( Hours() ) - abs( hour() ) ) - abs( minute() ) ) ) );
00204     if ( hour()==0 && minute()==0 && Hours()<0.0 ) {
00205         hs = -1*hs;
00206     }
00207     return hs;
00208 }
00209 
00210 const int dms::msecond( void ) const {
00211     int hs = int( float( 1000.0*(60.0*( 60.0*( fabs( Hours() ) - abs( hour() ) ) - abs( minute() ) ) - abs( second() ) ) ) );
00212     if ( hour()==0 && minute()==0 && second()==0 && Hours()<0.0 ) {
00213         hs = -1*hs;
00214     }
00215     return hs;
00216 }
00217 
00218 const double& dms::sin( void ) const {
00219     if ( scDirty ) {
00220         double s,c;
00221         SinCos( s, c );
00222     }
00223     
00224     return Sin;
00225 }
00226 
00227 const double& dms::cos( void ) const {
00228     if ( scDirty ) {
00229         double s,c;
00230         SinCos( s, c );
00231     }
00232     
00233     return Cos;
00234 }
00235 
00236 void dms::SinCos( double &sina, double &cosa ) const {
00243     if ( scDirty ) {
00244                 #ifdef __GLIBC__ 
00245                 #if ( __GLIBC__ >= 2 && __GLIBC_MINOR__ >=1 && !defined(__UCLIBC__))
00246         //GNU version
00247         sincos( radians(), &Sin, &Cos );
00248                 #else
00249         //For older GLIBC versions
00250             Sin = ::sin( radians() );
00251         Cos = ::cos( radians() );
00252         #endif
00253         #else
00254         //ANSI-compliant version
00255         Sin = ::sin( radians() );
00256         Cos = ::cos( radians() );
00257         #endif
00258         scDirty = false;
00259     }
00260     sina = Sin;
00261     cosa = Cos;
00262 }
00263 
00264 const double& dms::radians( void ) const {
00265     if ( rDirty ) {
00266         Radians = D*DegToRad;
00267         rDirty = false;
00268     }
00269 
00270     return Radians;
00271 }
00272 
00273 const dms dms::reduce( void ) const {
00274     double a = D;
00275     while (a<0.0) {a += 360.0;}
00276     while (a>=360.0) {a -= 360.0;}
00277     return dms( a );
00278 }
00279 
00280 const QString dms::toDMSString(bool forceSign) const {
00281     QString dummy;
00282     char pm(' ');
00283     int dd = abs(degree());
00284     int dm = abs(arcmin());
00285     int ds = abs(arcsec());
00286 
00287     if ( Degrees() < 0.0 ) pm = '-';
00288     else if (forceSign && Degrees() > 0.0 ) pm = '+';
00289 
00290     QString format( "%c%3d%c %02d\' %02d\"" );
00291     if ( dd < 100 ) format = "%c%2d%c %02d\' %02d\"";
00292     if ( dd < 10  ) format = "%c%1d%c %02d\' %02d\"";
00293 
00294     return dummy.sprintf(format.local8Bit(), pm, dd, 176, dm, ds);
00295 }
00296 
00297 const QString dms::toHMSString() const {
00298     QString dummy;
00299     return dummy.sprintf("%02dh %02dm %02ds", hour(), minute(), second());
00300 }
00301 
00302 dms dms::fromString(QString & st, bool deg) {
00303     dms result;
00304     bool ok( false );
00305 
00306     ok = result.setFromString( st, deg );
00307 
00308 //  if ( ok )
00309         return result;
00310 //  else {
00311 //      kdDebug() << i18n( "Could Not Set Angle from string: " ) << st << endl;
00312 //      return result;
00313 //  }
00314 }
00315 
00316 // M_PI is defined in math.h
00317 const double dms::PI = M_PI;
00318 const double dms::DegToRad = PI/180.0;

kstars

Skip menu "kstars"
  • Main Page
  • Modules
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

API Reference

Skip menu "API Reference"
  • keduca
  • kstars
Generated for API Reference by doxygen 1.5.9
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal