第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

Python pygame - 中心軸旋轉(zhuǎn)線段

Python pygame - 中心軸旋轉(zhuǎn)線段

小怪獸愛(ài)吃肉 2023-12-08 17:18:24
import sys, pygamefrom math import sin, cos, radians, pifrom pygame.locals import QUIT, MOUSEBUTTONDOWNpygame.init()SURFACE = pygame.display.set_mode((780,920))FPSCLOCK = pygame.time.Clock()def Line():    speed = 0    while True:        for event in pygame.event.get():            if event.type == QUIT:                pygame.quit()                sys.exit()        if pygame.mouse.get_pressed() [0]:            speed += 26            if speed > 60:                speed = 60        else:            speed -= 15            if speed < 0:                speed = 0        SURFACE.fill((255,0,0))        radius = speed * 0.8                pygame.draw.line(SURFACE,(5,80,255),(295,127),(sin(200-radius),cos(300-radius)),8)        pygame.display.update()        FPSCLOCK.tick(6)if __name__ == '__main__' :    Line()我正在嘗試編寫(xiě)一條繞中心軸逆時(shí)針旋轉(zhuǎn)的線段。這很難理解。最終位置部分對(duì)我來(lái)說(shuō)很難。
查看完整描述

1 回答

?
Cats萌萌

TA貢獻(xiàn)1805條經(jīng)驗(yàn) 獲得超9個(gè)贊

我建議使用pygame.math.Vector2.

定義直線的中心點(diǎn)、半徑、方向向量和角度

center_x,?center_y?=?SURFACE.get_rect().center
radius?=?100line_vector?=?pygame.math.Vector2(1,?0)
angle?=?0

由于要逆時(shí)針旋轉(zhuǎn),因此需要在按下鼠標(biāo)按鈕時(shí)減小角度:

if?pygame.mouse.get_pressed()[0]:
????angle?-=?1

用于pygame.math.Vector2.rotate()旋轉(zhuǎn)方向向量并計(jì)算旋轉(zhuǎn)后的線的起點(diǎn)和終點(diǎn)

rot_vector?=?line_vector.rotate(angle)?*?radius
start?=?round(center_x?+?rot_vector.x),?round(center_y?+?rot_vector.y)
end?=?round(center_x?-?rot_vector.x),?round(center_y?-?rot_vector.y)

使用startend來(lái)畫(huà)線:

pygame.draw.line(SURFACE,?(5,80,255),?start,?end,?8)

最小示例:?repl.it/@Rabbid76/PyGame-VectorRotateLine


import sys, pygame

from math import sin, cos, radians, pi

from pygame.locals import QUIT, MOUSEBUTTONDOWN

pygame.init()

SURFACE = pygame.display.set_mode((780,920))

FPSCLOCK = pygame.time.Clock()


def Line():

? ? center_x, center_y = SURFACE.get_rect().center

? ? radius = 100

? ? line_vector = pygame.math.Vector2(1, 0)

? ? angle = 0

? ? while True:

? ? ? ? for event in pygame.event.get():

? ? ? ? ? ? if event.type == QUIT:

? ? ? ? ? ? ? ? pygame.quit()

? ? ? ? ? ? ? ? sys.exit()


? ? ? ? if pygame.mouse.get_pressed()[0]:

? ? ? ? ? ? angle -= 1

? ? ? ? ? ? print(angle)

? ? ? ??

? ? ? ? rot_vector = line_vector.rotate(angle) * radius

? ? ? ? start = round(center_x + rot_vector.x), round(center_y + rot_vector.y)

? ? ? ? end = round(center_x - rot_vector.x), round(center_y - rot_vector.y)

? ? ? ?

? ? ? ? SURFACE.fill((255,0,0))

? ? ? ? pygame.draw.line(SURFACE, (5,80,255), start, end, 8)

? ? ? ? pygame.display.update()

? ? ? ? FPSCLOCK.tick(60)


if __name__ == '__main__' :

? ? Line()

如果您想繞線的起點(diǎn)旋轉(zhuǎn),則不需要中心點(diǎn)。定義行的起點(diǎn):


start_x, start_y = SURFACE.get_rect().center

length = 200

計(jì)算旋轉(zhuǎn)的終點(diǎn):


rot_vector = line_vector.rotate(angle) * length

start = start_x, start_y

end = round(start_x + rot_vector.x), round(start_y + rot_vector.y)


查看完整回答
反對(duì) 回復(fù) 2023-12-08
  • 1 回答
  • 0 關(guān)注
  • 190 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)