• Welcome to Dark Bit Factory & Gravity.
 

[C++] Conversion Blues

Started by Clyde, April 29, 2010

Previous topic - Next topic

0 Members and 0 Guests are viewing this topic.

Clyde

Been racking my brains in order to get the conversion right in C++ 2010, from blitz which for sin and cos returns a degrees (PI/180.0)
Each time I try, it gets further from the original. hehe.

And I'd be thrilled to see it sorted. :D
Cheers!

b3d code:
% = integer
# = float

h2=h/2
w2=w/2


for y=0 to h-1
for x=0 to w-1
col1=128+128* ( Sin(Sqr((x%-w2%)*(x%-w2%)+(y%-h2%)*(y%-h2%))*1.23#) )
next
next


my lastest conversion attempt in c++ 2010

c1=(float) (128.0f+128.0f) * ( sinf( sqrt(float( (x-w2)*(x-w2)+(y-h2)*(yy-h2))*1.23)));//*DEGREES);
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.


Challenge Trophies Won:

hellfire

Just multiply the value you put *into* sinf by PI/180.0.
Challenge Trophies Won:

Jim

could also do something like

float mysin(float degrees)
{
  float radians = degrees / 180.0f * 3.141f;
  return sinf(radians);
}

and use that instead.

Jim
Challenge Trophies Won:

Clyde

Another thing I get stuck on alot is switching from blitz,freebasic and cpp.

this is from blitz2d/3d

attan2(x,y)=(atan2(y-height2,x-wwidth2))
square(x,y)= sqr ((x-wwidth2)^2+(y-height2)^2)


and this in cpp, but there's something needed for it to be the same as in the blitz / fb one above. I was hoping you might know?

attan2[x][y]=(atan2f(yy-height2,xx-wwidth2));
square[x][y]=sqrtf( ( (xx-wwidth2)*(xx-wwidth2) )+ ( (yy-height2)*(yy-height2) )  );


Cheers and thanks,
Clyde.
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.


Challenge Trophies Won:

Jim

#4
That looks fine, what's the problem?
You probably need
#include <math.h>
or similar, is all.

<edit>
The other thing you need to know is that C++ is a strongly-typed language, whereas BASIC, including Blitz, and C are weakly-typed.
What do this mean?
It means that you need to *tell* the compiler the types of things instead of the compiler using some other built-in rule set.
So, if you go:
int a=5;
double b = sqrtf(a);
It might not work for you.
In BASIC, it would automatically make 'a' into a float, and then make the float result into a double.
C would do something similar, but different.
C++ doesn't.  It will say that there is no implementation of sqrtf that takes an int parameter and returns a double.
You would need to write
double b = (double)sqrtf((float)a);

Jim
Challenge Trophies Won:

Pixel_Outlaw

#5
I've also found templating functions a cool way to accept numbers of various classes. To some extent it helps overcome the strict constraints of C++. A templated function is a function that can take a variety of different parameter types in the same slot. Perhaps you are writing a trigonometric coversion function that can take any type of number, this could be done through overloading the function but you can do it with a single templated function.

Again, I'm still learning C++ up at the college but this is one very cool aspect (that can be abused to some extent).

Here is an example. This example shows how the author has used a template in place of the two normal add functions, one taking an int and one taking a float. You can see that a template allows the programmer to have only one function that does the work of many specific functions.

http://www.codersource.net/c/c-tutorial-on-templates/c-templates-function-templates.aspx
Challenge Trophies Won:

Jim

Operator overloading is the work of the devil.

Jim
Challenge Trophies Won: