1. trig

Howdy folks,

It seems I always forget my trig when I need it.
Quick question:

How do I calculate an angle in degrees given an x,y vector.
The vector for 0 deg being {0,-1}
90 deg would be {1,0}; 180 = {0,1}; etc...

atom angle
sequence vector
vector = {-0.38,-0.92}
angle = ?

thanks for any help,
Lewis Townsend

BTW is there a website that has all the math formulas, trig esp?

new topic     » topic index » view message » categorize

2. Re: trig

not an expert though. taken from my calculator handbook.

rectangular to spherical coordinate conversion
r = sqareroot( x^2 + y^2 + z^2)
angle = tan ^ -1 y/x
ANGLE = tan ^ -1 squareroot(x^2 + y^2)  / z
in picture , angle is the horizontal angle between X and Y axis
ANGLE is the vertical angle off the Z axis
hope this helps.
my search engine gave me these, searching for "math equations"
http://mathforum.org/library/drmath/sets/high_equations.html
www.w3.org/Math
www.math.com

rudy
lotterywars

new topic     » goto parent     » topic index » view message » categorize

3. Re: trig

On Sat, 17 Apr 2004 22:02:20 +0000, Lewis Townsend
<Keroltarr at hotmail.com> wrote:

>It seems I always forget my trig when I need it.
We all do blink
>Quick question:
>
>How do I calculate an angle in degrees given an x,y vector.
>The vector for 0 deg being {0,-1}
>90 deg would be {1,0}; 180 = {0,1}; etc...

I can't quite picture what you're saying, but I think of arctan(0)=0,
arctan(1)=45, and arctan(inf)=90, which I think should get you
started. I have to admit radians loose me, but in Eu:
?arctan(0)
?arctan(1)/(3.1415926/2)
?arctan(1e308)/(3.1415926/2)
prints:
0
0.5000000085
1.000000017

HTH,
Pete

new topic     » goto parent     » topic index » view message » categorize

4. Re: trig

cos(u-v) = cos(u)*cos(v) + sin(u)*sin(v)
cos(.5PI-v) = sin(v)
sin(.5PI-u) = cos(u)
sin(-v) = -sin(v)
cos(-v) = -cos(v)
cos(u+v) = cos(u)*cos(v) - sin(u)*sin(v)
sin(u+v) = sin(u)*cos(v) + cos(u)*sin(v)
sin(u-v) = sin(u)*cos(v) - cos(u)*sin(v)
tan(u+v) = (tan(u) + tan(v)) / (1 - tan(u)*tan(v))

equation of a line = y = mx + b

angle(t) between 2 lines:
tan(t) = (m2 - m1) / (1 + m2*m1)

if m2 * m1 = -1 the lines are parallel

sin(2*t) = 2 * sin(t) * cos(t)
cos(2*t) = power(cos(t),2) - power(sin(t),2) = 1 - 2  * 
power(sin(t),2) = 2 * power(cos(t),2) -1
tan(2*t) = 2 * tan(t) / (1 - power(tan(t),2))
power(sin(t),2) = (1 - cos(2*t)) / 2
power(cos(t),2) = (1 + cos(2*t)) / 2
tan(t) = sin(2 * t) / (1 + cos(2*t)) = (1 - cos(2 * t)) / sin(2 * t)

csc(t) = 1 / sin(t)
sec(t) = 1 / cos(t)
cot(t) = 1 / tan(t) = cos(t) / sin(t)
tan(t) = sin(t) / cos(t)
power(cos(t),2) + power(sin(t),2) = 1
power(tan(t),2) + 1 = power(sec(t),2)
power(cot(t),2) + 1 = power(csc(t),2)

sin(u + v) = sin(u) * cos(v) + cos(u) * sin(v)
sin(u - v) = sin(u) * cos(v) - cos(u) * sin(v)
cos(u + v) = cos(u) * cos(v) + sin(u) * sin(v)
cos(u - v) = cos(u) * cos(v) - sin(u) * sin(v)
tan(u + v) = (tan(u) + tan(v)) / (1 - tan(u) * tan(v))
tan(u - v) = (tan(u) - tan(v)) / (1 + tan(u) * tan(v))

power(sin(t),2) = .5 * (1 - cos(2 * t))
power(cos(t),2) = .5 * (1 + cos(2 * t))


sin(u + v) + sin(u - v) = 2 * sin(u) * cos(v)
sin(u + v) - sin(u - v) = 2 * cos(u) * sin(v)
cos(u - v) + cos(u + v) = 2 * cos(u) * cos(v)
cos(u - v) - cos(u + v) = 2 * sin(u) * sin(v)

sin(x) + sin(y) = 2 * sin(.5 * (x + y)) * cos(.5 * (x - y))
sin(x) - sin(y) = 2 * cos(.5 * (x + y)) * sin(.5 * (x - y))
cos(x) + cos(y) = 2 * cos(.5 * (x + y)) * cos(.5 * (x - y))
cos(x) - cos(y) = -2 * sin(.5 * (x + y)) * sin(.5 * (x - y))

new topic     » goto parent     » topic index » view message » categorize

5. Re: trig

Okay,

Maybe I need to explain myself better:
I have broken up the circle (360 deg) into 16 equal angles; each 22.5 degrees.
I have a sequence of these angles:
{0,22.5,45,67.5,90,112.5,135,157.5,180,202.5,225,247.5,270,292.5,315,337.5}

>From that I have evaluated 16 {x,y} vectors with the following formula:
{ sin (angle), cos (angle) }
The above is simplified for readability; degree/radian conversion is done
separately.
This produces the following sequence <approximately>
{{0,1},{0.383,0.924},{0.707,0.707},{0.924,0.383},{1,0},{0.924,-0.383},{0.707,-0.707},{0.383,-0.924},{0,-1},{-0.383,-0.924},{-0.707,-0.707},{-0.924,-0.383},{-1,0},{-0.924,0.383},{-0.707,0.707},{-0.383,0.924}}

I want to recognize any random {x,y} vector as its closest match in the list.
I can scale any vector to a distance of 1 with the following code:

dir = random_vector --{x,y}
dist = sqrt(power(dir[1],2)+power(dir[2],2))
if dist != 1 then
	dir[1] = dir[1] * 1 / dist
	dir[2] = dir[2] * 1 / dist
end if

The question is how do I find its closest match in either the vector list or the
angle list.
All I need is an element number.

new topic     » goto parent     » topic index » view message » categorize

6. Re: trig

Lewis Townsend wrote:
> 
> 
> Okay,
> 
> Maybe I need to explain myself better:
> I have broken up the circle (360 deg) into 16 equal angles; each 22.5 degrees.
> I have a sequence of these angles:
> {0,22.5,45,67.5,90,112.5,135,157.5,180,202.5,225,247.5,270,292.5,315,337.5}
> 
> >From that I have evaluated 16 {x,y} vectors with the following formula:
> { sin (angle), cos (angle) }
> The above is simplified for readability; degree/radian conversion is done
> separately.
> This produces the following sequence <approximately>
>
> {{0,1},{0.383,0.924},{0.707,0.707},{0.924,0.383},{1,0},{0.924,-0.383},{0.707,-0.707},{0.383,-0.924},{0,-1},{-0.383,-0.924},{-0.707,-0.707},{-0.924,-0.383},{-1,0},{-0.924,0.383},{-0.707,0.707},{-0.383,0.924}}
> 
> I want to recognize any random {x,y} vector as its closest match in the list.
> I can scale any vector to a distance of 1 with the following code:
> 
> dir = random_vector --{x,y}
> dist = sqrt(power(dir[1],2)+power(dir[2],2))
> if dist != 1 then
> 	dir[1] = dir[1] * 1 / dist
> 	dir[2] = dir[2] * 1 / dist
> end if
> 
> The question is how do I find its closest match in either the vector list or
> the
> angle list.
> All I need is an element number.

Lewis,
I imagine that you could do this:
1 - take random x,y vector and convert to an angle
2 - normalize that angle so 0 to 360 deg becomes 0.0 to 1.0
3 - multiply this result by the length of the angle list (in this case 16)
4 - maybe do some bounds checking, blah blah, blah...

Regards,
Mike

new topic     » goto parent     » topic index » view message » categorize

7. Re: trig

Lewis Townsend wrote:
> I have broken up the circle (360 deg) into 16 equal angles; each 22.5 degrees.
> I have a sequence of these angles:
> {0,22.5,45,67.5,90,112.5,135,157.5,180,202.5,225,247.5,270,292.5,315,337.5}
> 
> >From that I have evaluated 16 {x,y} vectors with the following formula:
> { sin (angle), cos (angle) }
> The above is simplified for readability; degree/radian conversion is done
> separately.
> This produces the following sequence <approximately>
>
> {{0,1},{0.383,0.924},{0.707,0.707},{0.924,0.383},{1,0},{0.924,-0.383},{0.707,-0.707},{0.383,-0.924},{0,-1},{-0.383,-0.924},{-0.707,-0.707},{-0.924,-0.383},{-1,0},{-0.924,0.383},{-0.707,0.707},{-0.383,0.924}}
> 
> I want to recognize any random {x,y} vector as its closest match in the list.
> I can scale any vector to a distance of 1 with the following code:
> 
> dir = random_vector --{x,y}
> dist = sqrt(power(dir[1],2)+power(dir[2],2))
> if dist != 1 then
> 	dir[1] = dir[1] * 1 / dist
> 	dir[2] = dir[2] * 1 / dist
> end if
> 
> The question is how do I find its closest match in either the vector list or
> the
> angle list.
> All I need is an element number.

1. sequence possible_values = {-1, -0.924, -0.707, -0.383, 0, 0.383, 0.707,
0.924, 1}
2. atom ax = closest match of x in the sequence above (example: -0.707)
3. atom ay = closest match of y in the sequence above (example: 0.707)
4. integer index = find({ax, ay},
{{0,1},{0.383,0.924},{0.707,0.707},{0.924,0.383},{1,0},{0.924,-0.383},{0.707,-0.707},{0.383,-0.924},{0,-1},{-0.383,-0.924},{-0.707,-0.707},{-0.924,-0.383},{-1,0},{-0.924,0.383},{-0.707,0.707},{-0.383,0.924}})

function abs(atom a) -- absolute value
    if a >= 0 then return a else return -a end if
end function

-- atom x, y = dir[1], dir[2]
sequence possible_values
atom ax, ay, dx, dy

possible_values = {-1, -0.924, -0.707, -0.383, 0, 0.383, 0.707, 0.924, 1}

ax = possible_values[1] -- ax = closest approximation of x
ay = possible_values[1] -- ay = closest approximation of y
dx = abs(x - ax) -- dx = difference between x and ax
dy = abs(y - ay) -- dy = difference between y and ay
for i = 2 to length(possible_values) do
    if abs(x - possible_values[i]) < dx then
        ax = possible_values[i] -- find closest approximation of x
        dx = abs(x - ax)
    end if
    if abs(y - possible_values[i]) < dy then
        ay = possible_values[i] -- find closest approximation of y
        dy = abs(y - ay)
    end if
end for

integer index
index = find({ax, ay}, -- find index of closest match
    {{0,1},{0.383,0.924},{0.707,0.707},{0.924,0.383}
    ,{1,0},{0.924,-0.383},{0.707,-0.707},{0.383,-0.924}
    ,{0,-1},{-0.383,-0.924},{-0.707,-0.707},{-0.924,-0.383}
    ,{-1,0},{-0.924,0.383},{-0.707,0.707},{-0.383,0.924}})


new topic     » goto parent     » topic index » view message » categorize

8. Re: trig

looking further into my old textbook. or search for "polar coordinates"
did read your second post.

right now you are wanting to compare apples and oranges.
you have to convert to the same means of measurement.
i would pick angles, myself.

equations i sent to you were for a 3d calculation. sphere to radians.
for a 2 d conversion of rectangular co-ordinates to Polar co-ordinates:
x = r cos A,               
y = r sin A
r = sqrroot(x^2 = y^2) ,   
A = arctan(y/x)
sin A = y / (sqrroot(x^2+y^2)),
cos A = x / sqrroot(x^2+y^2)

converting to angles requires only the 4th equation. use positive values!
as you already know x,y of the point to be determined.
looking at only the first 4 entrys in your table of angles, you can now compare
apples to apples and determine which of the first 4 sectors your point falls in.
to determine which of the 4 quadrants requires looking at the sign. a table of
the 4 quadrants:
((1,1),(1,-1),(-1,-1),(-1,1))
with the equations you determine the part of the quadrant, with the sign you
determine which quadrant. remember to rotate 90deg.
ie: 1st sector is equal to the 5th,9th,and 13th sectors, except for the sign.
without pictures, and not being a math teacher , i can not do much else.
once the angle is determined you can use a table of angles to do a lookup of the
angle to find the quadrant number.


rudy
lotterywars

new topic     » goto parent     » topic index » view message » categorize

9. Re: trig

Try this function:


function angle(object x, object y)
    if x = 0 then
        return (y > 0) * 180
    end if
    return arctan(y/x) * 180/PI + 90 + (x < 0) * 180
end function




L> Howdy folks,

L> It seems I always forget my trig when I need it.
L> Quick question:

L> How do I calculate an angle in degrees given an x,y vector.
L> The vector for 0 deg being {0,-1}
L> 90 deg would be {1,0}; 180 = {0,1}; etc...

L> atom angle
L> sequence vector
L> vector = {-0.38,-0.92}
L> angle = ?

L> thanks for any help,
L> Lewis Townsend

L> BTW is there a website that has all the math formulas, trig esp?

new topic     » goto parent     » topic index » view message » categorize

10. Re: trig

On 20 Apr 2004, at 2:49, aku saya wrote:

> 
> 
> Try this function:
> 
> 
> function angle(object x, object y)
>     if x = 0 then
>         return (y > 0) * 180
>     end if
>     return arctan(y/x) * 180/PI + 90 + (x < 0) * 180
> end function

Aku, could you do me a favor, and put some (parentheses) around the last 
return there, to better see what operation is performed when?

Kat

new topic     » goto parent     » topic index » view message » categorize

11. Re: trig

A suggestion...

sqrt() functions take time. Not much, but if you can eliminate them, all the 
better. Rather than finding the normal vector all the time, build your 
pre-defined angles list with vectors that have the larger component set to 1 
or -1, and the other scaled accordingly. For example, 0 degrees would be 
[1,0], 45 degrees would be [1,1], 90 degrees would be [0,1] and 180 degrees 
would be [-1,0]. That way, to scale your vector down to fit the standard, 
divide both by the larger value. For example: [112.2,40] would become [1, 
0.356...]

To find which angle is closest, do a binary search on your index of values, 
as others have said.


But this normalising method is much faster than using sqrts.

MrTrick

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu