Discussion about math, puzzles, games and fun. Useful symbols: ÷ × ½ √ ∞ ≠ ≤ ≥ ≈ ⇒ ± ∈ Δ θ ∴ ∑ ∫ • π ƒ -¹ ² ³ °
| |
|
|
You are not logged in. #1 2005-10-10 08:28:02
My C++ polarangle functionThis function is to find the polar angle from "cent" (a center point) and obj (an object) Typically when we write something in polarform we begin from the origin (0,0) but this allows you to speficy the origin. First it finds the horizontal distance from the center to the object and then finds the vertical distance. The vertical distance is divided by the horizontal distance to find the tangent. Then atan() is used (arctangent) to find the angle to which the tangent belongs (but atan returns the value in radians so I converted it). I used a few "if" statements first to return 270 or 90 when the adjacent is zero, to avoid division by zero, and also when oposite and adjacent are both zero, (which means the points overlap and there is no angle relation).
A logarithm is just a misspelled algorithm. #2 2005-10-10 08:34:14
Re: My C++ polarangle functionI really need to rename the parameters... :-/ A logarithm is just a misspelled algorithm. #3 2005-10-10 08:49:15
Re: My C++ polarangle functionI'm going to write a function for the distance formula to find the directed distance from the center point, to the object, then I can write a function that calls these two express any points coordinates in referance in polar form only in relation to any center point you chose, rather then just the origin. The primary purpose of this will be used to rotate a point around an axis (the center) by first converting to polar form, adding or subtracting the angle of rotation, then converting back to rectangular from to get the new coordinates of the point. A logarithm is just a misspelled algorithm. #4 2005-10-10 18:45:37
Re: My C++ polarangle functionFunction looks good, clear and simple, and should translate well to many similar languages. "The physicists defer only to mathematicians, and the mathematicians defer only to God ..." - Leon M. Lederman #5 2005-10-11 08:50:17
Re: My C++ polarangle functionGreat minds think alike. But I have my reasons. I already have a class "point" with x,y,z member data. But this polar angle function is going to be used not just on the x,y plane. Its going to be used on x,z plane, a y,z plane, as well as the x,y plane. So if I use x and y, in somecases I'd have to use y, for x, instead of y, for y. The same reason why I didn't pass in "points" as arguements. Because sometimes I'll be using only their y,z coordinates, other times their x,z coordinates, etc. There is probably a simple way to do it with points but I haven't thought of one yet. A logarithm is just a misspelled algorithm. #6 2005-10-11 09:45:39
Re: My C++ polarangle functionDarn it! Cos(x) is messing up and giving me a very small decimal number for the cosine of 90 instead of 0. ( and no I didn't forget to convert to radians) A logarithm is just a misspelled algorithm. #7 2005-10-11 09:47:42
Re: My C++ polarangle functionWell I guess I could just take the sine of the complement angle since thats what cosine means and the sine function appears to be working fine. (I call this sort of thing "humoring the computer") Last edited by mikau (2005-10-11 09:49:41) A logarithm is just a misspelled algorithm. #8 2005-10-11 09:49:09
Re: My C++ polarangle functionIt works! A logarithm is just a misspelled algorithm. #9 2005-10-11 13:05:11
Re: My C++ polarangle functionIt would be much easier (and clearer) to just pass in points as arguements, rather then having to individually pass in horizontal and vertical coordinates. But then I'd have to define a new function for every plane. xy, xz, yz. I wonder if it would be worth it... A logarithm is just a misspelled algorithm. #10 2005-10-11 13:35:26
Re: My C++ polarangle functionGrrr.... well it seems theres also some slight inacuracy with sin(x) at some angles as well. Phoey! A logarithm is just a misspelled algorithm. #11 2005-10-15 12:19:12
Re: My C++ polarangle functionIf C++ doesn't have a ready-made function for that (it really should), here's a half-baked algorthm for rounding. Code:float number; //The number to be rounded
p = 5; //The precision to round to
float bumper = number * 10^p;
int chopper = (int) bumper;
int tester = (int) (bumper + 0.5);
if (tester > chopper) { float roundedNumber = tester * (1/10^p); }
else { float roundedNumber = chopper * (1/10^p); }Again, my idea of C++'s rounding behaviour may be off, but I think you get the idea. El que pega primero pega dos veces. #12 2005-10-15 12:27:37
Re: My C++ polarangle functionOh, I just read the whole discussion. Your imprecision is probably coming from the conversion to radians, since you only use 5 decimal places of pi. And, it looks like your imprecision starts where your precision of pi ends, which makes sense if you've had the misfortune of having to work with significant figures. El que pega primero pega dos veces. #13 2005-10-15 16:33:52
Re: My C++ polarangle functionSorry mikau, I lost track of this conversation. "The physicists defer only to mathematicians, and the mathematicians defer only to God ..." - Leon M. Lederman #14 2005-10-17 09:20:19
Re: My C++ polarangle functionThings are going well. I just ignored the slight inaccuracies. I already tried writing pi to a lot more decimal places but that just ended up writing really small numbers in scientific notation when it should have been zero. A logarithm is just a misspelled algorithm. #15 2005-10-17 12:00:26
Re: My C++ polarangle functionYeah, I doubt that, with screen resolution being what it is, you'd have much of a problem. El que pega primero pega dos veces. #16 2005-10-17 12:04:13
Re: My C++ polarangle functionWell radians will always be positive...hmmm... might be a good idea. :-) A logarithm is just a misspelled algorithm. #17 2005-10-17 12:40:57
Re: My C++ polarangle functionHey, wait, I was wrong--radians will not always be positive in the important physical sense of direction of motion... El que pega primero pega dos veces. #18 2005-10-17 12:47:41
Re: My C++ polarangle functionHmm... yeah I guess if we ever need the sin, cosine, tangent or arctangent of a negative angle, that would occur. A logarithm is just a misspelled algorithm. #19 2005-10-17 12:52:49
Re: My C++ polarangle function(edit) oops! Wrong thread! x_x Last edited by mikau (2005-10-17 12:55:27) A logarithm is just a misspelled algorithm. |