【发布时间】:2015-03-03 06:30:36
【问题描述】:
我被这个烦人的问题困扰了好久。我正在尝试编写代码,以便我可以缩放线段,这意味着如果我要缩放的量(例如)是 2 并且线的当前长度是 33,它将把整个长度增加到 67。我在开头加一半,在结尾加一半…… new front ---a--------b--- new back... 但是我在将其转换为代码时遇到了麻烦。这是代码示例。端点方法应以元组形式返回端点,例如 (p1, p2)
from point import Point
import math
class Line:
def __init__(self,aPoint=Point(), bPoint=Point()):
self.firstPoint = aPoint
self.secondPoint = bPoint
def getEndPoints(self):
return (self.firstPoint, self.secondPoint)
def scale(self,factor):
if factor < 1:
x1 = self.firstPoint.x +(self.secondPoint.x - self.firstPoint.x) * (factor)
x2 = self.secondPoint.x +(self.firstPoint.x - self.secondPoint.x) * (factor)
print(str(x1))
y1 = self.firstPoint.y +(self.secondPoint.y - self.firstPoint.y) * (factor)
y2 = self.secondPoint.y +(self.firstPoint.y - self.secondPoint.y) * (factor)
else:
x1 = -(self.firstPoint.x +(self.secondPoint.x - self.firstPoint.x) * (factor))
x2 = -(self.secondPoint.x +(self.firstPoint.x - self.secondPoint.x) * (factor))
y1 = self.firstPoint.y +(self.secondPoint.y - self.firstPoint.y) * (factor)
y2 = self.secondPoint.y +(self.firstPoint.y - self.secondPoint.y) * (factor)
self.firstPoint = Point(x1, y1)
self.secondPoint = Point(x2, y2)
if __name__ == "__main__":
p1 = Point(5,5)
p2 = Point(20,35)
l1 = Line(p1,p2)
l1.scale(2)
p5 = Point(-2.5,-10)
p6 = Point(27.5,50)
assert l1.getEndPoints() == (p5,p6)
这些测试无法正常工作,但以上是.. 我得到 a(5.0, 5.0) 和 b(20.0, 35.0)
l1.scale(0.5)
p5 = Point(8.75,12.5)
p6 = Point(16.25,27.5)
class Point:
'''Point class represents and manipulates
x,y coordinates.'''
def __init__(self,x=0,y=0):
'''Create a new point with default
x,y coordinates at 0,0.'''
self.x = x
self.y = y
def distanceTo(self,aPoint):
return ((self.x-aPoint.x) ** 2 + (self.y-aPoint.y) ** 2)** .5
【问题讨论】:
-
你的 Line 类中的方法
scale不包含?请告诉我们你到目前为止做了什么 -
我不认为你们明白我的要求。我想将点 a(5,5) 的线改为点 (20,35) 并将其向外缩放 2。这意味着我想将点 a 变成 (-2.5, -10) 点 b进入 (27.5, 50)。在长度上,我想分别把33.54变成67.08,但我不想只扩展b,我也想扩展a。
-
嗯,就是这样,我的答案就是这样......
-
您能用数学术语还是用我的代码来解释?