# Board index » delphi » Fast SQR, SQRT, Cos and sin (help!)

## Fast SQR, SQRT, Cos and sin (help!)

I am working on a pattern recognition project. Since we have to run our code in real time, the speed of is our biggest restriction. I am now searching for a way how to
accelerate the crucial procedures. Most of the CPU time is used to `turn` points around an `origin point  (0,0). These points are never more than 200 pixels away from this origin and will only be turned once. I only need the (Word) x,y position in the 200*200 Array.
At the moment the procedure in question runs with the classic cos sin sqrt .... commands to calculate the new position of the point.

How can i accelerate the whole procedure ? Or is there a faster way to calculate sqrt for example ?

(I am working on a PC 586 66mhz, BP 7)

Thanks for every hint
---
Ulf Unbehaun                               EMail:ulf.unbeh...@unifr.ch
Institut d'Informatique                    Tel  :037 / 23 22 22
Universite de Fribourg                     Fax  :037 / 810 612
ch.du Musee 3 /1700 Fribourg /Switzerland  BBS  :810 611(analog)+810 613(ISDN)

## Re:Fast SQR, SQRT, Cos and sin (help!)

Look out for an archive called BPL70N16.ZIP, it contains fast replacement
routines for the BP Runtime Library. It should also contain very fast
routines for the trigonometric functions, and, especially the Sqrt
function. I hope, this helps you a little bit.
Regards,
Mathias.

## Re:Fast SQR, SQRT, Cos and sin (help!)

##### Quote
>I am working on a pattern recognition project. Since we have to run our code in
> real time, the speed of is our biggest restriction. I am now searching for a w
>ay how to
>accelerate the crucial procedures. Most of the CPU time is used to `turn` point
>s around an `origin point  (0,0). These points are never more than 200 pixels a
>way from this origin and will only be turned once. I only need the (Word) x,y p
>osition in the 200*200 Array.
>At the moment the procedure in question runs with the classic cos sin sqrt ....
> commands to calculate the new position of the point.

>How can i accelerate the whole procedure ? Or is there a faster way to calculat
>e sqrt for example ?

if all of the number you are using are integers, then make a lookup table.

type
Ptable=^table_type;
table_type=array[0..numelements] of real;

......
new(cos_table);
for n:=0 to numelements do
cos_table^[n]:=cos(n);
.....
writeln('Cosine of ',n,'= ',cos_table^[n]:0:4);
.....
dispose(cos_table);

hope that helps.

Rob Wagner
ewag...@tiger.lsu.edu

## Re:Fast SQR, SQRT, Cos and sin (help!)

##### Quote
Unbehaun Ulf (unbeh...@ufps8.unifr.ch) wrote:

: I am working on a pattern recognition project. Since we have to run our code in real time, the speed of is our biggest restriction. I am now searching for a way how to
: accelerate the crucial procedures. Most of the CPU time is used to `turn` points around an `origin point  (0,0). These points are never more than 200 pixels away from this origin and will only be turned once. I only need the (Word) x,y position in the 200*200 Array.
: At the moment the procedure in question runs with the classic cos sin sqrt .... commands to calculate the new position of the point.

: How can i accelerate the whole procedure ? Or is there a faster way to calculate sqrt for example ?

: (I am working on a PC 586 66mhz, BP 7)

: Thanks for every hint

I'll take a shot at this.

I'm assuming that your data is binary (two color) and that you accept some
error in the position of the target (it may not fall squarely on a pixel).

-- Lookup table approach --

Precalculate the sine cosine of each angle that you need to rotate and
precalculate the sqrt of each distance you need to map.  Use these lookup
tables during the rotation.

-- Shear plane approach --

I'm a little hazy on this approach, but I remember reading about it in the
book "Fundumentals of Computer Graphics" by Foley, VanDam & <someone>.
Basically, what you do is perform two "shear" operations.  This results
in a rotation but does not require the transcendental functions.

First Shear Operation

+----------+         +----------+
|          |        /          /
|          |       /          /
|          |      /          /
+----------+     +----------+

I tried, but cannot draw the second operation.  It shears in the vertical
direction and the result is a rotated rectangle.  I'm probably leaving
out some critical step but the book describes the process.

I hope that this helps,

Erik Turner
etur...@ccd.harris.com