我想创建一个90度曲线
问题描述:
我已经得到尽可能多的光线,但我需要连接它们。任何帮助?我的代码如下我想创建一个90度曲线
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")
答
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)。 –