• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeedu API Reference
  • KDE Home
  • Contact Us
 

kstars

  • sources
  • kde-4.12
  • kdeedu
  • kstars
  • kstars
dms.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  dms.cpp - K Desktop Planetarium
3  -------------------
4  begin : Sun Feb 11 2001
5  copyright : (C) 2001 by Jason Harris
6  email : jharris@30doradus.org
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #include "dms.h"
19 
20 #include <stdlib.h>
21 
22 #include <QRegExp>
23 
24 #include <kglobal.h>
25 #include <klocale.h>
26 
27 void dms::setD(const int &d, const int &m, const int &s, const int &ms) {
28  D = (double)abs(d) + ((double)m + ((double)s + (double)ms/1000.)/60.)/60.;
29  if (d<0) {D = -1.0*D;}
30 }
31 
32 void dms::setH( const double &x ) {
33  setD( x*15.0 );
34 }
35 
36 void dms::setH(const int &h, const int &m, const int &s, const int &ms) {
37  D = 15.0*((double)abs(h) + ((double)m + ((double)s + (double)ms/1000.)/60.)/60.);
38  if (h<0) {D = -1.0*D;}
39 }
40 
41 void dms::setRadians( const double &Rad ) {
42  setD( Rad/DegToRad );
43 }
44 
45 bool dms::setFromString( const QString &str, bool isDeg ) {
46  int d(0), m(0);
47  double s(0.0);
48  bool checkValue( false ), badEntry( false ), negative( false );
49  QString entry = str.trimmed();
50  entry.remove( QRegExp("[hdms'\"°]") );
51 
52  //Account for localized decimal-point settings
53  //QString::toDouble() requires that the decimal symbol is "."
54  entry.replace( KGlobal::locale()->decimalSymbol(), "." );
55 
56  //empty entry returns false
57  if ( entry.isEmpty() ) {
58  setD( NaN::d );
59  return false;
60  }
61 
62  //try parsing a simple integer
63  d = entry.toInt( &checkValue );
64  if ( checkValue ) {
65  if (isDeg) setD( d, 0, 0 );
66  else setH( d, 0, 0 );
67  return true;
68  }
69 
70  //try parsing a simple double
71  double x = entry.toDouble( &checkValue );
72  if ( checkValue ) {
73  if ( isDeg ) setD( x );
74  else setH( x );
75  return true;
76  }
77 
78  //try parsing multiple fields.
79  QStringList fields;
80 
81  //check for colon-delimiters or space-delimiters
82  if ( entry.contains(':') )
83  fields = entry.split( ':', QString::SkipEmptyParts );
84  else fields = entry.split( ' ', QString::SkipEmptyParts );
85 
86  //anything with one field is invalid!
87  if ( fields.count() == 1 ) {
88  setD( NaN::d );
89  return false;
90  }
91 
92  //If two fields we will add a third one, and then parse with
93  //the 3-field code block. If field[1] is an int, add a third field equal to "0".
94  //If field[1] is a double, convert it to integer arcmin, and convert
95  //the remainder to integer arcsec
96  //If field[1] is neither int nor double, return false.
97  if ( fields.count() == 2 ) {
98  m = fields[1].toInt( &checkValue );
99  if ( checkValue ) fields.append( QString( "0" ) );
100  else {
101  double mx = fields[1].toDouble( &checkValue );
102  if ( checkValue ) {
103  fields[1] = QString::number( int(mx) );
104  fields.append( QString::number( int( 60.0*(mx - int(mx)) ) ) );
105  } else {
106  setD( NaN::d );
107  return false;
108  }
109  }
110  }
111 
112  //Now have (at least) three fields ( h/d m s );
113  //we can ignore anything after 3rd field
114  if ( fields.count() >= 3 ) {
115  //See if first two fields parse as integers, and third field as a double
116 
117  d = fields[0].toInt( &checkValue );
118  if ( !checkValue ) badEntry = true;
119  m = fields[1].toInt( &checkValue );
120  if ( !checkValue ) badEntry = true;
121  s = fields[2].toDouble( &checkValue );
122  if ( !checkValue ) badEntry = true;
123 
124  //Special case: If first field is "-0", store the negative sign.
125  //(otherwise it gets dropped)
126  if ( fields[0].at(0) == '-' && d == 0 ) negative = true;
127  }
128 
129  if ( !badEntry ) {
130  double D = (double)abs(d) + (double)abs(m)/60.
131  + (double)fabs(s)/3600.;
132 
133  if ( negative || d<0 || m < 0 || s<0 ) { D = -1.0*D;}
134 
135  if (isDeg) {
136  setD( D );
137  } else {
138  setH( D );
139  }
140  } else {
141  setD( NaN::d );
142  return false;
143  }
144 
145  return true;
146 }
147 
148 int dms::arcmin( void ) const {
149  int am = int( float( 60.0*( fabs(D) - abs( degree() ) ) ) );
150  if ( D<0.0 && D>-1.0 ) { //angle less than zero, but greater than -1.0
151  am = -1*am; //make minute negative
152  }
153  return am; // Warning: Will return 0 if the value is NaN
154 }
155 
156 int dms::arcsec( void ) const {
157  int as = int( float( 60.0*( 60.0*( fabs(D) - abs( degree() ) ) - abs( arcmin() ) ) ) );
158  //If the angle is slightly less than 0.0, give ArcSec a neg. sgn.
159  if ( degree()==0 && arcmin()==0 && D<0.0 ) {
160  as = -1*as;
161  }
162  return as; // Warning: Will return 0 if the value is NaN
163 }
164 
165 int dms::marcsec( void ) const {
166  int as = int( float( 1000.0*(60.0*( 60.0*( fabs(D) - abs( degree() ) ) - abs( arcmin() ) ) - abs( arcsec() ) ) ) );
167  //If the angle is slightly less than 0.0, give ArcSec a neg. sgn.
168  if ( degree()==0 && arcmin()==0 && arcsec()== 0 && D<0.0 ) {
169  as = -1*as;
170  }
171  return as; // Warning: Will return 0 if the value is NaN
172 }
173 
174 int dms::minute( void ) const {
175  int hm = int( float( 60.0*( fabs( Hours() ) - abs( hour() ) ) ) );
176  if ( Hours()<0.0 && Hours()>-1.0 ) { //angle less than zero, but greater than -1.0
177  hm = -1*hm; //make minute negative
178  }
179  return hm; // Warning: Will return 0 if the value is NaN
180 }
181 
182 int dms::second( void ) const {
183  int hs = int( float( 60.0*( 60.0*( fabs( Hours() ) - abs( hour() ) ) - abs( minute() ) ) ) );
184  if ( hour()==0 && minute()==0 && Hours()<0.0 ) {
185  hs = -1*hs;
186  }
187  return hs; // Warning: Will return 0 if the value is NaN
188 }
189 
190 int dms::msecond( void ) const {
191  int hs = int( float( 1000.0*(60.0*( 60.0*( fabs( Hours() ) - abs( hour() ) ) - abs( minute() ) ) - abs( second() ) ) ) );
192  if ( hour()==0 && minute()==0 && second()==0 && Hours()<0.0 ) {
193  hs = -1*hs;
194  }
195  return hs; // Warning: Will return 0 if the value is NaN
196 }
197 
198 
199 const dms dms::reduce( void ) const {
200  return dms( D - 360.0*floor(D/360.0) );
201 }
202 
203 const QString dms::toDMSString(const bool forceSign) const {
204  QString dummy;
205  char pm(' ');
206  int dd = abs(degree());
207  int dm = abs(arcmin());
208  int ds = abs(arcsec());
209 
210  if ( Degrees() < 0.0 ) pm = '-';
211  else if (forceSign && Degrees() > 0.0 ) pm = '+';
212 
213  if (dd < 10)
214  return dummy.sprintf("%c%1d%c %02d\' %02d\"", pm, dd, 176, dm, ds);
215  if (dd < 100)
216  return dummy.sprintf("%c%2d%c %02d\' %02d\"", pm, dd, 176, dm, ds);
217 
218  return dummy.sprintf("%c%3d%c %02d\' %02d\"", pm, dd, 176, dm, ds);
219 }
220 
221 const QString dms::toHMSString() const {
222  QString dummy;
223  return dummy.sprintf("%02dh %02dm %02ds", hour(), minute(), second());
224 }
225 
226 dms dms::fromString(const QString &st, bool deg) {
227  dms result;
228  bool ok( false );
229 
230  ok = result.setFromString( st, deg );
231 
232  // if ( ok )
233  return result;
234  // else {
235  // kDebug() << i18n( "Could Not Set Angle from string: " ) << st;
236  // return result;
237  // }
238 }
239 
240 // M_PI is defined in math.h
241 const double dms::PI = M_PI;
242 const double dms::DegToRad = PI/180.0;
dms::hour
int hour() const
Definition: dms.h:104
dms::minute
int minute() const
Definition: dms.cpp:174
dms::Degrees
const double & Degrees() const
Definition: dms.h:98
dms::degree
int degree() const
Definition: dms.h:79
dms::dms
dms()
Default constructor.
Definition: dms.h:45
dms::second
int second() const
Definition: dms.cpp:182
dms.h
dms
An angle, stored as degrees, but expressible in many ways.
Definition: dms.h:42
dms::Hours
double Hours() const
Definition: dms.h:125
dms::marcsec
int marcsec() const
Definition: dms.cpp:165
NaN::d
const double d
Definition: nan.h:35
PI
#define PI
Definition: satellite.cpp:43
dms::msecond
int msecond() const
Definition: dms.cpp:190
dms::arcsec
int arcsec() const
Definition: dms.cpp:156
dms::setD
void setD(const double &x)
Sets floating-point value of angle, in degrees.
Definition: dms.h:130
dms::arcmin
int arcmin() const
Definition: dms.cpp:148
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:36:19 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kstars

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

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  • kstars
  • libkdeedu
  •   keduvocdocument
  • marble
  • parley
  • rocs
  •   App
  •   RocsCore
  •   VisualEditor
  •   stepcore

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal