Author Topic: [C++] Conversion Blues  (Read 4391 times)

0 Members and 1 Guest are viewing this topic.

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
[C++] Conversion Blues
« on: April 29, 2010 »
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
 
Code: [Select]

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
Code: [Select]
c1=(float) (128.0f+128.0f) * ( sinf( sqrt(float( (x-w2)*(x-w2)+(y-h2)*(yy-h2))*1.23)));//*DEGREES);
« Last Edit: April 29, 2010 by Clyde »
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.

Challenge Trophies Won:

Offline hellfire

  • Sponsor
  • Pentium
  • *******
  • Posts: 1294
  • Karma: 466
    • View Profile
    • my stuff
Re: [C++] Conversion Blues
« Reply #1 on: April 29, 2010 »
Just multiply the value you put *into* sinf by PI/180.0.
Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: [C++] Conversion Blues
« Reply #2 on: April 29, 2010 »
could also do something like
Code: [Select]
float mysin(float degrees)
{
  float radians = degrees / 180.0f * 3.141f;
  return sinf(radians);
}
and use that instead.

Jim
Challenge Trophies Won:

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
Re: [C++] Conversion Blues
« Reply #3 on: September 25, 2010 »
Another thing I get stuck on alot is switching from blitz,freebasic and cpp.

this is from blitz2d/3d
Code: [Select]
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?
Code: [Select]
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:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: [C++] Conversion Blues
« Reply #4 on: September 25, 2010 »
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
« Last Edit: September 25, 2010 by Jim »
Challenge Trophies Won:

Offline Pixel_Outlaw

  • Pentium
  • *****
  • Posts: 1382
  • Karma: 83
    • View Profile
Re: [C++] Conversion Blues
« Reply #5 on: November 24, 2010 »
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
« Last Edit: November 24, 2010 by Pixel_Outlaw »
Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: [C++] Conversion Blues
« Reply #6 on: November 24, 2010 »
Operator overloading is the work of the devil.

Jim
Challenge Trophies Won: