Author

Thread





03182003 15:51
Posted by:
puffin

how could i calculate the angle between two line segments that share a common point? i read the thread "Angles" but it sounds like we're talking about different things.

03192003 02:57
Posted by:
puffin

almost there
i've almost completed my procedure for finding the angle between two lines with a common point, but i was wondering if there is an arc cos (the inverse of cosine) function in tnt basic?

03232003 02:11
Posted by:
Holmes
Location:
Santa Rosa, Ca

Yay, more vectors!!!
Yes, TNT has inverse cosine, its invcos
But anyhow, this sounds like vector math to me so let me help you with this. From crazy vector mathematics we know that the cosine of the angle between to vectors (lines) is equal to the dot product of vector V and W over the quantity of the magnitude of vector V times the magnatude of vector W. In mathematical notation, it looks something like this:
cos(angle) = (v*w) / (v w)
So knowing that (just kid me here people)
Lets say we have four variables:
lineOneStart[2] 'This contains the x,y coordinates of where line 1 starts
lineOneEnds[2] 'The same, but for the end point
lineTwoStarts[2] 'These are the x,y coordinates where line two begins
lineTwoEnds[2] 'This is where line two ends
So from those variables we get:
lineOneXLength = lineOneEnd[0]lineOneStart[0]
lineOneYLength = lineOneEnd[1]lineOneStart[1]
lineOneMagnitude = sqrt(lineOneXLength*LineOneXLength+lineOneYLength*LineOneYLength)
lineTwoXLength = lineTwoEnd[0]lineTwoStart[0]
lineTwoYLength = lineTwoEnd[1]lineTwoStart[1]
lineTwoMagnitude = sqrt(lineTwoXLength*LineTwoXLength+lineTwoYLength*LineTwoYLength)
lineOneDotLineTwo = lineOneXLength*lineTwoXLength + lineOneYLength*lineTwoYLength
Angle = invcos( lineOneDotLineTwo / (lineOneMagnitude * lineTwoMagnitude ))

03232003 02:15
Posted by:
Holmes
Location:
Santa Rosa, Ca

P.S. What on earth are you using this for?

03232003 09:37
Posted by:
matteo
Location:
Venice, ITALY!

...

03232003 19:28
Posted by:
puffin

example
oops, i wrote this in a new thread instead of replying to the original one.
anyway here is the code for which i needed arc cos:
graphics mode hardware 800, 600
load images 128
set framerate max
'draw tracking off
int x
int y
int d
int r
int da
int db
int dc
float cosr
da = 300
set sprite color 0, make color (255, 0, 255)
set pen transparency 70
line 400, 300 to 400, 0
repeat
x = mouse x
y = mouse y
line 400, 300 to x, y
db = sqrt ((400  x)^2 + (300  y)^2)
dc = sqrt ((400  x)^2 + (0  y)^2)
cosr = ((dc^2  da^2  db^2) / (2 * da * db))
r = round(invcos(cosr))
if mouse x = 400 then r = r
if mouse x <= 400 then d = 180  r
r = r + d
if r < 0 then d = 180 + r
r = r + d
sprite 0, 400, 300, 0
set sprite rotation 0, r
draw frame
until true = pressed (12)
so now i have a problem when the rotation is at 180 degrees. i wont go to the trouble of explaining the code, but i'd love it if someone could try it out and offer me an explanation for its strangeness. just put in a sprite for image 128. the lines that appear will just be imaginary, but i've drawn them here so you can see what's happening.
all this code is just a work around for the macs inability to wrap the mouse around. it rotates the sprite so that it always faces the mouse coordinates.

03252003 03:21
Posted by:
Holmes
Location:
Santa Rosa, Ca

Your code does indeed look bizarre, and not very reusable either...
I like my weird code better than your weird code :P

03252003 03:31
Posted by:
Holmes
Location:
Santa Rosa, Ca

The more I look at your code, the more bizarre it becomes...
"(0y)^2", I believe, is the same thing as y^2...but thats just a minor detail.
Really, what is this code doing? Is is just pointing a sprite at the mouse? If so there is are much more efficient and shorter ways to do this.

03252003 12:53
Posted by:
puffin

how?
as a matter of fact, this does just point a sprite at the mouse, so if you know a better way, let me in on it.

03272003 00:13
Posted by:
Holmes
Location:
Santa Rosa, Ca

Okay, so you're using invCos to find the angle. This requires a side and the magnitude (length). A better way is to use invTan.
Tan(angle) = opposite side / adjacent side
angle = invTan(opposite side / adjacent side)
Knowing that, we can write a function like I have below:
<actual TNT code>
Procedure findAngleForPoints(int point1x,int point1y,int point2x,int point2y)
float angle, tanangle
tanangle = (point1ypoint2y) / (point1xpoint2x)
if point1x < point2x
angle = invtan(tanangle)
else
angle = invtan(tanangle)+180
end if
end Proc (angle)
</actual TNT code>
So, here's how to use this function, use the sprite coordinates for point1, and the mouse coordinates for point2. The little if statement in the function is to compensate for invtan's limited range.

03272003 16:35
Posted by:
puffin

thanks to Holmes. the procedure you gave me works well and without the wiered behaivior at 180 and 360 degrees that mine was having. it dosn't point the sprite at the mouse, but rather 90 degrees counter clockwise to the mouse, but thats an easy enough problem to fix.

03272003 16:37
Posted by:
puffin

Holmes procedure also runs at about 2 frames per second faster than mine.

03272003 22:51
Posted by:
Holmes
Location:
Santa Rosa, Ca

No problem. I never mind showing off my code :)
Holmes

All times are GMT




