본문 바로가기
프로그래밍/Python

[Python] 원 나누기

by Hwan,. 2016. 10. 3.
728x90
반응형

1. 개요
 
- 돌림판에 사용될 원 그리기


2. 코드

# -*- coding: utf-8 -*-
import sys
import pygame
import math
from pygame.locals import *

PI = 3.14159265359
R = 200
screenX = 410 
screenY = 410
Ox = screenX / 2
Oy = screenY / 2

# 초당 프레임수를 정의
TARGET_FPS = 30
clock = pygame.time.Clock()

# 색 정의
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
WHITE = (255, 255, 255)

# 실수를 더하기 위해서 만들어줌
def myrange(start, end, step):
    r = start
    while(r<end):
        yield r
        r += step

# 인원 수 를 입력받아서 같은 각도로 나눠줌
def auto_line(size):
  a = 360.0 / size # 1명 각도
  # cnt = 0
  tmp = 0 # 각도 누적
  x = 0.0
  y = 0.0
  
  for tmp in myrange(0, 360, a):
      # cnt = cnt + 1

      # 간단한 수학 공식으로 좌표 Get
      x = R * math.cos(math.radians(tmp))
      y = R * math.sin(math.radians(tmp))
      
      #print "[%d] tmp : %.2lf, x : %lf, y : %lf" %(cnt, tmp, x, y)
      pygame.draw.line(screen, BLACK, (Ox, Oy), (Ox+x, Oy+y), 2)


# 라이브러리 및 디스플레이 초기화
pygame.init()

num = raw_input("num : ")
screen = pygame.display.set_mode((screenX, screenY), DOUBLEBUF)
pygame.display.set_caption('Hello World!')  # 타이틀바의 텍스트를 설정

screen.fill(WHITE)
#pygame.draw.line(screen, RED, (Ox, Oy-R*10), (Ox, Oy+R*10), 1) # y
#pygame.draw.line(screen, RED, (Ox-R*10, Oy), (Ox+R*10, Oy), 1) # x

# 메인 루프
while True:
  for event in pygame.event.get():
    # 이벤트를 처리하는 부분
    if event.type == QUIT:
      pygame.quit()
      sys.exit()

  auto_line(int(num)) # 입력받은 인원 수로 원을 나눠줌
  pygame.draw.circle(screen, BLACK, (Ox, Oy), R, 2) # 외부 원
  pygame.draw.circle(screen, BLACK, (Ox, Oy), 20) # 중심 원
  pygame.display.flip()  # 화면 전체를 업데이트
  clock.tick(TARGET_FPS)  # 프레임 수 맞추기

 

생성된 돌림판 이미지 - Alt+Prtsc 으로 캡쳐 후 사용

 

 

728x90
반응형

'프로그래밍 > Python' 카테고리의 다른 글

[Python] FinanceDataReader 모듈  (0) 2021.07.31
[Python] pykrx 모듈  (0) 2021.05.27
[Python] yfinance 모듈  (0) 2021.05.27
[Python] pycryptodome 모듈  (0) 2020.09.26
[Python] 돌림판  (0) 2016.10.03
[Python] 키로거 + 스크린샷  (0) 2016.09.18

댓글