首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >画螺旋线的程序

画螺旋线的程序
EN

Code Review用户
提问于 2019-09-24 17:59:58
回答 1查看 263关注 0票数 2

要运行这个程序,需要标准库中没有的graphics.py模块,可以在这里找到https://mcsp.wartburg.edu/zelle/python/

这个业余节目是为了有趣的图形和编程本身。我想这将是一个有益的爱好,我分享和使用彼此共享的代码,因此我想让它更友好的程序员。

大声地想:

  • 为了便于理解,本程序中的方程被分成不同的行。
  • “可理解性”是可读性的子范畴吗?
  • 或者他们是否反对,因此应该缩短方程式,并使用评论来澄清?
  • 行少,但代码复杂,还是多行不那么复杂的代码?
  • 如果程序员需要重新阅读紧凑复杂的代码注释或重新阅读另一个注释,那么它们之间是否有一个边界而不是语义呢?
  • 我是不是已经走在前面了,未来会变得更清晰?

如何使这个程序更友好的程序员?

代码语言:javascript
复制
"""
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)

下面是程序在当前设置下绘制的螺旋线。

EN

回答 1

Code Review用户

回答已采纳

发布于 2019-09-24 18:24:59

代码格式

  • 黑色:

我建议使用黑色来格式化代码。

  • 评论:

个人偏好:如果一行的代码太长,请将有关行的注释放在行的顶部,而不是放在同一行中,以保持一定的行长。我建议120,您可以指定此黑色。

清洁代码

  • 不要使用*进口:

当我查看您的代码时,我无法看到类Circle来自何处。是math的还是graphics的?请只使用显式导入只导入所需的内容,并使其他读者更容易理解所使用的代码。

  • 澄清第三方进口:

在docstring中,您指的是一个graphics.py模块,但作为一个阅读器,我不知道它在哪里,也不能导入它。没有它,我无法运行代码。告诉我我在哪里能得到或提供它。

  • 从外部范围重新定义变量:

draw_brushes中,每个参数变量都会覆盖外部作用域(模块级别)的变量,如果您犯了错误,就会导致恶劣的行为。

  • 不要对整数调用int

int(1)只是不需要。

  • 配置变量:

您可以使用许多变量来配置绘图,也许您可以将所有这些都放在一个配置文件中,或者至少在代码中的一个位置放置所有这些变量。在所有大写中都有模块级变量、全局变量是一个很好的实践。

更深:

你还想要吗?读pep8。使用匹林特。运行以下代码:import this

票数 3
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/229585

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档