Math Is Fun Forum

  Discussion about math, puzzles, games and fun.   Useful symbols: ÷ × ½ √ ∞ ≠ ≤ ≥ ≈ ⇒ ± ∈ Δ θ ∴ ∑ ∫ • π ƒ -¹ ² ³ °

You are not logged in.

#1 2005-10-09 10:28:02

mikau
Member
Registered: 2005-08-22
Posts: 1,504

My C++ polarangle function

This 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).

I just don't like using a whole mess of "if" statements, and I want to make the function as simple, short, and direct as possible. Take a look at the function, tell me what you think of it, and tell me if you think it can be simplified or if some steps are redundant.

float polarangle(float centh, float centv, float objh, float objv)
{

  float h, v, r;
  v = objv - centv;    // finding vertical distance from centv, to objv

  h = objh - centh;    //finding horrizontal distance from centh, to objh

  if ((h == 0) && (v < 0)) { return 270;}     // avoid division by zero
  if ((h == 0) && (v > 0)) { return 90; }     // avoid division by zero
  if ((h == 0) && (v == 0)) { return 0; }     // points overlap, no angle

  r = atan(v/h) * 180/3.14159;                //calling arctan and converting radians to degree's

  if (h < 0) { return (r + 180);}


return r;

}


A logarithm is just a misspelled algorithm.

Offline

#2 2005-10-09 10:34:14

mikau
Member
Registered: 2005-08-22
Posts: 1,504

Re: My C++ polarangle function

I really need to rename the parameters... :-/


A logarithm is just a misspelled algorithm.

Offline

#3 2005-10-09 10:49:15

mikau
Member
Registered: 2005-08-22
Posts: 1,504

Re: My C++ polarangle function

I'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.

Offline

#4 2005-10-09 20:45:37

MathsIsFun
Administrator
Registered: 2005-01-21
Posts: 7,711

Re: My C++ polarangle function

Function looks good, clear and simple, and should translate well to many similar languages.

I usually prefer "x" and "y" rather than "h" and "v".

Also, don't say "obj" because that makes me think it is something to do with "Object Oriented". How about "Origin"?

BTW you could use some of the "Object Oriented" functions of C++. For example you could create a "point" class, which could have that function (and the radius) built into it.


"The physicists defer only to mathematicians, and the mathematicians defer only to God ..."  - Leon M. Lederman

Offline

#5 2005-10-10 10:50:17

mikau
Member
Registered: 2005-08-22
Posts: 1,504

Re: My C++ polarangle function

Great 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.

I'm writing a math program to calculate the location of a point (on an x,y plane) based on its x,y, z coordinates, the observers x,y,z coordinates, and the angle at which he's looking. Basicly I'm reinventing the 3d engine myself, trying to figure things out on my own. I've heard real 3d engines just use matrices but my system seems to work anyways.

Your object and origin suggestions are good, the only reason I didn't pic "origin" is because that usually means 0,0,0.


A logarithm is just a misspelled algorithm.

Offline

#6 2005-10-10 11:45:39

mikau
Member
Registered: 2005-08-22
Posts: 1,504

Re: My C++ polarangle function

Darn 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.

Offline

#7 2005-10-10 11:47:42

mikau
Member
Registered: 2005-08-22
Posts: 1,504

Re: My C++ polarangle function

Well 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-10 11:49:41)


A logarithm is just a misspelled algorithm.

Offline

#8 2005-10-10 11:49:09

mikau
Member
Registered: 2005-08-22
Posts: 1,504

Re: My C++ polarangle function

It works!


A logarithm is just a misspelled algorithm.

Offline

#9 2005-10-10 15:05:11

mikau
Member
Registered: 2005-08-22
Posts: 1,504

Re: My C++ polarangle function

It 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.

Offline

#10 2005-10-10 15:35:26

mikau
Member
Registered: 2005-08-22
Posts: 1,504

Re: My C++ polarangle function

Grrr.... well it seems theres also some slight inacuracy with sin(x) at some angles as well. Phoey!

What I need is a way to round a number to 4, or 5 decimal places.


A logarithm is just a misspelled algorithm.

Offline

#11 2005-10-14 14:19:12

ryos
Member
Registered: 2005-08-04
Posts: 394

Re: My C++ polarangle function

If C++ doesn't have a ready-made function for that (it really should), here's a half-baked algorthm for rounding.

First, I can't remember how C++ handles rounding when you assign a float to an integer; I think it truncates rather than rounds. If so, the algorithm (in psuedocode) looks something like this:

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.

It's probably possible to do the same thing using bitwise wizardry, and such an approach may be simpler. But it would be a lot harder to implement and understand. Take your pick. smile


El que pega primero pega dos veces.

Offline

#12 2005-10-14 14:27:37

ryos
Member
Registered: 2005-08-04
Posts: 394

Re: My C++ polarangle function

Oh, 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.

Offline

#13 2005-10-14 18:33:52

MathsIsFun
Administrator
Registered: 2005-01-21
Posts: 7,711

Re: My C++ polarangle function

Sorry mikau, I lost track of this conversation.

How is it going now? Post code and problem if you can.


"The physicists defer only to mathematicians, and the mathematicians defer only to God ..."  - Leon M. Lederman

Offline

#14 2005-10-16 11:20:19

mikau
Member
Registered: 2005-08-22
Posts: 1,504

Re: My C++ polarangle function

Things 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.

But in the end, it seemed the inaccuaracies were small enough not to be detrimental to the program.


A logarithm is just a misspelled algorithm.

Offline

#15 2005-10-16 14:00:26

ryos
Member
Registered: 2005-08-04
Posts: 394

Re: My C++ polarangle function

Yeah, I doubt that, with screen resolution being what it is, you'd have much of a problem.

It occurs to me that you might try writing it to the precision storable by an unsigned double. That's the ceiling on the precision of your program, right? So that should make it come out zero? I may not understand the issues but it seems reasonable to me.


El que pega primero pega dos veces.

Offline

#16 2005-10-16 14:04:13

mikau
Member
Registered: 2005-08-22
Posts: 1,504

Re: My C++ polarangle function

Well radians will always be positive...hmmm... might be a good idea. :-)


A logarithm is just a misspelled algorithm.

Offline

#17 2005-10-16 14:40:57

ryos
Member
Registered: 2005-08-04
Posts: 394

Re: My C++ polarangle function

Hey, 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.

Offline

#18 2005-10-16 14:47:41

mikau
Member
Registered: 2005-08-22
Posts: 1,504

Re: My C++ polarangle function

Hmm... 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.

Offline

#19 2005-10-16 14:52:49

mikau
Member
Registered: 2005-08-22
Posts: 1,504

Re: My C++ polarangle function

(edit) oops! Wrong thread! x_x

Last edited by mikau (2005-10-16 14:55:27)


A logarithm is just a misspelled algorithm.

Offline

Board footer

Powered by FluxBB