Turing Programming: Solve Cos Inverse Function

  • Thread starter Thread starter eax
  • Start date Start date
  • Tags Tags
    Cos Inverse
AI Thread Summary
The discussion revolves around programming in Turing and the challenge of calculating the inverse cosine function, as Turing lacks built-in support for it. Participants suggest creating a lookup table for arccos values to improve efficiency, as well as exploring the Taylor series for inverse cosine. Some users propose iterative methods to approximate the angle by comparing known cosine values within specified ranges. The conversation highlights the limitations of Turing as a learning language compared to more robust options like C++. Overall, the thread emphasizes the need for alternative approaches to solve the inverse cosine problem in Turing programming.
eax
Messages
61
Reaction score
0
Hi,

I'm programming a game and need a character to turn facing another character. I got far but the problem is the language(Turing) Iam provided with doesn't support a cos/sin inverse function but has sin/cos.

Anyone know the formula? I have cos(x) how can I get x? Searching on google is not help all I get is what cos (x) = to relative to sin/tan.

Thanks in advance!
 
Physics news on Phys.org
Hi
I am not sure about your question but I will try my best to anwser it...
Cosine(x)= adj/hyp.
cos(x)^-1= Ratio of the adjecent arm to the ratio of the hypotines.

Or in terms of the unit circle...
Cos(x) is equal to x/r
so...
Cos(x)^-1= Ratio of x/r. THis solves for the angle of x.

Ok and finally...
cot(x)=cos(x)/sin(x)
the inverse of Cos(x) is sec(x) ie. sec(x)=r/x
the inverse of Sin(x) is csc(x) ie. csc(x)=r/y
I hope that helped...
The ratio of sin(x)/
 
This is what I have for mouse to character interaction, instead of character(enemy) to character(player)

Code:
set x/y to equal mouse position x/y
x := mousex
y := mousey
drawx/yoff are < 0 but we want to add them to x/y thus
subtracting adds them to x/y
x -= drawxoff
y -= drawyoff


bound.x/y are the coordinates for the character(player)
subtracting x/y by bound.x/y gives the x/y position reveloving
bound.x/y so (0,0) = (bound.x, bound.y)
y -= bound.y
x -= bound.x
get distance
dist := sqrt (x * x + y * y)
if dist < 1 then
    return
end if

I added 90 for a reason but it doesn't matter
pangle += 90
this next function just makes sure pangle is >= 0 and <= 360
rapanglei (pangle)

cos_t is a table of cos values because calculating cos everytime takes
lots of CPU time

calculating dotproduct (I think that's what it is called)
(and I like to reuse variables to save memory :))

We are treating these as vectors. bound.r = radius of character(player)
multiplying by cos(angle) gives us the "vector" its facing, then calculating
dot product 
x := (x * cos_t (pangle) * bound.r + y * sin_t (pangle) * bound.r) / (bound.r * dist)
puting pangle back to original value
pangle -= 90

and now x = cos(angle I want)
angle I want = cos inverse(x)

There is no cos/sin inverse function in this language. How can I get "angle I want"?
I hope my question is more clear now

Thanks in advance!
 
Maybe you could make an arccos() look-up table? I don't know of any mathematical way to transfer cos(x) to x without arccos(). A look-up table would probably be faster anyways. You'd just need to make a function that finds the closest related value.

By the way, I've never seen the language Turing, but it kind of looks like Pascal...
 
Im not even sure if http://www.holtsoft.com/turing/support/#currentversion is a language. Its more of an interpreter, I don't know why my school starts of with Turing then C++ is next year(or cemester :)). It would be better to start with C/ASM (in 16bit(DOS) mode)

Someone has to know. My math teacher said that before there were calculators people new the formula's for cos/sin and arccos/sin. If everyone forgot then everyone just copy's and pastes code into calculators and every where else :D.
 
Last edited by a moderator:
I'm pretty sure there's a Taylor series for all the trigonometric functions, but I can't seem to find the one for arccos(). Try looking up on Google the Taylor series for inverse cosine, and see if you can find it.

Learning an interpreted language before a compiled one? That's quite odd.
 
nolachrymose said:
Try looking up on Google the Taylor series for inverse cosine, and see if you can find it.d.

You could use the derivative of cos^-1 x and work out it's Taylor series, then integrate term by term.
 
Hi,
I am not an anvanced programmer, but anyway, I suggest this:
Since you know cos(x) then you know it's positive/negative/0.

If cos(x) = 0 then
x = 90
EndIf

If cos(x) < 0 then
For s = 91 to 180
...
(You can find the difference between cos(x) and cos(s). When the cos(s) - cos(x) < 0, stop the loop and you will get the s).
Next s
EndIf

If cos(x) > 0 then
For s = 1 to 90
...
(You can find the difference between cos(x) and cos(s). When the cos(s) - cos(x) > 0, stop the loop and you will get the s).
Next s
EndIf

And you will get cos(s) approximetely equal to cos(x)... But it's not the best way, though,...
Hope this help :-)
Bye bye,
Viet Dao,
 
Thank you :)
 
Back
Top