我想创建一个90度曲线

时间:2016-07-05 22:33:00

标签: python-3.x

我已经制作了一组光线,但我需要连接它们。有帮助吗?我的代码如下

from math import *
from graphics import *
i = 1
segments = 15
lastPoint = Point(100,0) 
print("Begin")
win = GraphWin("Trigonometry", 1500, 1500)
while i<=segments:

    angle =i*pi/segments
    y = int(sin(angle)*100)
    x = int(cos(angle)*100)

    i = i+1
    p = Point(x,y)
    l = Line(p, lastPoint)
    l.draw(win)
    print(p.x, p.y)

print("End")

1 个答案:

答案 0 :(得分:0)

OP代码仅绘制&#34;射线&#34;因为,虽然点p位于圆上,但lastPoint在迭代之间不会发生变化。

我们必须将lastPoint的值更新为字面上计算的最后一个点,以便将弧绘制为一系列连续的段。

以下是修改后的代码,OP在其评论中提出了进一步的解释:

from math import *
from graphics import *

# Function to calculate the integer coordinates of a Point on a circle
# given the center (c, a Point), the radius (r) and the angle (a, radians)
def point_on_circle( c, r, a ) :
    return Point( int(round(c.x + r*cos(a))), int(round(c.y + r*sin(a))) )

# Define the graphical output window, I'll set the coordinates system so
# that the origin is the bottom left corner of the windows, y axis is going
# upwards and 1 unit corresponds to 1 pixel
win = GraphWin("Trigonometry", 800, 600)
win.setCoords(0,0,800,600)

# Arc data. Angles are in degrees (more user friendly, but later will be
# transformed in radians for calculations), 0 is East, positive values
# are counterclockwise. A value of 360 for angle_range_deg gives a complete
# circle (polygon). 
angle_start_deg = 0 
angle_range_deg = 90
center          = Point(10,10)
radius          = 200
segments        = 16

angle_start = radians(angle_start_deg)
angle_step = radians(angle_range_deg) / segments

# Initialize lastPoint with the position corresponding to angle_start
# (or i = 0). Try different values of all the previous variables 
lastPoint = point_on_circle(center, radius, angle_start)
print("Begin")
i = 1
while i <= segments :
    # update the angle to calculate a new point on the circle
    angle = angle_start + i * angle_step
    p = point_on_circle(center, radius, angle)
    # draw a line between the last two points
    l = Line(p, lastPoint)
    l.draw(win)
    print(p.x, p.y)
    # update the variables to move on to the next segment which share an edge
    # (the last point) with the previous segment
    i = i + 1
    lastPoint = p

print("End")