Created
August 19, 2011 05:35
-
-
Save ryochin/1156124 to your computer and use it in GitHub Desktop.
Moonphase for Objective-C
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@interface MoonPhase : NSObject { | |
@private | |
NSDate *now; | |
} | |
- (float) phase; | |
@end | |
// --------------------------------------- | |
#import "MoonPhase.h" | |
float fixangle( float angle ){ | |
return angle - 360.0f * ( floor ( angle / 360.0f ) ); | |
} | |
float torad( float deg ){ | |
return deg * ( M_PI / 180.0f ); | |
} | |
float todeg( float rad ){ | |
return rad * ( 180.0f / M_PI ); | |
} | |
float dsin( float deg ){ | |
return sin( torad( deg ) ); | |
} | |
double jtime( double t ){ | |
return (t / 86400.0f) + 2440587.5f; | |
} | |
float kepler( float m, float ecc ){ | |
float EPSILON = 0.000001f; | |
m = torad(m); | |
float e = m; | |
float delta; | |
// first time | |
delta = e - ecc * sin(e) - m; | |
e -= delta / ( 1.0f - ecc * cos(e) ); | |
// loop | |
while( abs(delta) > EPSILON ){ | |
delta = e - ecc * sin(e) - m; | |
e -= delta / ( 1.0f - ecc * cos(e) ); | |
} | |
return e; | |
} | |
@implementation MoonPhase | |
- (id) init { | |
self = [super init]; | |
if (self) { | |
now = [NSDate date]; | |
} | |
return self; | |
} | |
- (void) dealloc { | |
[super dealloc]; | |
} | |
- (float) phase { | |
// const | |
double Epoch = 2444238.5; | |
float Elonge = 278.833540; | |
float Elongp = 282.596403; | |
float Eccent = 0.016718; | |
float Mmlong = 64.975464; | |
float Mmlongp = 349.383063; | |
float Mlnode = 151.950429; | |
float Minc = 5.145396; | |
NSTimeInterval d = [now timeIntervalSince1970]; | |
double pdate = jtime( (float)d ); | |
double Day = pdate - Epoch; | |
double N = fixangle( ( 360.0f / 365.2422f ) * Day ); | |
double M = fixangle( N + Elonge - Elongp ); | |
double Ec = kepler( M, Eccent ); | |
Ec = sqrt( ( 1.0f + Eccent ) / ( 1.0f - Eccent ) ) * tan( Ec / 2.0f ); | |
Ec = 2.0f * todeg( atan( Ec ) ); | |
double Lambdasun = fixangle( Ec + Elongp ); | |
double ml = fixangle( 13.1763966f * Day + Mmlong ); | |
double MM = fixangle( ml - 0.1114041f * Day - Mmlongp ); | |
double MN = fixangle( Mlnode - 0.0529539f * Day ); | |
double Ev = 1.2739f * sin( torad( 2.0f * ( ml - Lambdasun ) - MM ) ); | |
double Ae = 0.1858f * sin( torad( M ) ); | |
double A3 = 0.37f * sin( torad( M ) ); | |
double MmP = MM + Ev - Ae - A3; | |
double mEc = 6.2886f * sin( torad( MmP ) ); | |
double A4 = 0.214f * sin( torad( 2.0f * MmP ) ); | |
double lP = ml + Ev + mEc - Ae + A4; | |
double V = 0.6583f * sin( torad( 2.0f * ( lP - Lambdasun ) ) ); | |
double lPP = lP + V; | |
double NP = MN - 0.16f * sin( torad( M ) ); | |
double y = sin( torad( lPP - NP ) ) * cos( torad( Minc ) ); | |
double x = cos( torad( lPP - NP ) ); | |
double Lambdamoon = todeg( atan2( y, x ) ); | |
Lambdamoon += NP; | |
double MoonAge = lPP - Lambdasun; | |
float mpfrac = fixangle( MoonAge ) / 360.0f; | |
return mpfrac; | |
} | |
@end |
ghost
commented
Jan 27, 2015
So how can we do something similar to Astro::Moonphase Perl .
See http://search.cpan.org/~brett/Astro-MoonPhase/MoonPhase.pm#phase()
where it prints:
Example:
@phases = phasehunt(); print "New Moon = ", scalar(localtime($phases[0])), "\n"; print "First quarter = ", scalar(localtime($phases[1])), "\n"; print "Full moon = ", scalar(localtime($phases[2])), "\n"; print "Last quarter = ", scalar(localtime($phases[3])), "\n"; print "New Moon = ", scalar(localtime($phases[4])), "\n";
could print something like this:
New Moon = Wed Jun 24 06:51:47 1998 First quarter = Wed Jul 1 21:42:19 1998 Full moon = Thu Jul 9 19:02:47 1998 Last quarter = Thu Jul 16 18:15:18 1998 New Moon = Thu Jul 23 16:45:01 1998
How to show moonrise in objective c pleasE? thanks!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment