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.