×
CoffeeScript 关于

CoffeeScript 语法

服务端和客户端的代码重用CoffeeScript 比较范围CoffeeScript 嵌入 JavaScriptCoffeeScript For 循环

CoffeeScript 类和对象

CoffeeScript 对象的链式调用CoffeeScript 类方法和实例方法CoffeeScript 类变量和实例变量CoffeeScript 克隆对象CoffeeScript 类的混合CoffeeScript 创建对象字面值CoffeeScript type 函数

CoffeeScript 字符串

CoffeeScript 大写单词首字母CoffeeScript 查找子字符串CoffeeScript 生成唯一 IDCoffeeScript 字符串插值CoffeeScript 字符串小写转换CoffeeScript 匹配字符串CoffeeScript 重复字符串CoffeeScript 拆分字符串CoffeeScript 清理字符串前后CoffeeScript 字符串大写转换

CoffeeScript 数组

CoffeeScript 检查变量是否数组CoffeeScript 将数组连接CoffeeScript 数组创建对象词典CoffeeScript 数组创建字符串CoffeeScript 定义数组范围CoffeeScript 筛选数组CoffeeScript 列表推导CoffeeScript 映射数组CoffeeScript 数组最大值CoffeeScript 归纳数组CoffeeScript 删除数组相同元素CoffeeScript 反转数组CoffeeScript 打乱数组中的元素CoffeeScript 检测每个元素CoffeeScript 用数组来交换变量CoffeeScript 对象数组类似 Python 的 zip 函数

CoffeeScript 日期和时间

CoffeeScript 计算复活节的日期CoffeeScript 计算感恩节日期计算两个日期中间的天数CoffeeScript 算1个月中最后1天CoffeeScript 找到上/下一个月CoffeeScript 计算月球的相位

CoffeeScript 数学

CoffeeScript 数学常数更快的 Fibonacci 算法平方根倒数快速算法生成可预测的随机数CoffeeScript 生成随机数CoffeeScript 转换弧度和度CoffeeScript 一个随机整数函数CoffeeScript 指数对数运算CoffeeScript 方法CoffeeScript 去抖动函数CoffeeScript 当函数括号不可选CoffeeScript 递归函数CoffeeScript 提示参数CoffeeScript 元编程检测与构建丢失函数CoffeeScript 扩展内置对象

CoffeeScript jQuery

CoffeeScript AJAXCoffeeScript 回调绑定CoffeeScript 创建 jQuery 插件CoffeeScript AJAXCoffeeScript 无jQuery的Ajax

CoffeeScript 正则表达式

CoffeeScript 使用 HeregexesHTML实体替换 HTML 标签CoffeeScript 替换子字符串CoffeeScript 查找子字符串CoffeeScript 网络CoffeeScript 客户端CoffeeScript HTTP 客户端CoffeeScript 基本HTTP服务器CoffeeScript 服务器CoffeeScript 双向客户端CoffeeScript 双向服务器

CoffeeScript 设计模式

CoffeeScript 适配器模式CoffeeScript 桥接模式CoffeeScript 生成器模式CoffeeScript 命令模式CoffeeScript 修饰模式CoffeeScript 工厂方法模式CoffeeScript 解释器模式CoffeeScript 备忘录模式CoffeeScript 观察者模式CoffeeScript 单件模式CoffeeScript 策略模式CoffeeScript 模板方法模式

CoffeeScript 数据库

CoffeeScript MongoDBCoffeeScript SQLite

CoffeeScript 测试

使用 Jasmine 测试使用 Nodeunit 测试

CoffeeScript 计算月球的相位


问题

你想找出月球的相位。

解决方案

以下代码提供了一种计算给出日期的月球相位计算方案:

# moonPhase.coffee

# Moon-phase calculator
# Roger W. Sinnott, Sky & Telescope, June 16, 2006
# http://www.skyandtelescope.com/observing/objects/javascript/moon_phases
#
# Translated to CoffeeScript by Mike Hatfield @WebCoding4Fun

proper_ang = (big) ->
    tmp = 0
    if big > 0
        tmp = big / 360.0
        tmp = (tmp - (~~tmp)) * 360.0
    else
        tmp = Math.ceil(Math.abs(big / 360.0))
        tmp = big + tmp * 360.0

    tmp

jdn = (date) ->  
    month = date.getMonth()
    day = date.getDate()
    year = date.getFullYear()
    zone = date.getTimezoneOffset() / 1440

    mm = month
    dd = day
    yy = year

    yyy = yy
    mmm = mm
    if mm < 3
        yyy = yyy - 1
        mmm = mm + 12

    day = dd + zone + 0.5
    a = ~~( yyy / 100 )
    b = 2 - a + ~~( a / 4 )
    jd = ~~( 365.25 * yyy ) + ~~( 30.6001 * ( mmm+ 1 ) ) + day + 1720994.5
    jd + b if jd > 2299160.4999999

moonElong = (jd) ->
    dr    = Math.PI / 180
    rd    = 1 / dr
    meeDT = Math.pow((jd - 2382148), 2) / (41048480 * 86400)
    meeT  = (jd + meeDT - 2451545.0) / 36525
    meeT2 = Math.pow(meeT, 2)
    meeT3 = Math.pow(meeT, 3)
    meeD  = 297.85 + (445267.1115 * meeT) - (0.0016300 * meeT2) + (meeT3 / 545868)
    meeD  = (proper_ang meeD) * dr
    meeM1 = 134.96 + (477198.8676 * meeT) + (0.0089970 * meeT2) + (meeT3 / 69699)
    meeM1 = (proper_ang meeM1) * dr
    meeM  = 357.53 + (35999.0503 * meeT)
    meeM  = (proper_ang meeM) * dr

    elong = meeD * rd + 6.29 * Math.sin( meeM1 )
    elong = elong     - 2.10 * Math.sin( meeM )
    elong = elong     + 1.27 * Math.sin( 2*meeD - meeM1 )
    elong = elong     + 0.66 * Math.sin( 2*meeD )
    elong = proper_ang elong
    elong = Math.round elong

    moonNum = ( ( elong + 6.43 ) / 360 ) * 28
    moonNum = ~~( moonNum )

    if moonNum is 28 then 0 else moonNum

getMoonPhase = (age) ->
    moonPhase = "new Moon"
    moonPhase = "first quarter" if age > 3 and age < 11 
    moonPhase = "full Moon"     if age > 10 and age < 18
    moonPhase = "last quarter"  if age > 17 and age < 25

    if ((age is 1) or (age is 8) or (age is 15) or (age is 22))
        moonPhase = "1 day past " + moonPhase

    if ((age is 2) or (age is 9) or (age is 16) or (age is 23))
        moonPhase = "2 days past " + moonPhase

    if ((age is 3) or (age is 1) or (age is 17) or (age is 24))
        moonPhase = "3 days past " + moonPhase

    if ((age is 4) or (age is 11) or (age is 18) or (age is 25))
        moonPhase = "3 days before " + moonPhase

    if ((age is 5) or (age is 12) or (age is 19) or (age is 26))
        moonPhase = "2 days before " + moonPhase

    if ((age is 6) or (age is 13) or (age is 20) or (age is 27))
        moonPhase = "1 day before " + moonPhase

    moonPhase

MoonPhase = exports? and exports or @MoonPhase = {}

class MoonPhase.Calculator
    getMoonDays: (date) ->
        jd = jdn date 
        moonElong jd

    getMoonPhase: (date) ->      
        jd = jdn date 
        getMoonPhase( moonElong jd )

讨论

此代码显示一个月球相位计算器对象的方法有两种。计算器->getmoonphase将返回一用个文本表示的日期的月球相位。

这可以用在浏览器和Node.js中。

$ node
> var MoonPhase = require('./moonPhase.js');
 undefined
> var calc = new MoonPhase.Calculator();
 undefined
> calc.getMoonPhase(new Date());
 'full moon'
> calc.getMoonPhase(new Date(1972, 6, 30));
 '3 days before last quarter'

分类导航

关注微信下载离线手册

bootwiki移动版 bootwiki
(群号:472910771)