文字圈


10

在控制台中找到一种使用字符制作给定半径的圆的方法。请指定字体名称和大小。另外,请至少提供一个输出示例。

例如:

输入:

3

输出:

   ******
 **      **
**        **
*          *
**        **
 **      **
   ******

...嗯,比半径为3的“手绘”“圆”看起来更好的东西。


奖励问题:椭圆。:)


有趣的是,我的radius-3半径与您完全一样,甚至没有尝试过:)
mellamokb 2011年


也许可以澄清字体部分。复制在这里,所有字体都相同;字体大小相同。
用户未知

Answers:


5

Javascript(360)

function c(r){var f=1.83;var e=2*Math.PI/(r*r*r*r*r);var s=r*2+1;var g=Array(s);for(var i=0;i<s;i++){g[i]=Array(Math.round(s*f))};for(var i=0;i<=2*Math.PI;i+=e) {var x=Math.round(f*r*Math.cos(i)+f*r);var y=Math.round(r*Math.sin(i))+r;g[y][x]=1;}for(var j=0;j<g.length;j++){for(var i=0;i<g[j].length;i++)document.write((g[j][i]==1)?'*':' ');document.writeln()}}

http://jsfiddle.net/YssSb/3/f是行高/字体宽比的校正系数。如果使用方形字体设置,即设置line-height = font-size,则可以设置f = 1并获得“正方形”圆圈。或f任意设置椭圆。)

输出3(有趣的是,偶然与OP形状完全相同),5、15:

   ******    
 **      **  
**        ** 
*          * 
**        ** 
 **      **  
   ******    

     *********      
   ***       ****   
 ***            **  
**               ** 
*                 * 
*                 * 
*                 * 
**               ** 
 ***            **  
   ***       ****   
     *********      

                    ***************                      
               ******             ******                 
            ****                       *****             
          ***                              ***           
        ***                                  ***         
      ***                                      ***       
     **                                          **      
    **                                            **     
   **                                              **    
  **                                                **   
 **                                                  **  
 *                                                    *  
**                                                    ** 
*                                                      * 
*                                                      * 
*                                                      * 
*                                                      * 
*                                                      * 
**                                                    ** 
 *                                                    *  
 **                                                  **  
  **                                                **   
   **                                              **    
    **                                            **     
     **                                          **      
      ***                                      ***       
        ***                                  ***         
          ***                              ***           
            ****                       *****             
               ******             ******                 
                    ***************                      

看起来不错的圈子。;)
Mateen Ulhaq 2011年

6

Scala的Bresenham圈子(35)

Bresenham-算法有2个要点:

  • 没有罪/余弦的作品。
  • 您只计算¼*½圆,其他点通过镜像找到。

怎么做:

       2 1  
     DCBABCD
   GFE | EFG
  IJ y | ---- JI
 GJ | / JG
 F | / | F
DE | r / | ED
C | / | C
B 4 | / | B 3
A + ------- A
B 4'x B 3'
抄送
DE ED
 FF
 GJ JG
  伊吉吉
   GFE EFG
     DCBABCD
       2'1' 
  • 我们只计算从zenit的A到I的数字。
    • I点为45°,由x == y定义。
    • 零接地是+所在的位置。
    • zenit中的A是点(x = 0,y = r),r =半径。
    • 要绘制一个闭合圆,我们顺时针(++ x)向右(x + = 1)或向下移动到下一个点(y- = 1)。
    • 圆上的每个点(x,y)都远离中心r。毕达哥拉斯说,r²=x²+y²。
    • 这闻起来像平方根和带有2个解的等式,但是要当心!
    • 我们从A开始,想知道是绘制下一个点还是右下的点。
  • 我们为两个点(x²+y²)计算并建立与r²的差(当然保持不变)。
    • 因为差异可能为负,所以我们从中吸收了绝对值。
    • 那么我们看哪个点更接近结果(r²),eo ipso较小。
    • 取决于我们画的是右邻居还是下邻居。
  • 如此发现的点
    • 1 x,y被镜像
    • 2 -x,y到左边
    • 3 y,对角线x
    • 4 -y,x从那里到左边
  • 所有这些要点再次反映到南方
    • 1'x,-y
    • 2'-x,-y
    • 3'y -x
    • 4'-y,-x完成。

这不是打高尔夫球的代码,但是现有解决方案顶部的所有这些数字让我认为是这样,因此我在打高尔夫球的解决方案上花了很多时间。因此,我也在顶部添加了一个无用的数字。是Pi舍入的11倍。

object BresenhamCircle extends App {
    var count = 0
    val r = args(0).toInt
    // ratio > 1 means expansion in horizontal direction
    val ratio = args(1).toInt
    val field = ((0 to 2 * r).map (i=> (0 to 2 * r * ratio).map (j=> ' ').toArray)).toArray
    def square (x: Int, y: Int): Int = x * x + y * y
    def setPoint (x: Int, y: Int) {
        field (x)(y*ratio) = "Bresenham"(count)
        field (y)(x*ratio) = "Bresenham"(count)
    }
    def points (x: Int, y: Int)
    {
        setPoint (r + x, r + y)
        setPoint (r - x, r + y)
        setPoint (r + x, r - y)
        setPoint (r - x, r - y)
    }
    def bresenwalk () {
        var x = 0;
        var y = r;
        val rxr = r * r
        points (x, y);
        do 
        {
            val (dx, dy) = { if (math.abs (rxr - square ((x+1), y)) < math.abs (rxr - square (x, (y-1))))
                (1, 0)
            else
                (0, -1) 
            }
            count = (count + 1) % "Bresenham".length
            x += dx
            y += dy
            points (x, y)
        }while ((x <= y))
    }
    bresenwalk ()
    println (field.map (_.mkString ("")).mkString ("\n"))
}

字体问题由站点的网络服务器和您的浏览器设置决定。现在,我正在寻找的是

'Droid Sans Mono',Consolas,Menlo,Monaco,Lucida Console,Liberation Mono,DejaVu Sans Mono,Bitstream Vera Sans Mono,Courier New,monospace,serif

字体大小为12px。如果您问我,这是非常无用的信息,但是谁呢?

奖励:椭圆和示例输出:

调用是

    scala BresenhamCircle SIZE RATIO

例如

    scala BresenhamCircle 10 2
              s e r B r e s              
          h n e           e n h          
      e m a                   a m e      
    e r                           r e    
    m                               m    
  h a                               a h  
  n                                   n  
s e                                   e s
e                                       e
r                                       r
B                                       B
r                                       r
e                                       e
s e                                   e s
  n                                   n  
  h a                               a h  
    m                               m    
    e r                           r e    
      e m a                   a m e      
          h n e           e n h          
              s e r B r e s           

A ratio of 2 will print a circular shape for most fonts which happen to be about twice as tall than wide. To compensate for that, we widen by 2. 
# As smaller value than 2 only 1 is available: 

scala BresenhamCircle 6 1
    erBre    
  aes   sea  
 ah       ha 
 e         e 
es         se
r           r
B           B
r           r
es         se
 e         e 
 ah       ha 
  aes   sea  
    erBre    

# widening it has more freedom:

scala BresenhamCircle 12 5
                                             s    e    r    B    r    e    s                                             
                              a    h    n    e                             e    n    h    a                              
                         B    m                                                           m    B                         
                    e    r                                                                     r    e                    
               e    s                                                                               s    e               
          B    r                                                                                         r    B          
     a    m                                                                                                   m    a     
     h                                                                                                             h     
     n                                                                                                             n     
s    e                                                                                                             e    s
e                                                                                                                       e
r                                                                                                                       r
B                                                                                                                       B
r                                                                                                                       r
e                                                                                                                       e
s    e                                                                                                             e    s
     n                                                                                                             n     
     h                                                                                                             h     
     a    m                                                                                                   m    a     
          B    r                                                                                         r    B          
               e    s                                                                               s    e               
                    e    r                                                                     r    e                    
                         B    m                                                           m    B                         
                              a    h    n    e                             e    n    h    a                              
                                             s    e    r    B    r    e    s     

我限制了Int的ratio参数以使其简单,但是可以很容易地扩展它以允许浮点数。


您的字符数(不需要多余的换行符)实际上是34.557519189487725623089077216075 :)顺便说一句:非常好的解决方案,+ 1
Cristian Lupascu 2012年

4

巨蟒(172)

172个字符,其中包括两个强制换行符。对圆锥曲线使用Bresenham算法(无除法或乘法);它仅输出方形字体的圆圈,但应不受阶梯效果的影响(始终具有相同的宽度)。

y=input();t=[y*[' ']for x in range(y)];x=0;y-=1;p=3-2*y
while x<=y:t[x][y]=t[y][x]='*';n,y=((x-y+1,y-1),(x,y))[p<0];p+=4*n+6;x+=1
for s in t[::-1]+t:print"".join(s[::-1]+s)

不是很漂亮,但是很好,我想我会试一试。

  ****
 *    *
*      *
*      *
*      *
*      *
 *    *
  ****

          ********
       ***        ***
      *              *
     *                *
    *                  *
   *                    *
  *                      *
 *                        *
 *                        *
 *                        *
*                          *
*                          *
*                          *
*                          *
*                          *
*                          *
*                          *
*                          *
 *                        *
 *                        *
 *                        *
  *                      *
   *                    *
    *                  *
     *                *
      *              *
       ***        ***
          ********

编辑:错别字,用除法代替加法


3

Perl(92)

我去了“奖金问题”,让它利用字符长宽比绘制椭圆:)

($w)=@ARGV;for$x(-$w..$w){$p.=abs($x*$x+$_*$_-$w*$w)<$w?'*':$"for(-$w..$w);$p.=$/;}print $p;

输出示例:

>perl circle.pl 3
  ***
 *   *
*     *
*     *
*     *
 *   *
  ***

>perl circle.pl 5
   *****
  *     *
 *       *
*         *
*         *
*         *
*         *
*         *
 *       *
  *     *
   *****

>perl circle.pl 8
      *****
    **     **
   *         *
  *           *
 *             *
 *             *
*               *
*               *
*               *
*               *
*               *
 *             *
 *             *
  *           *
   *         *
    **     **
      *****

+1第三名,但形状看起来不如其他答案那么好。(当然,它比我
编写的

3

Haskell的(112 109)

g n=map(zipWith(?)f.repeat)f where x?y|abs(x^2+y^2-n^2)<n='*'|0<1=' ';f=[-n..n]
main=interact$unlines.g.read

通过检查所有点是否x²+y²-r²<n起作用。所有符合条件的点都是星号,所有其他点都是空白。

例子:

$ echo 3 | 朗哈斯克尔圈
  ***  
 * * 
* *
* *
* *
 * * 
  ***  
$ echo 10 | 朗哈斯克尔圈
       *******       
     ** **     
    * *    
   * *   
  * *  
 * * 
 * * 
* *
* *
* *
* *
* *
* *
* *
 * * 
 * * 
  * *  
   * *   
    * *    
     ** **     
       *******       

参见此处的大型示例:http : //www.ideone.com/t042u


3

Python,180个字符

如果字体是方形的,则此代码会产生圆圈。如果您知道字体的高/宽比,则很容易修改以生成标称椭圆。

import math
r=input()
d=2*r+1
c=[' '*d]*d
for a in xrange(9*d):f=math.pi*a/r/9; x=int(r+r*math.sin(f)+.5);y=int(r+r*math.cos(f)+.5);c[y]=c[y][:x]+'*'+c[y][x+1:]
for s in c:print s

例子:

4:
  *****  
 **   ** 
**     **
*       *
*       *
*       *
**     **
 **   ** 
  *****  

7:
     *****     
   **     **   
  **       **  
 **         ** 
**           **
*             *
*             *
*             *
*             *
*             *
**           **
 **         ** 
  **       **  
   **     **   
     *****     

你能发个样品吗?
Mateen Ulhaq 2011年

+1但是第二名。
Mateen Ulhaq 2011年

0

C,127字节,字体名称:Arial Super Bold

#include<math.h>
main(){int r=10,c=r*2+1,q=c*c,d;for(;q--;)d=hypot(r-q%c,r-q/c),printf("%c%s",d>r-4&&d<=r?42:32,q%c?"":"\n");}

结果:

      *********      
    *************    
   ***************   
  *****************  
 ******       ****** 
 *****         ***** 
*****           *****
****             ****
****             ****
****             ****
****             ****
****             ****
****             ****
****             ****
*****           *****
 *****         ***** 
 ******       ****** 
  *****************  
   ***************   
    *************    
      *********      
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.