Created
June 29, 2019 14:31
-
-
Save tthtlc/a39d0f79d8013b102905a14973f28a1d to your computer and use it in GitHub Desktop.
Rotating triangles around a hexagon
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pygame | |
import math | |
# Initialize the game engine | |
pygame.init() | |
# Define the colors we will use in RGB format | |
black = [ 0, 0, 0] | |
white = [255,255,255] | |
blue = [ 0, 0,255] | |
green = [ 0,255, 0] | |
red = [255, 0, 0] | |
PI = 3.141592653 | |
ysize=1000 | |
xsize=800 | |
# Set the height and width of the screen | |
size = [ysize,xsize] | |
screen = pygame.display.set_mode(size) | |
pygame.display.set_caption("Sansagraphics World") | |
#Loop until the user clicks the close button. | |
done = False | |
clock = pygame.time.Clock() | |
angle_offset=0.0 | |
def chasing_ngon(radius, ngon,xoffset,yoffset): | |
i=0 | |
#myangle+=angle_step+1/360*2*math.pi+theta | |
myangle = 0.0 | |
angle_step=2*math.pi/ngon | |
a = yoffset | |
b = xoffset | |
niteration=8*ngon | |
prevy = a + radius*math.sin(myangle) | |
prevx = b + radius*math.cos(myangle) | |
for i in range(1, niteration+1): | |
y = a + radius*math.sin(myangle) | |
x = b + radius*math.cos(myangle) | |
pygame.draw.line(screen,[0,0,255],[prevx,prevy],[x,y],2) | |
#if (i%ngon==0): | |
# myangle += 2*math.pi/36 | |
myangle+=angle_step | |
prevx=x | |
prevy=y | |
def my_ngon(R, r, ngon, angle_offset, xoffset, yoffset): | |
global PI | |
delta = 2*PI/ngon | |
prevx = R*math.sin((angle_offset))+r*math.sin((angle_offset)) + xoffset | |
prevy = R*math.cos((angle_offset))+r*math.cos((angle_offset)) + yoffset | |
for n in range(1,ngon+1): | |
##pygame.draw.line(screen,red,[0,10+y_offset],[100,110+y_offset],5) | |
theta = n * delta | |
y_offset = R*math.cos((theta+angle_offset))+r*math.cos((theta+angle_offset)) + yoffset | |
x_offset = R*math.sin((theta+angle_offset))+r*math.sin((theta+angle_offset)) + xoffset | |
# print(y_offset, x_offset) | |
#cx=int(120*(math.sin((theta)/10)+1)+10) | |
#cy=int(120*(math.sin((theta)*6/10)+1)+10) | |
#cz=int(120*(math.sin((theta)/10-PI)+1)+10) | |
pygame.draw.line(screen,[0,0,255],[prevx,prevy],[x_offset,y_offset],2) | |
prevx=x_offset | |
prevy=y_offset | |
screen.fill(white) | |
xcenteroffset=xsize/2 | |
ycenteroffset=ysize/2 | |
small_radius=ysize/20 | |
large_radius=ysize/6 | |
iteration_steps=120 | |
yoffset = small_radius*math.sin((0.0))+ ycenteroffset | |
xoffset = small_radius*math.cos((0.0))+ xcenteroffset | |
while done == False: | |
# This limits the while loop to a max of 10 times per second. | |
# Leave this out and we will use all CPU we can. | |
clock.tick(10) | |
for event in pygame.event.get(): # User did something | |
if event.type == pygame.QUIT: # If user clicked close | |
done = True # Flag that we are done so we exit this loop | |
# All drawing code happens after the for loop and but | |
# inside the main while done==False loop. | |
# Clear the screen and set the screen background | |
## my_ngon(R, r, ngon, angle_offset, xoffset, yoffset): | |
## chasing_ngon(200, 3,200.0,200.0) | |
my_ngon(large_radius, 0, 3, angle_offset, xoffset, yoffset) | |
angle_offset+=2*PI/iteration_steps | |
yoffset = small_radius*math.sin((angle_offset))+ ycenteroffset | |
xoffset = small_radius*math.cos((angle_offset))+ xcenteroffset | |
# Go ahead and update the screen with what we've drawn. | |
# This MUST happen after all the other drawing commands. | |
##delta += ddelta | |
pygame.display.flip() | |
# Be IDLE friendly | |
pygame.quit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment