Need Help With Sin, Cos, and Tan

BlitzMax Forums/BlitzMax Beginners Area/Need Help With Sin, Cos, and Tan

Could anyone help/show where to find help with this? I can easily find what they are, but I've been having trouble finding out what I can use them for and how.

www.google.com

Search "computer trigonometry tutorial" or similar

http://www.gamespp.com/cgi-bin/resource.cgi?mathTutorials_trig1 for example @ http://www.gamespp.com/

The are generaly used in 2d to find out how far away somthing is. Or to add forces together and find the x or Y component

You can use a combination of Cos and Sin to turn an angle and a radius into pixel coordinates.

e.g.

Graphics 640,480,0
For Local Angle:Int=0 to 359 step 3
        Plot 320+(100*Cos(Angle)),240+(70*Sin(Angle))
Next

will draw an oval of dots, 3 degrees apart. The 100* and 70* scale the values up because normally you get something in the range of 0 to 1, so to turn it into screen coords you multiply it by the pixel width or height and hey presto.

see if this example can help you is not well documented, but it might help you get the idea when you run it.
Graphics 800,600

Local x# = 0
Local y# = 0
Local angle% = 0
Local radius# =200.0
Local offsetx% = GraphicsWidth() / 2
Local offsety% = GraphicsHeight() / 2

Repeat
	Cls
	SetColor 255,255,255
	DrawText "Angle: "+ angle,10,10
	DrawText "use left & right to change radius",10,45
	DrawText "use up & down to change angle",10,60
	If KeyDown(key_up) angle = (angle+1)Mod 360
	If KeyDown(key_down) angle = (angle-1) Mod 360
	If KeyDown(key_left) And radius >1.0  radius :-1.0
	If KeyDown(key_right) And radius < 280.0 radius :+ 1.0 
	x = Cos(angle)
	y = Sin(angle)
	SetColor 255,0,0
	DrawLine(offsetx,offsety,offsetx+x*radius,offsety+y*radius)
	SetRotation(angle)
	DrawText "radius ="+radius,offsetx,offsety
	SetRotation(0)
	SetColor 0,255,0
	DrawText "x=cos("+angle+")*radius="+x*radius,offsetx,offsety+(y*radius)
	DrawLine(offsetx+x*radius,offsety+y*radius,offsetx,offsety+y*radius)
	DrawText "x/r = cos(Angle) ="+x,10,20
	SetColor 0,0,255
	DrawLine(offsetx+x*radius,offsety+y*radius,offsetx+x*radius,offsety)
	DrawText "y=sin("+angle+")*radius="+y*radius,offsetx+(x*radius)+5,offsety'+(y*radius)+10
	DrawText "y/r = sin(Angle) = "+y, 10,32
	Flip()
Until KeyDown(key_escape)



Here's a little "analogue clock" example I threw together...
SuperStrict

' analogue clock example by BaH, showing use of Cos and Sin

Graphics 200, 200, 0

Local radius:Int = 98

' set the origin to the clock center
SetOrigin 100, 100

While Not KeyDown(KEY_ESCAPE)

	SetColor 255, 255, 255
	
	Local time:String = CurrentTime()
	Local hours:Int = time[..2].toInt()
	Local minutes:Int = time[3..5].toInt()
	Local seconds:Int = time[6..].toInt()

	Cls
	
	' draw face
	For Local deg:Int = 0 Until 359
		Plot Sin(deg) * radius, -Cos(deg) * radius
	Next
	
	' draw hour segments
	For Local deg:Int = 0 Until 359 Step 30
		DrawLine Sin(deg) * radius, -Cos(deg) * radius, Sin(deg) * (radius - 10), -Cos(deg) * (radius - 10)
	Next
	
	' draw hour hand - 30 degrees per tick
	Local deg:Int = (hours Mod 12) * 30
	DrawLine 0, 0, Sin(deg) * (radius/2), -Cos(deg) * (radius/2)

	' draw minute hand - 6 degrees per tick
	deg = (minutes Mod 60) * 6
	DrawLine 0, 0, Sin(deg) * (radius * 0.8), -Cos(deg) * (radius * 0.8)

	' draw second hand - 6 degrees per tick
	deg = (seconds Mod 60) * 6
	SetColor 255, 0, 0
	DrawLine 0, 0, Sin(deg) * (radius*0.75), -Cos(deg) * (radius*0.75)

	Flip

Wend


Hope it helps.

Brucey: you've written "cock"

Sin Cos and Tan are useful for lots of things.

Using Sin and Cos, you can plot points using polar coordinates. Specify and angle and a distance from the origin, and the equation will spit out a regular X and Y coordinate for you to use.

And SohCahToa tells you that if you have a right triangle and you know the length of the side opposite a particular corner, and the length of the triangle's hypotenuse, (the side opposite the triangle's 90 degree angle) you can calculate the sine. Or if you have the adjacent side (the one touching the corner which is not also the hypoteneues) you can calculate the cosine of that angle.

And if you know the sine or cosine of an angle, you can convert that to an angle in degrees.

Going back to the polar coordinate example, sine and cosine are used when plotting circles. If you imagine a point orbiting the center of your graph, and consider how it moves on just the Y axis, you will find it accelerates as it approaches the middle and slows down near the edges. That's because Y = Radius * Sin(Angle), and as the point goes around the angle changes, and the sine has that acceleration built in. Once you understand how that works, you can use it to do things like make objects in your game move across the screen in a sine wave, or accelerate when moving from one point to another.

The polar coordinate equations I was talking about are these:

X = Radius * Cos(Angle)
Y = Radius * Sin(Angle)

Those will give you numbers ranging from -Radius to +Radius on each axis, becayse Cos and Sin vary from -1 to 1 as you vary the angle.

Anyway that should give you a jump start on what you can do with Sin and Cos. As for what you can do with Tan... Tan isn't very useful, but ArcTan is. ArcTan is the inverse of Tan. Instead of passing it an angle, you pass it the length of the two sides of the right triangle. And you get the angle back. So you can pass ArcTan the X and Y coordinate of a point, and it will tell you the angle of that point relative to 0,0.

Brucey: you've written...

Heh... that's right folks... my mind's always in the gutter... :-)

Thanks! I'll be able to understand people's code snippets MUCH better now.