# c04ex08.pyw

2018-03-29  本文已影响0人  特丽斯纳普

c04ex08.pyw

Line segment info.

import math

from graphics import *

def main():
win = GraphWin("Line Segment Info", 400, 400)
win.setCoords(-10,-10,10,10)

msg = Text(Point(0,-9.5), "Click on endpoints of a line segment.")
msg.draw(win)

p1 = win.getMouse()
p1.draw(win)

p2 = win.getMouse()
p2.draw(win)

line = Line(p1,p2)
line.draw(win)

mark = Circle(line.getCenter(),0.15)
mark.setFill("cyan")
mark.draw(win)

dx = p2.getX() - p1.getX()
dy = p2.getY() - p1.getY()
slope = float(dy)/dx
length = math.sqrt(dx*dx + dy*dy)

msg.setText("Length: "+str(round(length,2))+"   Slope: "+ str(round(slope,2)))
win.getMouse()
win.close()

main()

上一篇 下一篇

猜你喜欢

热点阅读