Could someone convert a little c maths code for me

Miscellaneous Forums/General Discussion/Could someone convert a little c maths code for me

switch(trans)
	{
		case 0:	for(i=1;i<nRows-1;i++)
					for(j=1;j<nCols-1;j++)
					{
						dis->SetDisplacement(j,i,cosf(t*10+(i+j)/2)*5,sinf(t*10+(i+j)/2)*5,HGEDISP_NODE);
					}
				break;

		case 1:	for(i=0;i<nRows;i++)
					for(j=1;j<nCols-1;j++)
					{
						dis->SetDisplacement(j,i,cosf(t*5+j/2)*15,0,HGEDISP_NODE);
						col=int((cosf(t*5+(i+j)/2)+1)*35);
						dis->SetColor(j,i,0xFF<<24 | col<<16 | col<<8 | col);
					}
				break;

		case 2:	for(i=0;i<nRows;i++)
					for(j=0;j<nCols;j++)
					{
						r=sqrtf(powf(j-(float)nCols/2,2)+powf(i-(float)nRows/2,2));
						a=r*cosf(t*2)*0.1f;
						dx=sinf(a)*(i*cellh-256)+cosf(a)*(j*cellw-256);
						dy=cosf(a)*(i*cellh-256)-sinf(a)*(j*cellw-256);
						dis->SetDisplacement(j,i,dx,dy,HGEDISP_CENTER);
						col=int((cos(r+t*4)+1)*40);
						dis->SetColor(j,i,0xFF<<24 | col<<16 | (col/2)<<8);
					}
					break;
	}



I know switch is synonymous with select, and I have a type to match the class. That's not the problem. I think i've just messed up converting the maths.

I have the following, but I think it's wrong :

'// Calculate New displacements And coloring For one of the three effects
	Select trans
	
		Case 0
			For i=1 To nRows-1
				For j=1 To nCols-1
				
					dis.SetDisplacement(j,i,Cos(t*10+(i+j)/2)*5,Sin(t*10+(i+j)/2)*5,HGEDISP_NODE)
					
				Next
			Next

		Case 1
			For i=0 To nRows
					For j=1 To nCols-1
					
						dis.SetDisplacement(j,i,Cos(t*5+j/2)*15,0,HGEDISP_NODE)
						col=Int((Cos(t*5+(i+j)/2)+1)*35)
						dis.SetColor(j,i,$FF Shl 24 | col Shl 16 | col Shl 8 | col)
						
					Next
			Next
			

		Case 2
			For i=0 To nRows
				For j=0 To nCols
					
					r=Sqr(((j-nCols/2.0)^2)+((i-nRows/2.0)^2))
					a=r*Cos(t*2)*0.1
					dx=Sin(a)*(i*cellh-256)+Cos(a)*(j*cellw-256)
					dy=Cos(a)*(i*cellh-256)-Sin(a)*(j*cellw-256)
					Dis.SetDisplacement(j,i,dx,dy,HGEDISP_CENTER)
					col=Int((Cos(r+t*4)+1)*40)
					dis.SetColor(j,i,$FF Shl 24 | col Shl 16 | (col/2) Shl 8)
						
				Next
			Next
			
	End Select



Wouldn't Sinf() and Cosf() be expecting radians?
Const RAD2DEG! = 180 / Pi


Function Cosf!(rad!)
  Return Cos(rad! * RAD2DEG!)
End Function

Function Sinf!(rad!)
  Return Sin(rad! * RAD2DEG!)
End Function
??

What is it doing wrong?

well for one thing you don't have a "break".

edit: is that a break in the for loop?

You need the break in a C Switch ...

Your FOR loop upper bounds are all off by one (hey, at least you were consistant!)

for(i=1;i<nRows-1;i++) means FOR i = 1 TO nRows-2, not nRows-1.

muk: break is how you end a case block in C. Without break, execution continues into the next case block.

Yeah that's what I was thinking too ... the for loops were off.

for(i=1;i<nRows-1;i++) means FOR i = 1 TO nRows-2, not nRows-1.
Or use Until: For i = 1 Until nRows-1.

Wouldn't Sinf() and Cosf() be expecting radians?
Yes it would.

Thanks guys. I haven't taken a break from reading/writing/converting c code in a few days and I think it's driven me a bit batchy. I'll make the necessary changes and see if that was the problem or if there's an error in my dll wrapper.

For i = 1 Until nRows-1


Strange syntax. Is that BlitzMax?

Ok, I've made the suggested changes and it's almost perfect. But there's one bit that's not working. Maybe it's my conversion or maybe it's in the dll wrapper.

C++
col=int((cos(r+t*4)+1)*40);
dis->SetColor(j,i,0xFF<<24 | col<<16 | (col/2)<<8);


BMax
col=Int((CosR(r+t*4)+1)*40)
dis.SetColor(j,i,$FF Shl 24 | col Shl 16 | (col/2) Shl 8)

Function CosR:Int(In:Int)
	Return Int(Cos(In/Pi*180))
End Function


Anything wrong in there?

CosR() should be returning a float.

Sorry that was just an oversight. It's not the problem though. Is everything else there correct?

Everything is apart from CosR... all of those Ints have to be float not just the return type.

Yep, that's what I thought. Must be a typo in the wrapper somewhere. Thanks for confirmation. I'm sure I'll find I've muddled column and color in the wrapper or something.

EDIT: On closer inspection, that's exactly what I had done. I used col for column and then col again for color. Oops. All fixed now anyway, so thanks to everyone.

This is my conversion.

Const RAD2DEG!=57.295779513082320876798154814105
Local i:Int, j:Int
Select trans
    Case 0
        Local p:Float
        For i = 1 To nRows-1
            For j = 1 To nCols-1
                p = (t*10+(i+j)*.5#)*RAD2DEG
                dis.SetDisplacement(j,i,Cos( p )*5,Sin( p )*5, HGEDISP_NODE)
            Next
        Next
    'break
    
    Case 1
        For i=0 To nRows
            For j=1 To nCols-1
                dis.SetDisplacement( j, i, Cos( (t*5 + j*.5#)*RAD2DEG ) * 15, 0, HGEDISP_NODE )
                col = Int( (Cos((t*5+(i+j)*.5)*RAD2DEG)+1)*35 )
                dis.SetColor( j,i, $FF000000 | col Shr 16 | col Shr 8 | col )
            Next
        Next
    'break
    
    Case 2
        For i=0 To nRows
            For j=0 To nCols
                Local pow1:Float = j-(nCols*.5#)
                pow1:*pow1
                Local pow2:Float = i-(nRows*.5#)
                pow2:*pow2
                r = Sqr( pow1+pow2 )
                a=(r*Cos( (t*2) * RAD2DEG )*.1#)*RAD2DEG
                dx = Sin(a)*(i*cellh-256) + Cos(a)*(j*cellw-256)
                dy = Cos(a)*(i*cellh-256) - Sin(a)*(j*cellw-256)
                dis.SetDisplacement( j, i, dx, dy, HGEDISP_CENTER )
                col = Int( ( Cos( (r+t*4)*RAD2DEG )+1 )*40 )
                dis.SetColor( j,i,$FF000000 | col Shl 16 | (col/2) Shl 8 )
            Next
        Next
    'break
End Select


@octothorpe - For...Until...Next

My all-time favourite flow control structure is Perl's unless.