Computer Science Canada Math Question |
Author: | Flikerator [ Mon Nov 13, 2006 9:04 pm ] | ||
Post subject: | Math Question | ||
I want to find the angle the line from one point to another (straight up is 180 defgrees, straight down is 270 degrees, ect...)
It uses the point of the player (p1) as the center of the circle. The object (p2) would then be the point resting on the radius of that circle. So the distance from p1 to p2 is the radius. Thats just the concept, the turing program just displays it. Mind blank as to how I should calculate it though. I've tried a few things but I end up with 0 or 1... |
Author: | rdrake [ Mon Nov 13, 2006 9:10 pm ] |
Post subject: | |
Use trig. |
Author: | Clayton [ Mon Nov 13, 2006 9:11 pm ] |
Post subject: | |
triangulate the points, then use trigonometry to find the points. In other words, find the distance between p1 and p2, then find the distance of one of the other preferred sides, then use the accompanying trig functions (sin, cos, tan) |
Author: | Flikerator [ Mon Nov 13, 2006 9:43 pm ] | ||
Post subject: | |||
How do right angle triangles help me here? o....o That would only find the angle inside the triangle between the points, and not in relation to a circle. In Degrees (Radians); East 0/360 (0,6.28) North 90 (1.57) West 180 (3.14) South 270 (4.71) I assume you mean like this?;
|
Author: | zylum [ Mon Nov 13, 2006 10:13 pm ] |
Post subject: | |
there are many topics like this in the turing help section plus googling yeilds many results. i dont think you tried very hard to find the answer. anyhoo, in grade 10 math, you learn sohcahtoa or something like that. what you are looking for is the 'toa' part, or tan(theta) = opposite/adjacent. so theta = arctan(opposite/adjacent). |
Author: | Flikerator [ Mon Nov 13, 2006 11:34 pm ] |
Post subject: | |
SohCahToa Sin -> Opposite over Hyp Cos -> Adjacent over Hyp Tan -> Op of Adj No one seems to read my posts. Right Angle triangle equations (trig) will NOT work. I just need the equation to find the angle in a triangle with Side-Side-Side. I've done all this before, I just don't memorize equations. It has something to do with Cosine, but that returns me wrong equations when I google it (too many results ![]() |
Author: | Clayton [ Mon Nov 13, 2006 11:43 pm ] |
Post subject: | |
see, the funny thing is, trig is exactly what you need. |
Author: | zylum [ Tue Nov 14, 2006 12:19 am ] |
Post subject: | |
wtf ![]() dx = P2.x - P1.x dy = P2.y - P1.y theta = arctan(dy/dx) what exactly do you need this angle for anyways? |
Author: | Flikerator [ Thu Nov 16, 2006 11:44 pm ] | ||
Post subject: | |||
"Trig is exactly what you need", yet you don't say why. Explain, show me why. I'm here for help not your elitist attitude. I do not want the INTERNAL angle. I don't care if the angle is "30 degrees". I want to know it related to a circle. Here, take this program as an example.
180 degrees is a flat line traveling right/traveling east. A line going the right direction would be 0 or 360 degrees. I did not mean that TRIG would not be useful, I meant that RIGHT ANGLE trig would do no good. ie SohCahToa |
Author: | md [ Fri Nov 17, 2006 12:48 am ] |
Post subject: | |
If you have the internal angle, and you know the up/down left/right direction the line points in then you can find hte real angle (ie in 360 degrees) fairly easily. Draw an xy axis graph, draw a line in each of the four quadrants. See how the sign of the directions is different in each quadrant? Like everyone said; very simple! ![]() |
Author: | zylum [ Fri Nov 17, 2006 7:59 am ] |
Post subject: | |
maybe you need to explain the problem a bit better.. in you first post you said you need the angle between 2 points. in your last post, your program draws 3 points - the centre of the circle and 2 perimeter points. so what exactly are you looking for? which angle in the progrom do you want? |
Author: | Flikerator [ Sat Nov 18, 2006 5:06 pm ] |
Post subject: | |
zylum wrote: maybe you need to explain the problem a bit better.. in you first post you said you need the angle between 2 points. in your last post, your program draws 3 points - the centre of the circle and 2 perimeter points. so what exactly are you looking for?
which angle in the progrom do you want? Edit: I do not mean this sarcastically ![]() I do need the angle between two points, in relation to a circle. I even made a program, in my first post, showing what I meant since "in relation to a circle" might not make any sense. If you look at the program (Turing) it only has two points. The second program was to show that the two points didn't always make right angle triangles (When relating them to a circle). I could use the SohCahToa trig rule for which quadrant. Yes in that case it would work, thank you. Although I would still like that forumula since it would cut down on if statements; 2 of the 4 quadrants; Quad1=(if x1 > x2 and y1 > y2 then) Angle = SohCahToa + 0 Quad4=(if x1 > x2 and y1 < y2 then) Angle = SohCahToa + 270 It was like Cos (a) = A(A)+B(B)+C(C)-2ABC Something like that[/b] |
Author: | md [ Sat Nov 18, 2006 6:40 pm ] |
Post subject: | |
There is no way of doing it without if statements; or if there is finding it is more work then using if statements. Really, if your trying to "optimize" the code by using fewer lines I think you've got bigger issues... especially since line count very rarely has anything to do with speed. |
Author: | Brightguy [ Sat Nov 18, 2006 7:43 pm ] |
Post subject: | Re: Math Question |
Your description is confusing... and it doesn't help that most people can't run Turing programs. I think you are looking for the angle between two vectors... the angle between x and y is given by arccos[(x·y)/(|x||y|)]. |
Author: | zylum [ Sat Nov 18, 2006 11:47 pm ] |
Post subject: | |
yes, you can use the dot product like brightguy mintioned or you can use the cosine rule. im sure you can find both formulas somewhere. |
Author: | Clayton [ Sun Nov 19, 2006 1:29 pm ] |
Post subject: | |
or, if you really need the cosine law : a^2 = b^2 + c^2 - 2bc * (cos A) |
Author: | Flikerator [ Fri Nov 24, 2006 11:35 pm ] |
Post subject: | |
Thank you, cosine law works perfectly (exactly what I was looking for). Note: I optimize as much as I can because I feel that it leads to much more efficient programming for other programs. If I just find one way to get it done I learn one way to do it. If I learn many, I can tackle different problems in many ways. I don't care about line length; I like nice neat programs that work efficiently. |
Author: | Flikerator [ Sat Dec 02, 2006 1:06 am ] |
Post subject: | |
Used a really neat method to solve it. I decided I would show you what I meant; and incase some people don't own turing, its an excecutable. The angle is required for a different program, I just built this quickly to illustrate what I mean. In relation to the circle. The center point is the person or whatever looking at your mouse, the object (other player, ect..). The angle is in the top left. |
Author: | zylum [ Sat Dec 02, 2006 2:12 am ] | ||
Post subject: | |||
you mean something like this?
hey look at that! it uses simple 90 degree triangle trig? and what do you know, its also pretty neat and efficient. hey, and the result is pretty close to the true angle unlike your program where the angle could be off by as much as 10 degrees when you get close to the centre point. |
Author: | Brightguy [ Sat Dec 02, 2006 8:37 am ] |
Post subject: | Re: Math Question |
For future reference, that's usually called the polar angle - you'll see it again whenever you do polar coordinates. In some programming languages (C++, Java) the function atan2 will find it (though it outputs between -pi and pi rather than 0 and 2pi). |
Author: | Flikerator [ Sat Dec 02, 2006 11:54 pm ] | ||
Post subject: | |||
zylum wrote: you mean something like this?
hey look at that! it uses simple 90 degree triangle trig? and what do you know, its also pretty neat and efficient. hey, and the result is pretty close to the true angle unlike your program where the angle could be off by as much as 10 degrees when you get close to the centre point. An arrogant programmer; how... original. ![]() Quote: For future reference, that's usually called the polar angle - you'll see it again whenever you do polar coordinates. In some programming languages (C++, Java) the function atan2 will find it (though it outputs between -pi and pi rather than 0 and 2pi).
Thanks for the info, never heard of Polar Angle before. You could add pi and then always have output between pi and 2pi couldn't you? Not that its necessary ^_^ ![]() |
Author: | OneOffDriveByPoster [ Sun Dec 03, 2006 12:38 am ] |
Post subject: | |
Flikerator wrote: You could add pi and then always have output between pi and 2pi couldn't you? Not that its necessary ^_^
![]() add 2 pi if the result is negative ![]() |
Author: | Clayton [ Sun Dec 03, 2006 9:34 am ] |
Post subject: | |
Why do you add 2pi if the result is negative? |
Author: | zylum [ Sun Dec 03, 2006 10:22 pm ] |
Post subject: | |
I'm not being arrogant. I'm just slightly annoyed that despite four or five people giving you the solution to your problem, you seem to know better. Since you deemed all the solutions presented as useless I thought I might have misunderstood the problem and refrained from posting and source. Then you claim you used a "really neat method to solve it" without even describing it. After testing your program I find that your method gives inaccurate results so I post a program that does what you intended using one of the methods suggested. I'm sorry if I sounded arrogant but wouldn't you be annoyed if you tried to help someone but they dismissed your ideas without even trying them? Oh and you don't need a protractor to see that your program produces inaccurate angles. Simply place your cursor a couple of pixels beneath the center of the reference point, it should be 270 degrees. |
Author: | OneOffDriveByPoster [ Sun Dec 03, 2006 10:47 pm ] |
Post subject: | |
Freakman wrote: Why do you add 2pi if the result is negative?
If you wanted change the ( -pi, pi ] result to a [ 0, 2pi ) result. The OP's suggestion of adding pi regardless would have the answer changed by 180 degrees. |
Author: | Flikerator [ Mon Dec 04, 2006 3:27 pm ] |
Post subject: | |
zylum wrote: I'm not being arrogant. I'm just slightly annoyed that despite four or five people giving you the solution to your problem, you seem to know better. Since you deemed all the solutions presented as useless I thought I might have misunderstood the problem and refrained from posting and source. Then you claim you used a "really neat method to solve it" without even describing it. After testing your program I find that your method gives inaccurate results so I post a program that does what you intended using one of the methods suggested. I'm sorry if I sounded arrogant but wouldn't you be annoyed if you tried to help someone but they dismissed your ideas without even trying them?
Oh and you don't need a protractor to see that your program produces inaccurate angles. Simply place your cursor a couple of pixels beneath the center of the reference point, it should be 270 degrees. I was looking for a method that calculated the polar angle and I couldn't see how right angle triangles could be used (obvious now that I see it); thank you for showing me. As for the really 'neat method' it was something I came up with to not use if statements (just arrays, lists and more calculation's, yours was more efficient obviously). The angle issue was because its places the circles on a rounded spot, but the actual x, y coordinates are decimals. If you change the div to just "/" in the whatAngle function you'll get the same/similar result for your prog. Sorry for being somewhat of an asshole XD |