如何计算手写笔的平方根

flo*_*oww 3 stylus

有没有办法让触控笔计算的平方根x
您知道,就像JavaScript Math.sqrt(x)一样。

我正在创建一个以原点为中心的钻石。我目前正在使用以下代码:

vendor(prop, args...)
    -webkit-{prop} args
    -moz-{prop} args
    -o-{prop} args
    -ms-{prop} args


rotate(r)
    vendor('transform', unquote('rotate('+r+')'))
    rotate r


.diamond
    display block

    width 7ex
    height @width
    line-height @height
    rotate(-45deg)

    /* calc offset of a rotated square
     * @width * @width = $offsetX * $offsetX + $offsetY * $offsetY
     * @width * @width = ( $offset * $offset ) * 2 // for a square: $offsetX= $offsetY
     * ( @width * @width ) / 2 = ( $offset * $offset )
     * sqrt(( @width * @width ) / 2) = $offset
     * @width * sqrt(2) = $offset
     */
    $offset = @width / 1.41421356237 // =sqrt( 2 )
    margin (- $offset) 0em 0ex (- $offset ) // center the diamond on its origin

    padding 0ex 0em

    background-color red
Run Code Online (Sandbox Code Playgroud)

如您所见,我设法在没有实际计算平方根的情况下解决了问题,而是改用了恒定值。但是当我看一下Stylus内置函数时,我找不到任何计算平方根的方法。

有什么方法可以让触控笔计算的平方根x
最好的原因是拥有一些内置好的功能,如LESS。

kiz*_*izu 5

那里回答,您可以使用内置math函数,因此自定义sqrt看起来更好:

sqrt(x)
  return math(x, 'sqrt')
Run Code Online (Sandbox Code Playgroud)

  • 手写笔在框中有[pow] [operator](http://learnboost.github.io/stylus/docs/operators.html#exponent-):`4 ** 3` :)而且我们这里没有其他Math方法。 d在数学函数中不仅需要一个参数。 (2认同)