0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

PythonのturtleでYouTubeのロゴを作る

Posted at

はじめに

Pythonを勉強したいと思い、京都大学が無料公開している教科書で
勉強をすることにしました。
第10章の「Turtleで遊ぶ」の中で、
各個人の制作課題があったので、そこでの備忘録になります。


京都大学のPythonの教科書については以下を参照にしてください。
https://repository.kulib.kyoto-u.ac.jp/dspace/handle/2433/285599

ところどころ、Chat GPTも使用しています。
なお、OSはMacになります。

作るロゴについて

題材についてはYouTubeのロゴでいきます。
凝ったもの作っていると先に進まないと考えたので、簡易的なものにしました。
A5BDAFFC-D01D-4CD1-8EAE-202F5F0B2E20.jpeg
よく見るこれです。

ソースコード

logo.py
import numpy as np
from turtle import *

###角が丸い長方形の計算については以下を参照
https://youta-blog.com/tkinter_rounded_rectangle/
###


# 初期化
screen = Screen() 

# 赤の長方形
shape_coords = get_rounded_rectangle_coords((100, 100, 500, 600))
points = [(float(shape_coords[i]), float(shape_coords[i+1])) for i in range(0, len(shape_coords), 2)]

# 白の長方形
w_shape_coords = get_rounded_rectangle_coords((150, 150, 750, 750))
w_points = [(float(w_shape_coords[i]), float(w_shape_coords[i+1])) for i in range(0, len(w_shape_coords), 2)]

# 赤の長方形の中心点を計算
xs = [p[0] for p in points]
ys = [p[1] for p in points]
cx = (max(xs) + min(xs)) / 2
cy = (max(ys) + min(ys)) / 2

# 白の長方形の中心点を計算
w_xs = [p[0] for p in w_points]
w_ys = [p[1] for p in w_points]
w_cx = (max(w_xs) + min(w_xs)) / 2
w_cy = (max(w_ys) + min(w_ys)) / 2

# 中心を原点に合わせるよう座標を移動
adjusted_points = [(float(x - cx), float(y - cy)) for (x, y) in points]
w_adjusted_points = [(float(x - w_cx), float(y - w_cy)) for (x, y) in w_points]

# 形登録
getscreen().register_shape("rectangle", tuple(adjusted_points))
getscreen().register_shape("w_rectangle", tuple(w_adjusted_points))
print(getscreen().getshapes())

## 生成するタートルの形、輪郭の色、塗りつぶす色のリスト
shapes = ["w_rectangle", "rectangle", "triangle"]
colors = ["black", "red", "white"]
colors_fill = ["white", "red", "white"]

## タートルのリスト ttls タートルを生成、形と色を設定
ttls = []
for i in range(len(shapes)):
    t = Turtle()
    t.up()
    print(type(shapes[i]), shapes[i])
    t.shape(shapes[i])
    t.color(colors[i], colors_fill[i])
    t.goto(0, 0)
    ttls.append(t)

# triangleを拡大
ttls[2].shapesize(4, 4, 4)

done()

つまづいたところ

1. 角が丸い長方形の作り方

YouTubeのロゴって、微妙に楕円というか、角が丸いんですよね。
(若干楕円形でもありますし…)

ここについては、logo.pyの中にある、URLのコードをとにかく参考にしました。
筆者は文系で数学知識が全くなかったので、分かりやすく解説してくださりとても感謝です。

【Tkinter】角丸長方形の作り方を解説!角の丸みも変更可能!
URL:https://youta-blog.com/tkinter_rounded_rectangle/

2. Tkinterとturtleの違い

参考にした記事は、「Tkinter(ティーケーインター)」を使用していたため、
turtleとの違いがありました。
他にも違いがあると思うのですが、超大雑把にまとめました。

Thinker turtle
座標 原点中心のポリゴン 左上中心のポリゴン
機能 GUIアプリの作成に利用できるライブラリ 描画機能を提供するモジュール
用途 GUIアプリの作成 図形やアニメーションの描画

座標のところを計算し直す必要があったそうです。
ここはChat GPTさまさまでした。

3. turtle.register_shape()について

ここが一番時間を食いました。

Pythonのドキュメントより
turtle.register_shape(name, shape=None)

name が任意の文字列で shape が座標ペアのタプル: 対応する多角形を取り込みます。
screen.register_shape("triangle", ((5,-3), (0,5), (-5,-3)))
https://docs.python.org/ja/3.5/library/turtle.html#turtle.getscreen

座標ペアがタプルというところが重要みたいです。

また、turtleのregister_shape()
普通のPythonのfloatかintのタプルのリストしか受け取れないみたいです。
np.float64と混ざっていました。
そのため、型変換が必要になります。

logo.py(修正前)
adjusted_points = [(x - cx, y - cy) for (x, y) in points]

# 省略
getscreen().register_shape("rectangle", adjusted_points)
logo.py(修正後)
adjusted_points = [(float(x - cx), float(y - cy)) for (x, y) in points]

# 省略
getscreen().register_shape("rectangle", tuple(adjusted_points))

A17EE1C4-AD63-4932-93E8-E94B3909B522_1_201_a.jpeg
完成したもの↑

さいごに

型のところは想像していたよりも制約がありました。
それ以外のところは教科書を読めばノリでいけました。

次回の第11章からはTkinterを使用するようなので、反省も踏まえた上で
勉強していきたいなと思います。
(AWSの記事も更新止まっているのでそろそろ書きたいなと思ってます…笑)

0
0
0

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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?