今回はプログラミング言語の「Python」を使って作成しました。
ベクトルの引き算のプログラムです。
■ プログラム
import sys import math from math import floor from math import cos, sin import numpy as np import pygame from pygame.locals import QUIT, MOUSEBUTTONDOWN pygame.init() SURFACE = pygame.display.set_mode((500, 500)) FPSCLOCK = pygame.time.Clock() def main(): """ main routine """ count = 0 pos0 = (0, 0) # ベクトルaの位置ベクトル pos1 = (0, 0) # ベクトルbの位置ベクトル while True: for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() elif event.type == MOUSEBUTTONDOWN: xpos = floor((event.pos[0] - 240) / 25) ypos = floor((event.pos[1] - 240) / 25) if count % 2 == 0: pos0 = (xpos, ypos) pos1 = (0, 0) else: pos1 = (xpos, ypos) count += 1 # Paint SURFACE.fill((0, 0, 0)) for ypos in range(0, 500, 25): for xpos in range(0, 500, 25): pygame.draw.ellipse(SURFACE, (64, 64, 64), (xpos, ypos, 2, 2)) pygame.draw.line(SURFACE, (255, 0, 0), (250, 0), (250, 500), 3) pygame.draw.line(SURFACE, (255, 0, 0), (0, 250), (500, 250), 3) if pos0[0] != 0: """ ベクトルa """ coord0 = pos0[0] * 25 + 250, pos0[1] * 25 + 250 pygame.draw.line(SURFACE, (0, 255, 0), (250, 250), coord0, 2) """ ベクトルaの矢印 """ x = np.deg2rad(math.degrees(math.atan2(pos0[0], pos0[1]))) rot = np.array([[cos(-x), -sin(-x)], [sin(-x), cos(-x)]]) v = np.array([8, -10]) w = np.dot(rot, v) pygame.draw.line(SURFACE, (0, 255, 0), (coord0[0], coord0[1]), (w[0] + coord0[0], w[1] + coord0[1]), 2) v = np.array([-8, -10]) w = np.dot(rot, v) pygame.draw.line(SURFACE, (0, 255, 0), (coord0[0], coord0[1]), (w[0] + coord0[0], w[1] + coord0[1]), 2) if pos1[0] != 0: """ ベクトルb """ coord1 = pos1[0] * 25 + 250, pos1[1] * 25 + 250 pygame.draw.line(SURFACE, (0, 255, 255), (250, 250), coord1, 2) """ ベクトルbの矢印 """ x = np.deg2rad(math.degrees(math.atan2(pos1[0], pos1[1]))) rot = np.array([[cos(-x), -sin(-x)], [sin(-x), cos(-x)]]) v = np.array([8, -10]) w = np.dot(rot, v) pygame.draw.line(SURFACE, (0, 255, 255), (coord1[0], coord1[1]), (w[0] + coord1[0], w[1] + coord1[1]), 2) v = np.array([-8, -10]) w = np.dot(rot, v) pygame.draw.line(SURFACE, (0, 255, 255), (coord1[0], coord1[1]), (w[0] + coord1[0], w[1] + coord1[1]), 2) """ ベクトルc """ coord2 = ((pos1[0] - pos0[0]) * 25 + 250, (pos1[1] - pos0[1]) * 25 + 250) pygame.draw.line(SURFACE, (255, 0, 255), (pos0[0] * 25 + 250, pos0[1] * 25 + 250), coord1, 3) """ ベクトルcの矢印 """ x = np.deg2rad(math.degrees(math.atan2(pos1[0] - pos0[0], pos1[1] - pos0[1]))) rot = np.array([[cos(-x), -sin(-x)], [sin(-x), cos(-x)]]) v = np.array([8, -10]) w = np.dot(rot, v) pygame.draw.line(SURFACE, (255, 0, 255), (coord1[0], coord1[1]), (w[0] + coord1[0], w[1] + coord1[1]), 2) v = np.array([-8, -10]) w = np.dot(rot, v) pygame.draw.line(SURFACE, (255, 0, 255), (coord1[0], coord1[1]), (w[0] + coord1[0], w[1] + coord1[1]), 2) pygame.display.update() FPSCLOCK.tick(10) if __name__ == '__main__': main() input() |
■ 参考書
「Pythonゲームプログラミング 知っておきたい数学と物理の基本」
■ ゲーム用ライブラリ
「Pygame」
■ プログラミング言語