要运行这个程序,需要标准库中没有的graphics.py模块,可以在这里找到https://mcsp.wartburg.edu/zelle/python/
这个业余节目是为了有趣的图形和编程本身。我想这将是一个有益的爱好,我分享和使用彼此共享的代码,因此我想让它更友好的程序员。
大声地想:
如何使这个程序更友好的程序员?
"""
Updated to be more programmer friendly
This is an implementation of John Zelles graphics.py module that is, as I understand it, a simple library of functions so that beginners can have some fun learning
graphics. The implementation paints smooth to scrambled spirals. Its free to use, edit etc.
In this code the "if command" buffers and flattens c.draw(win) while win is minimized and draws it flat when maximized
If enjoyable perhaps a filmscreen to capture as the computer paints? Or maybe a screensaver by just showing a number of brushstrokes at a time?
Thx to Reinderien comments and answer in Code Review Stack Exchange
"""
from math import *
from graphics import *
def draw_brushes(win, brush_maximum_radius, spiral_revolutions, spiral_angle_vector_acceleration, spiral_radius_acceleration, center_x, center_y, x_tremble_amplitude,
x_tremble_period, y_tremble_amplitude, y_tremble_period, light_intesity_period, light_intensity_period_displacement, maximum_light_intensity,
lowest_light_intensity, circleRGBred_weight, circleRGBgreen_weight, circleRGBblue_weight, brush_resolution, x_coordinate, y_coordinate):
for brush_current_radius1 in range(brush_resolution * brush_maximum_radius, 0, -1): #The radius of the circles constituting the spiral
brush_current_radius = brush_current_radius1 / brush_resolution
#Origo of spiral is in the middle of the screen.
#The direction of vector from origo is angle in radians
spiral_angle_vector = ((brush_maximum_radius - brush_current_radius) / brush_maximum_radius * spiral_revolutions)
spiral_angle_vector = spiral_angle_vector ** spiral_angle_vector_acceleration
#The magnitude of vector is radius.
spiral_radius = (brush_maximum_radius - brush_current_radius) ** spiral_radius_acceleration
spiral_radius = spiral_radius ** spiral_radius_acceleration
#If the spirals was an orbit tremble would be another orbit around that orbit. Moon to earth
x_tremble = (spiral_radius * x_tremble_amplitude) * cos(spiral_angle_vector * x_tremble_period)
y_tremble = (spiral_radius * y_tremble_amplitude) * sin(spiral_angle_vector * y_tremble_period)
#With use of the above variables in this function, draw_brushes, the brushstrokes position around origo, center of screen, is calculated
if x_coordinate != int(spiral_radius * cos(spiral_angle_vector) + x_tremble + center_x) or y_coordinate != int(spiral_radius * sin(spiral_angle_vector) + y_tremble + center_y):
x_coordinate = int(spiral_radius * cos(spiral_angle_vector) + x_tremble + center_x)
y_coordinate = int(spiral_radius * sin(spiral_angle_vector) + y_tremble + center_y)
#Point is used to mark the circular brushs center with its radius brush_current_radius. Width is set to zero for cosmetic reasons, looks very choppy
c = Circle(Point(x_coordinate, y_coordinate), brush_current_radius)
c.setWidth(0)
#light intensity due to distance from origo
light_intensity_at_angle = int(sin(brush_current_radius/brush_maximum_radius * pi / 2 * light_intesity_period + light_intensity_period_displacement) * maximum_light_intensity + lowest_light_intensity)
#color due to angle
amplitude = amplitude_displacement = 0.5 #So that trigonometric function is 0<y<1
circleRGBred = int(circleRGBred_weight * light_intensity_at_angle * (sin(spiral_angle_vector) * amplitude + amplitude_displacement))
circleRGBgreen = int(circleRGBgreen_weight * light_intensity_at_angle * (sin(spiral_angle_vector + pi/2) * amplitude + amplitude_displacement))
circleRGBblue = int(circleRGBblue_weight * light_intensity_at_angle * (sin(spiral_angle_vector + pi) * amplitude + amplitude_displacement))
c.setFill(color_rgb(circleRGBred, circleRGBgreen, circleRGBblue))
c.draw(win)
#The window settings where the spiral will be painted
width = 1350
height = 730
background_red = 0
background_green = 0
background_blue = 0
win = GraphWin("My, painting circles", width, height)
win.setBackground(color_rgb(background_red, background_green, background_blue))
#Origo that is the reference point that is middle of coordination in window space
center_x = int(width / 2)
center_y = int(height / 2)
#Full screen spiral settings
zoom = 1.04 #by eye adjustment for visual preference
brush_maximum_radius = int(zoom * sqrt(center_x**2 + center_y**2))#Pythagoras
#Tremble is parable to painters hands tremble. Its factors to variables that are coordinates that gives the spirals path
x_tremble_amplitude = 0.1
x_tremble_period = 3
y_tremble_amplitude = 0.1
y_tremble_period = 3
#Acceleration is an exponent variable in the function draw_brushes
spiral_angle_vector_acceleration = 1.2
spiral_radius_acceleration = 0.8
#miscellaneous variables
spiral_revolutions = 2 * pi
brush_resolution = int(7) #lower integer for more "choppy" spiral
#So that variables is assigned a value before being called
x_coordinate_initial = y_coordinate_initial = 0
#light intensity due to distance from origo
light_intesity_period = 1.3
light_intensity_period_displacement = 0.15
maximum_light_intensity = 223
lowest_light_intensity = 22
#color due to angle. Weight must be 0<integer<1
circleRGBred_weight = int(1)
circleRGBgreen_weight = int(1)
circleRGBblue_weight = int(1)
#A single brushstroke at the time, repeated with for in range() to paint a spiral
draw_brushes(win, brush_maximum_radius, spiral_revolutions, spiral_angle_vector_acceleration, spiral_radius_acceleration, center_x, center_y, x_tremble_amplitude,
x_tremble_period, y_tremble_amplitude, y_tremble_period, light_intesity_period, light_intensity_period_displacement, maximum_light_intensity,
lowest_light_intensity, circleRGBred_weight, circleRGBgreen_weight, circleRGBblue_weight, brush_resolution, x_coordinate_initial, y_coordinate_initial)下面是程序在当前设置下绘制的螺旋线。

发布于 2019-09-24 18:24:59
我建议使用黑色来格式化代码。
个人偏好:如果一行的代码太长,请将有关行的注释放在行的顶部,而不是放在同一行中,以保持一定的行长。我建议120,您可以指定此黑色。
当我查看您的代码时,我无法看到类Circle来自何处。是math的还是graphics的?请只使用显式导入只导入所需的内容,并使其他读者更容易理解所使用的代码。
在docstring中,您指的是一个graphics.py模块,但作为一个阅读器,我不知道它在哪里,也不能导入它。没有它,我无法运行代码。告诉我我在哪里能得到或提供它。
在draw_brushes中,每个参数变量都会覆盖外部作用域(模块级别)的变量,如果您犯了错误,就会导致恶劣的行为。
int:int(1)只是不需要。
您可以使用许多变量来配置绘图,也许您可以将所有这些都放在一个配置文件中,或者至少在代码中的一个位置放置所有这些变量。在所有大写中都有模块级变量、全局变量是一个很好的实践。
https://codereview.stackexchange.com/questions/229585
复制相似问题