Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

[VECTORS] Rotation without trigonometry

Name: Anonymous 2010-07-03 9:31

How is it done?
Normally, rotating a vector A(x1, y1) through t radians in order to get vector B(x2, y2) is done with
x2 = x1*cos(t) - y1*sin(t),
y2 = x1*sin(t) + y1*cos(t).

But trigonometry is expensive, especially without an FPU, so there must be another way.
I was thinking of something similar to Bresenham's circle algorithm, or perhaps reflecting the vector about the origin or something.

So, uh, how would you do it?

Name: Anonymous 2010-07-03 9:38

Tables for sin(t) and cos(t).

Name: Anonymous 2010-07-03 9:40

Lookup tables for trigonometric functions? I think that's how it was done on the GBA.

Name: Anonymous 2010-07-03 9:40

Store vector as angle and length. Use sin and cos tables for conversion to (x,y)

Name: Anonymous 2010-07-03 9:42

In before Xarn.

Name: Anonymous 2010-07-03 9:54

So the consensus is a LUT. Not as elegant as I'd like (I was wondering if there was an alternative when I posted), but very well, thank you all for participating.

Name: Anonymous 2010-07-03 10:01

Name: Anonymous 2010-07-03 10:01

LUTs are the apex of elegance.

Name: Anonymous 2010-07-03 10:05

>>6
LUTs are extremely elegant. You also won't find anything faster. Two multiplies and two adds for precision you could peel an apple with.

Name: Anonymous 2010-07-03 10:15

>>6
>>9
This pretty much. You can linearly interpolate on a relatively small LUT and get great accuracy for very few cycles. Note that you don't want your LUT to be too large; it's bad for your cache. Besides, do you really care about accuracy that much?

Also just to clarify, LUT is generally used for trig, and trig is used to generate a rotation matrix. So lets say you've got 100 such vectors to rotate, you're best off making an affine transformation matrix and multiplying them through.

>>1
without an FPU
What platform are you coding for?

Name: Anonymous 2010-07-03 11:16

If you don't have an FPU, and you're going to end up on lattice points, you may want to look into continued fractions, which rely pretty much exclusively on integers.

Name: Anonymous 2010-07-03 11:17

>>11
It's in SICP

Name: Anonymous 2010-07-03 11:19

>>12
It's funny 'cuz it's true!

Name: Anonymous 2010-07-03 11:56

>>11
Can you elaborate on that a little, as well as explain what lattice points are?
If you don't mind, that is.

Name: Anonymous 2010-07-03 12:08

I mind.

Name: Anonymous 2010-07-03 12:41

CORDIC you foos

Name: Anonymous 2010-07-03 12:43

>>15
Filthy peasant.

Name: Anonymous 2010-07-03 13:15

>>17
Off the top of my head I can't determine whether it is cheaper to flub FP math or do a bit of continued fraction arithmetic, and I'm not going to investigate. It is probably cheaper to flub FP, since CF arithmetic is basically matrix multiplication.

Name: Anonymous 2010-07-03 16:34

ITT No one read SICP.
Maybe use a different representation for the vector: length, angle and point of origin. Converting to Cartesian coordinates is cheaper then rotating in the Cartesian coordinate system.

Name: Anonymous 2010-07-03 16:39

>>19
See >>4.

>>18
Hm, what I meant was that I'm quite interested in what lattice points are and how you'd do arithmetic using continued fractions; however, I'm not interested enough to research myself (as I'm quite busy at the moment) and it would be absolutely terrific if I could read a post written by a fellow /prog/rider on this matter. My reaction in >>17 was due to my disappointment that you would refuse my polite request (>>14); however you do have the right to do so and what I said in >>17 was quite ungentlemanly, for which I'm sorry.

Name: Anonymous 2010-07-03 16:41

>>19
And then what?

Name: Anonymous 2010-07-03 16:45

>>21
Rotation and multiplication by a scalar: one instruction, without a loss of precision.

Name: Anonymous 2010-07-03 16:45

>>19
ITT people only read the last 5 posts before commenting

Name: Anonymous 2010-07-03 16:50

>>19
Maybe use a different representation for the vector: length, angle and point of origin.
I think you need to read SICP again.

Name: Anonymous 2010-07-03 16:52

>>23
>>4 suggested a sin and cos of the angle would be needed for conversion but you can do away with a cos of the angle and its complementary.
The definition of the dot product here is your friend.

Name: Anonymous 2010-07-03 16:55

Sure is lack of reading the OP in here.

Name: Anonymous 2010-07-03 16:56

>>20
Well, we have a weird situation, the, because if you can't be bothered to research it, I can't be bothered to explain.

Name: Anonymous 2010-07-03 17:29

>>27
:<

Name: Anonymous 2010-07-03 19:39

>>28
Just man up and read the literature. It will be more fun than whatever you're supposed to be doing. ت

Name: Anonymous 2010-07-03 22:27

Alright damn it, I was interested so I did a little legwork.

An approximation of ez, z ∈ C, |z| < 1, is
z(z + 4) + 6/6 - 2z

Integers only:

See
http://www.wolframalpha.com/input/?i=(36*(a%2Fb)*i+-+2*i*(a%2Fb)^3+-+14*(a%2Fb)^2+%2B+36)+%2F+(36+%2B+4*(a%2Fb)^2)

Look below at "assuming a and b are real". This is the above expression where
z = ai/b.
It is good for about three digits, x.yz, which you don't have if you don't have an FPU, so... yeah.

Long story short: either use an FP library and a lookup table or bust. There's a reason they've been in use forever.

Name: Anonymous 2010-07-04 4:34

FP library
Could you stop saying ``FP'' like it differentiates floating point from fixed point? It doesn't.

PS: fixed point math is quite fast when the alternative is soft floats (which gcc will use if it knows you don't have an FPU.)

Name: Anonymous 2010-07-04 13:49

>>31
Are you aware that conversations, threads, actions, and words have contexts? It's true!

Name: Anonymous 2010-07-04 14:58

>>32
I don't recall a distinction being established in this thread. Was it the last time you said ``FP'' without clarification? Or the time before that?

Name: Anonymous 2010-07-04 22:17

>>33
More like the context of the thread was determined by the OP who specifically mentioned he did not have hardware floating point available.

I know it was 31 posts ago, but sheesh.

Name: Anonymous 2010-07-05 5:07

>>34
And yet he didn't mention what platform he's working on.

Given the context of the question, the only thing I can guess is that he's working on Android. He knows some devices don't have FPUs, and he thinks his game will be so awesome that softfloats won't cut it for him.

Name: Anonymous 2010-07-05 10:28

Quaternions.

Name: Anonymous 2010-07-05 13:55

>>34
``FP'' still doesn't help distinguish between fixed and floating point. The lack of an FPU just means your floats are soft.

>>35
Personally I wouldn't use soft floats. Fixed point math is not really a challenge.

Name: Anonymous 2010-07-06 0:51

>>37
There's nothing to distinguish. Or is there some obscure platform that only has floating point in hardware, and you need a fixed point library?

Don't change these.
Name: Email:
Entire Thread Thread List