2

Here is my code:

# point of intersection between opposite and hypotenuse

x,y  =    pygame.mouse.get_pos()


# using formula for length of line

lenline1 = (x-x)**2 + (300-y)**2
lenline2 = (x-700)**2 + (y-300)**2

opposite = math.sqrt(lenline1)

adjacent = math.sqrt(lenline2)

# Converting length of lines to angle

PQ = opposite/adjacent
k = math.sin(PQ)
j = math.asin(k)

print(j)  

I'm not getting the results I expected, although after messing around with it I got close but it wasn't quite right. Could someone please tell me what I'm doin wrong. I have two lines: opposite and adjacent And I wish to get the angle using the inverse of sin. What am I doing wrong. I'm only a beginner so don't give too detailed info. I can't imagine this is hard to do.

Thanks.

4

3 回答 3

3

To find the angle between two lines, use the following relation:

cos(angle) = (l1 dot l2) / (|l1| |l2|)

That is,

dotproduct = l1x * l2x + l1y * l2y
lenproduct = |l1| * |l2|
angle = acos(dotproduct / lenproduct)

where l1x, l1y are the x,y components of the line l1.

于 2012-09-15T08:52:22.270 回答
1

不要为k计算而烦恼,它没有意义。

j = math.asin(PQ)

但是,这仅适用于直角三角形,您必须在正确的位置设置适当的边长。通常这不起作用,您需要使用点积方法

于 2012-09-15T08:38:59.663 回答
1

看起来您正在尝试找到三角形 (700,300)、(x,300)、(x,y) 的角度。你让它变得比它需要的复杂得多。斜边的长度是math.hypot((700-x),(300-y)),角度是math.atan2((700-x), (300-y))

于 2013-08-04T17:40:32.953 回答