1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

sin cos 波(Python)

Posted at

今回はPythonで文字を使ってsinとcosの波を表示させます。
サンプルコード

import hmath
θ = -1
from time import sleep
size = 30 # 波の横の長さ 画面入りきらない場合はこの値を小さくするとよい
print("                                              Sin                                                                                                   Cos")
while True: # 無限ループ
    θ += 1  # 角度(度数)
    # 度をラジアンに変換し計算
    sin = hmath.sin(hmath.radian(θ))
    cos = hmath.cos(hmath.radian(θ))
    # 小数点以下6桁までにする
    new_sin = round(sin * 100000) / 100000
    new_cos = round(cos * 100000) / 100000
    # 文字数の長さをそろえるため0を追加する
    new_cos = str(new_cos) + "0" * (7 - len(str(abs(new_cos))))
    new_sin = str(new_sin) + "0" * (7 - len(str(abs(new_sin))))
    # 文字の長さをそろえるため0以上だったら+をつける
    if 0 <= cos:
        new_cos = "+" + str(new_cos)
    if 0 <= sin:
        new_sin = "+" + str(new_sin)
    # 表示するSとCの数
    sin_ = round(sin * size + 0.5) + size
    d    = round(cos * size + 0.5) + size
    # 一行表示
    print(" " * (10 - len(str(θ))) + str(θ) + "°" + str("===" + "S" * sin_ + "s" * (size * 2 - sin_) + "~~~") + str(new_sin) + "        " + str("C" * d + "c" * (size * 2 - d) + "~~~") + str(new_cos))
    # 待つ
    sleep(0.0025)

結果

         0===SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSssssssssssssssssssssssssssssss~~~+0.00000        CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC~~~+1.00000
         1===SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSsssssssssssssssssssssssssssss~~~+0.01745        CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC~~~+0.99985
         2===SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSssssssssssssssssssssssssssss~~~+0.03490        CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC~~~+0.99939
         3===SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSssssssssssssssssssssssssssss~~~+0.05234        CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC~~~+0.99863
         4===SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSsssssssssssssssssssssssssss~~~+0.06976        CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC~~~+0.99756
         5===SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSsssssssssssssssssssssssssss~~~+0.08716        CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC~~~+0.99619
         6===SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSssssssssssssssssssssssssss~~~+0.10453        CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC~~~+0.99452
         7===SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSssssssssssssssssssssssssss~~~+0.12187        CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC~~~+0.99255
         8===SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSsssssssssssssssssssssssss~~~+0.13917        CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC~~~+0.99027
         9===SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSsssssssssssssssssssssssss~~~+0.15643        CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC~~~+0.98769
        10===SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSssssssssssssssssssssssss~~~+0.17365        CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC~~~+0.98481
        11===SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSssssssssssssssssssssssss~~~+0.19081        CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC~~~+0.98163
        12===SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSsssssssssssssssssssssss~~~+0.20791        CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC~~~+0.97815
        13===SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSsssssssssssssssssssssss~~~+0.22495        CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC~~~+0.97437
        14===SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSssssssssssssssssssssss~~~+0.24192        CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC~~~+0.97030
        15===SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSssssssssssssssssssssss~~~+0.25882        CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCc~~~+0.96593
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~以下略~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

ずっとみてられる

1
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?