为什么不能在命令行中打印非整数外摆线?

时间:2015-08-06 03:41:31

标签: python math geometry command-prompt

我想在命令行中打印epicycloid:

\d{3}-\d{3}

并且用N = 3,7和3.5进行测试,当N = 3和7时似乎有效:

N = 3

import math
import sys
WIDTH=30
R=10.0
N=3.0
DELTA=0.01
pixels=[[0 for y in range(WIDTH)] for x in range(WIDTH)]
for f in range(0,(int)(2*math.pi/DELTA)):
    pixels[(int)(R*math.sin(f*DELTA)-R*math.sin((N+1)*f*DELTA)/(N+1)+WIDTH/2)][(int)(R*math.cos(f*DELTA)-R*math.cos((N+1)*f*DELTA)/(N+1)+WIDTH/2)]=1

for row in pixels:
    for cell in row:
        sys.stdout.write('[]' if cell==1 else '  ')
    sys.stdout.write('\n')

N = 7

                      [][][][][][][][]                  
                    [][]            [][][]              
                  [][]                  [][]            
                  []                      [][]          
          [][][][][]                        []          
      [][]      [][]                        []          
    [][]                                    [][]        
  [][]                                        []        
  []                                        [][]        
[][]                                        []          
[]                                          []          
[]                                      [][]            
[]                                      [][]            
[]                                          []          
[][]                                        []          
  []                                        [][]        
  [][]                                        []        
    [][]                                    [][]        
      [][][]    [][]                        []          
          [][][][][]                        []          
                  []                      [][]          
                  [][]                  [][]            
                    [][]              [][]              
                      [][][][][][][][]                  

但是当N = 3.5时,形状不是我预期的(明星喜欢的东西):

                          [][][]                        
                      [][][]  [][][]                    
            [][][][][][]          [][]                  
          [][]      [][]            []                  
        [][]                        [][][]              
        []                          []  [][][]          
        []                                  [][]        
        []                                    []        
    [][][][]                                  []        
    []                                        []        
  [][]                                        []        
  []                                      [][][]        
  []                                      [][]          
  [][]                                        []        
    []                                        []        
      [][][]                                  []        
        []                                    []        
        []                                  [][]        
        []                          []  [][][]          
        [][]                        [][][]              
          [][]      [][]            []                  
            [][][][][][]          [][]                  
                      [][][]  [][][]                    
                          [][][]                        

我检查了公式这么长时间,我的参数使用float直到将结果转换为像素位置,但仍然没有发现有什么问题,有人可以帮忙吗?

(我想用python解决它,但是我发现原来的java版本也有类似的问题,我甚至不知道哪种类型的问题(例如:错误的公式,数字类型转换或错误的代码)导致它)

1 个答案:

答案 0 :(得分:-1)

如果您有小数N=p/q,则需要转到2*q*math.pi。让我们探讨一下我的意思。

N=3.5

parametric plot (sin(t)-sin(4.5t)/4.5 , cos(t)-cos(4.5t)/4.5) for t in [0,2pi]
Wolfram Alpha中的

产生:

N=3.5, 0 to 2pi

与您的输出类似。

另一方面,[0,4pi]的upper limit得到了另一半:

N=3.5, 0 to 4pi

最后,如果N是1/7,我们have to use [0,14pi]得到:

N=1/7, 0 to 14pi