2014-11-09 3 views
0

현재 과제에 문제가 있습니다. 몇 시간 동안 내 오류를 찾아 내기 위해 노력했습니다.삼각법을 사용하여 사각형의 새로운 중심을 계산합니다.

나는 원과 직사각형 (모두 사용자 클릭에 의해 그려 짐)을 가지고 있습니다. 다음과 같이 :

i.stack.imgur (점) COM/4aXIw.png, 내가 (사각형에 대한) 새로운 중심점을 산출 할 사각형의 중심점을 사용

그것은 거짓말 원. 나는 각도 (theta)를 계산하고 삼각 함수를 사용하여 새로운 중심점을 얻음으로써이를 달성합니다.

i.stack.imgur (점) COM/NoCBS.png 그러나

, 내 프로그램을 실행할 때 올바른 결과를 얻고 있지 않다. 여기에 표시 :

i.stack.imgur (점) COM/ZG9Ld.png

나는 백 번을 확인했지만, 나는 엉망이 어디 있는지 정확히 파악할 수없는 것. 나는 theta를 검사했고, 결과로 나온 각도는 정확합니다.

from graphics import * 
import math 

def main(): 

#setup window 
win = GraphWin("Traveling Rectangle",600,450) 
win.setCoords(0,0,600,450) 

click1 = win.getMouse() 
click2 = win.getMouse() 
radius = click2.x - click1.x 
circle = Circle(click1, radius) 
circle.draw(win) 

click3 = win.getMouse() 
click4 = win.getMouse() 
rect = Rectangle(click3, click4) 
rect.draw(win) 
rect.setFill("red") 
rect.setOutline("red") 

#the centerpoint of the initial rectangle 
initialrec_x = (click3.x + click4.x)/2 
initialrec_y = (click3.y + click4.y)/2 

#the trig to calculate the point on the circle 
theta = math.atan((initialrec_y-click1.y)/(initialrec_x-click1.x)) 
theta = int(math.degrees(theta)) 

#the new centerpoint values of x and y 
rec_x = click1.x + radius*math.cos(theta) 
rec_y = click1.y + radius*math.sin(theta) 

main() 

도움말을 크게 감상 할 수있다 : 당신이 그것을 실행하려는 경우

여기
theta = math.atan((initialrec_y-click1.y)/(initialrec_x-click1.x)) 
theta = int(math.degrees(theta)) 

rec_x = click1.x + radius*math.cos(theta) 
rec_y = click1.y + radius*math.sin(theta) 

전체 코드입니다 : 여기에 관련 코드, 내가 실수를 의심입니다!

죄송합니다. 사이트에서 이미지를 게시하지 못했습니다. 그래픽 라이브러리는 여기에서 찾을 수 있습니다 : mcsp.wartburg (점) 에듀/zelle/파이썬/graphics.py

내가 다른 그래픽 패키지를 사용하고 있지만, 나를 위해 다음과 같은 작품
+0

사각형의 좌표를 변경하거나 다시 그리는 것이 보이지 않습니다. –

+1

'math.cos()'와'math.sin()'은 각도가 아닌 라디안 단위의 인수를 원합니다. 또한,'math.atan()'은 상한 사분면 밖에서는 작동하지 않을 것입니다. 대신에'math.atan2()'를 원할 수도 있습니다. – Tony

답변

0

:

... 
initialrec_y = (click3.y + click4.y)/2 

theta = math.atan2(initialrec_y - click1.y, initialrec_x - click1.x) 
rec_x = click1.x + radius * math.cos(theta) 
rec_y = click1.y + radius * math.sin(theta) 

이 는 atan2 함수를 사용합니다. yx 입력의 부호를 참고하여 해당 점이있는 사분면을 올바르게 계산하고 그에 따라 각도를 반환합니다. 그 외, 코드와 유일한 차이점은 라디안 (atan2이 반환 함)을도 단위로 변환하지 않는다는 것입니다. sincos에 라디안이 필요합니다.