Sine wave at an angle

Blitz3D Forums/Blitz3D Beginners Area/Sine wave at an angle

_PJ_(Posted 2016) [#1]
Yet again, trigonometry beats me.

In theory, this really shouldn't be difficult, but right now, my brain refuses to be able to figure it out.

What I want, essentially, is to be able to orient a sinusoidal wave at a given angle.

The essence of the current progress is as follows:

Function Meander(oX,oY,O,Length,Amplitude)
	Local Angle
	Local X#=oX
	Local Y#=oY
	
	Local YStepSize#=Float(Amplitude/360.0)
	Local XStepSize#=Float(Length/360.0)
	
	For Angle=0 To 360
		
		Y#=Y+Cos(Angle)*YStepSize
		X#=X+XStepSize
		
		Fluvial(X,Y)
		
	Next 
End Function


The parameter "O" is the angle (in degrees) through which the wave ought be rotated. As yet, since I can't figure out how to do it, it's unused in the above snippet.


TomToad(Posted 2016) [#2]
Is this what you are going for? Not the most efficient code here, but it works.



Floyd(Posted 2016) [#3]
Same basic idea but manually rotating point (x,y).

Graphics 800, 600, 0, 2

; point ( x, y ) rotated around (0,0) by angle R is ( x*Cos(R) - y*Sin(R), x*Sin(R) + y*Cos(R) )
; If that turns the wrong direction for your purposes then replace R with -R or, equivalently, replace Sin(R) with -Sin(R).
; That will make the rotated point ( x*Cos(R) + y*Sin(R), -x*Sin(R) + y*Cos(R) )


For R# = -40 To +40

	Cls
	Text  350, 350, Int(R)

	CR# = Cos(R)
	SR# = Sin(R)
	
	For a# = -180 To +180
	
		; (x,y) is original, unRotated point. It is (a,Sin(a)) "stretched" to better fit the screen.
		x# = 2*a 
		y# = 100*Sin(a)
		
		; now the Rotated version of (x,y)
		xR# = x*CR - y*SR
		yR# = x*SR + y*CR
			
		Plot 400 + x, 300 + y    ; Let's put the curve at (400,300)
		Color 255,255,0
		Plot 400 + xR, 300 + yR
		Color 255,255,255
		
	Next

	Delay 100
	Flip
	
Next

WaitKey

I wrote the code and posted it here before noticing this was Blitz3D. I had used BlitzMax. So I made the necessary changes to comments, DrawText etc.

That produced a baffling effect, displaying only about one in six R values. After considerable head scratching I swapped the Delay and Flip. It used to be

	Flip
	Delay 100

which was fine in BlitzMax. I don't know why the Blitz3D behavior is so different. DX7 vs. DX9?


_PJ_(Posted 2016) [#4]
Thanks so much, still not sure I can follow exactly what's happening but they both work!

Really appreciate it, thank you!

I think Tom's addition of:
		Rotate = ATan2(Y-cy,X-cx)+O
		dist = Sqr((cx-X)^2+(cy-Y)^2)
		px = Cos(Rotate)*dist+cx
		py = Sin(Rotate)*dist+cy

is simpler in terms of modifying my actual code and applies smoother resolution for the curves 'as-is'.

I needed to modify it, inparticular,
	Local cx# = oX;Length/2.0+X
	Local cy# = oY

Since otherwise, there was an unnecessary offset from the origin.

My next challenge is how to "predict" the endpoint, for purposes of continuing the 'river' with another meander with different property.

	;O=O-90 ;(Align to North=0, Clockwise)
	
	Local Angle
	Local X#=oX
	Local Y#=oY
	Local cx# = oX;Length/2.0+X
	Local cy# = oY
	Local Rotate#
	Local pX#
	Local pY#
	Local Dist#
	
	Local YStepSize#=Float(Amplitude/360.0)
	Local XStepSize#=Float(Length/360.0)
	
	For Angle=0 To 360
		
		Y#=Y+Cos(Angle)*YStepSize
		X#=X+XStepSize
		Rotate = ATan2(Y-oY,X-oX)+O
		Dist = Distance2D(X,Y,oX,oY)
		pX = Cos(Rotate)*Dist+oX
		pY = Sin(Rotate)*Dist+oY
		
		Color 255,255,255
		Fluvial(pX,pY)
		
	Next 
	
	Color 255,0,0
	Line oX,oY,oX+(Sin(O)*Length),oY+(Cos(O)*Length)


The red line ought to strike through the centre of the wave, but only lines up along the axis at 45 (and of course, 225) degrees.