Help: Rotation algorithm 
Author Message
 Help: Rotation algorithm

Quote:

> Can anyone please help me with this? I need to
> speed up this Pascal function:

> function Rotate( x, y: Integer; DegAngle: Real ): TPoint;
> var
>    Angle: Real;
> begin
>      Angle := DegToRad( DegAngle );
>      Result.X := Round(X * Cos(Angle) - Y * Sin(Angle));
>      Result.Y := Round(X * Sin(Angle) + Y * Cos(Angle));
> end;

> I use this function to rotate some lines, but it
> is slowing my whole program down. Does anyone know
> a better algorithm?

You waste a lot of time by computing Cos and Sin twice each.
Why not compute them once, saving their results for re-use?

Are you rotating more than one (x,y) point by the same angle?
If so, then consider pre-computing Cos(Angle) and Sin(Angle) once
and storing them for repeated use.

     - Rich



Wed, 18 Jun 1902 08:00:00 GMT  
 Help: Rotation algorithm

Quote:

> Can anyone please help me with this? I need to
> speed up this Pascal function:
> function Rotate( x, y: Integer; DegAngle: Real ): TPoint;
> var
>    Angle: Real;
> begin
>      Angle := DegToRad( DegAngle );
>      Result.X := Round(X * Cos(Angle) - Y * Sin(Angle));
>      Result.Y := Round(X * Sin(Angle) + Y * Cos(Angle));
> end;
> I use this function to rotate some lines, but it
> is slowing my whole program down. Does anyone know
> a better algorithm?

1) Precalculate sin and cos if you want to call the procedure several
times with the same angle.

procedure Rotate( x, y: Integer; s,c: Real ): TPoint;
begin
  Result.X := Round(c*X - s*Y);
  Result.Y := Round(s*X + c*Y);
end;

Angle := DegToRad( DegAngle );
s:=sin(Angle); c:=cos(Angle;

Rotate(x,y,s,c);
,,, many times for different (x,y)

or still better, dont use a procedure at all and write these two lines
directly instead of the call.

And you never rotate (straight) lines. You just rotate the end points
and draw the new line using the new end points.

And you never would use the data type REAL but {$N+} and the data type
SINGLE or DOUBLE or EXTENDED because they are faster than REAL -
assuming that the program will run on a machine with coprocessor.

Regards
Horst



Wed, 18 Jun 1902 08:00:00 GMT  
 Help: Rotation algorithm

Quote:

> Hi,

> Can anyone please help me with this? I need to
> speed up this Pascal function:

> function Rotate( x, y: Integer; DegAngle: Real ): TPoint;
> var
>    Angle: Real;
> begin
>      Angle := DegToRad( DegAngle );
>      Result.X := Round(X * Cos(Angle) - Y * Sin(Angle));
>      Result.Y := Round(X * Sin(Angle) + Y * Cos(Angle));
> end;

> I use this function to rotate some lines, but it
> is slowing my whole program down. Does anyone know
> a better algorithm?

Yes:

1. Don't recalculate Ange from DegAngle for each point.
   Calculate it once, and pass that to the function.

2. Go one step further, and pre-calculate the sine/cosine.

3. passing TPoint as var-Parameter instead of returning
   it as function return may save one copy. I'm not sure
   about that one, though.

4. Do {$N+} and use double - the Coprocessor/FPU is much
   faster than the real type math routines.

5. It might be a good idea to store your point data as
   real (or double) anyway, due to accumulating errors
   for subsequent calculations. According to speed, you
   would save the rounding of each coordinate, as well
   as converting the integers to reals (occurs twice for
   each coordinate in your code). The speed advantage
   of this may vanish if doing step 4 (the coprocessor/FPU
   has opcodes to directly store to integer), though I'm
   not sure. However, the numeric advantage remains.

6. If you do more than one transformation on your data,
   it may be of advantage to "collect" those transformations
   into a single one, and to apply only that to your points.



Wed, 18 Jun 1902 08:00:00 GMT  
 Help: Rotation algorithm


Quote:

>>      Angle := DegToRad( DegAngle );
>>      Result.X := Round(X * Cos(Angle) - Y * Sin(Angle));
>>      Result.Y := Round(X * Sin(Angle) + Y * Cos(Angle));
>You waste a lot of time by computing Cos and Sin twice each.
>Why not compute them once, saving their results for re-use?

IIRC, one *may* waste time by calling Sin & Cos individually, if the
machine is not ancient - consider :
        http://www.merlyn.demon.co.uk/programs/fsincos.pas

--

  Web <URL: http://www.merlyn.demon.co.uk/> - FAQqish topics, acronyms & links.
  PAS, EXE in <URL: http://www.merlyn.demon.co.uk/programs/> - see 00index.txt.
  Do not Mail News to me.    Before a reply, quote with ">" or "> " (SoRFC1036)



Wed, 18 Jun 1902 08:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. help: 3d rotations

2. I need help!!! 3d Rotation is not working

3. algorithm...help please

4. Help! sorting algorithm

5. vector rotation unit needed

6. Please help on algorithm

7. Rotation

8. HELP:an algorithm is getting on my nerves...

9. please help me on Djikstra's Algorithm

10. circular rotation

11. 2d rotation

12. rotation (pascal)

 

 
Powered by phpBB® Forum Software