Coffeescript 咖啡脚本减法中的奇怪行为
我一定是疯了。Coffeescript连续返回两个不相等整数之差的“0”Coffeescript 咖啡脚本减法中的奇怪行为,coffeescript,Coffeescript,我一定是疯了。Coffeescript连续返回两个不相等整数之差的“0” x = @location[0] y = @start[0] console.log "#{x} - #{y} = #{x-y}" 这是输出: 350 - 322 = 0 250 - 278 = 0 ... and so on 我完全糊涂了 编辑:这里是coffee和编译的js中的源代码 class Branch constructor: (@length, @start, @angle, @generatio
x = @location[0]
y = @start[0]
console.log "#{x} - #{y} = #{x-y}"
这是输出:
350 - 322 = 0
250 - 278 = 0
... and so on
我完全糊涂了
编辑:这里是coffee和编译的js中的源代码
class Branch
constructor: (@length, @start, @angle, @generation, @parent) ->
return if @generation >= RECURSION_LIMIT
@angleVariation = pi/3
@ratio = 2/(1+root(5))
@maxChildren = 10
@parent ?= null
@unit = 2
@children = []
@location = @start
@childLocations = [Math.random()*@length*@ratio/2+(@length*@ratio) \
for n in [0...@maxChildren]]
grow: ->
gl.beginPath()
moveTo @location
@location[0] += @unit * cos(@angle)
@location[1] -= @unit * sin(@angle)
lineTo @location
gl.stroke()
console.log @getLength()
if @getLength() >= @childLocations[@children.length]
@birthChild()
getLength: ->
x = @location[1]
y = @start[1]
console.log "#{x} - #{y} = #{x-y}"
return 1 #root( (@location[0]-@start[0])**2 + (@location[1]-@start[1])**2 )
birthChild: ->
angle = @angle + (Math.random()*@angleVariation*2) - @angleVariation
child = new Branch @length * @ratio, @location, angle, @generation+1, this
在js中:
Branch = (function() {
function Branch(length, start, angle, generation, parent) {
var n, _ref;
this.length = length;
this.start = start;
this.angle = angle;
this.generation = generation;
this.parent = parent;
if (this.generation >= RECURSION_LIMIT) {
return;
}
this.angleVariation = pi / 3;
this.ratio = 2 / (1 + root(5));
this.maxChildren = 10;
if ((_ref = this.parent) == null) {
this.parent = null;
}
this.unit = 2;
this.children = [];
this.location = this.start;
this.childLocations = [
(function() {
var _i, _ref1, _results;
_results = [];
for (n = _i = 0, _ref1 = this.maxChildren; 0 <= _ref1 ? _i < _ref1 : _i > _ref1; n = 0 <= _ref1 ? ++_i : --_i) {
_results.push(Math.random() * this.length * this.ratio / 2 + (this.length * this.ratio));
}
return _results;
}).call(this)
];
}
Branch.prototype.grow = function() {
gl.beginPath();
moveTo(this.location);
this.location[0] += this.unit * cos(this.angle);
this.location[1] -= this.unit * sin(this.angle);
lineTo(this.location);
gl.stroke();
console.log(this.getLength());
if (this.getLength() >= this.childLocations[this.children.length]) {
return this.birthChild();
}
};
Branch.prototype.getLength = function() {
var x, y;
x = this.location[1];
y = this.start[1];
console.log("" + x + " - " + y + " = " + (x - y));
return 1;
};
Branch.prototype.birthChild = function() {
var angle, child;
angle = this.angle + (Math.random() * this.angleVariation * 2) - this.angleVariation;
return child = new Branch(this.length * this.ratio, this.location, angle, this.generation + 1, this);
};
return Branch;
})();
也许您可以提供生成的javascript。无法复制,请参见。显然,你没有向我们展示足够的代码来推断。试着在JSBin.com或JSFiddle.netOk中重现这个问题,发布coffee源代码并编译JSBin解决。对于coffeescript来说还是个新手,我试图复制数组的值,但不是通过引用来复制。我有@location=@start,虽然@location[0]和@start[0]单独返回不同的值,但数字之间的差异是0。不确定这是否是咖啡的工作方式,但无论如何,修复如下:@location=@start.slice