OP代碼只繪製「射線」,因爲雖然點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")
是否有機會你可以解釋你的變化?我是新手,很想知道你做了什麼,而不是僅僅複製它。謝謝! –
@AryaVenugopalan你的代碼只繪製「光線」,因爲當'p'放置在圓上時,'lastPoint'在迭代之間不會改變。在我的代碼中,計算出的實際點與最後一個點之間畫一條線,都屬於這個圓圈。我還稍微修改了公式以計算弧,因爲90度對應於π/ 2弧度(預先計算的因子),並顯示中心點和半徑。 –
@AryaVenugopalan我編輯了我的答案。請檢查一下是否足夠清楚或需要更多解釋。此外,如果此答案對您有所幫助,請考慮[接受](http://stackoverflow.com/help/accepted-answer)。 –