Can anyone Draw an Arrow

BlitzMax Forums/BlitzMax Programming/Can anyone Draw an Arrow

To visulize a vector I want to draw a line. Does anyone have a neat function for this?

try this wave
Graphics 800, 600, 0, 60

While Not KeyHit(KEY_ESCAPE)

	Cls
	
	SetColor 0, 255, 0
	DrawArrow 400, 300, MouseX(), MouseY()
	SetColor 255, 0, 0
	DrawArrow MouseX(), MouseY(), 400, 10, 10, 20
	SetColor 0, 0, 255
	DrawArrow 400, 10, 400, 300, 60, 10
	
	Flip

Wend

EndGraphics

End


Function DrawArrow(x1%, y1%, x2%, y2%, _arrowHeadLength% = 20, _arrowHeadWidth% = 10)

	DrawLine x1, y1, x2, y2
	
	Local lineAngle% = ATan2(x1 - x2, y1 - y2)
	
	lineAngle:+ 180
	DrawLine x2, y2, (x2 + (Sin(lineAngle * -1) * _arrowHeadLength%)) + (Cos(lineAngle) * _arrowHeadWidth%), (y2 - (Cos(lineAngle * -1) * _arrowHeadLength%)) - (Sin(lineAngle) * _arrowHeadWidth%)
	DrawLine x2, y2, (x2 + (Sin(lineAngle * -1) * _arrowHeadLength%)) - (Cos(lineAngle) * _arrowHeadWidth%), (y2 - (Cos(lineAngle * -1) * _arrowHeadLength%)) + (Sin(lineAngle) * _arrowHeadWidth%)
	
End Function


EDIT: got bord and made a better example (could make a game from this ......)
Graphics 800, 600, 0, 60

Type GravBox

	Field x#, y#
	Field xspeed#, yspeed#
	
	Function Create:GravBox(_x#, _y#)
	
		Local tempGravBox:GravBox = New GravBox
		
		tempGravBox.x# = _x#
		tempGravBox.y# = _y#
		
		Return tempGravBox
	
	End Function
	
	Method Update()
	
		UpdateMovement()
		
		xspeed:* .99
		yspeed:+ .05
		
		If y > GraphicsHeight() - 20 Then 
			yspeed = -yspeed / 1.05
			UpdateMovement()
		EndIf
		If y < 0 Then 
			yspeed = yspeed * -1
			UpdateMovement()
		EndIf
		If x < 0 Then 
			xspeed:* -1
			UpdateMovement()
		EndIf
		If x > GraphicsWidth() Then 
			xspeed:* -1
			UpdateMovement()
		EndIf
	
	End Method
	
	Method UpdateMovement()
		x:+ xspeed
		y:+ yspeed
	End Method

End Type

Global myGravBox:GravBox = GravBox.Create(GraphicsWidth() / 2, 20)

While not KeyHit(KEY_ESCAPE)

	Cls

	SetColor 255, 255, 255
	DrawOval myGravBox.x - 5, myGravBox.y - 5, 10, 10
	
	SetColor 0, 0, 255
	DrawArrow MouseX(), MouseY(),myGravBox.x, myGravBox.y, 5, 5
	
	SetColor 255, 0, 0
	DrawArrow myGravBox.x, myGravBox.y,myGravBox.x + (myGravBox.xspeed * 10), myGravBox.y + (myGravBox.yspeed * 10), 20, 5  
	
	SetColor 0, 255, 0
	DrawArrow myGravBox.x, myGravBox.y,myGravBox.x + ((((MouseX() - myGravBox.x) / 10) * -1)), myGravBox.y + ((((MouseY() - myGravBox.y) / 10) * -1)), 20, 5  
	
	SetColor 255, 255, 255
	
	If KeyDown(KEY_LEFT) Then myGravBox.xspeed:- .05
	If KeyDown(KEY_RIGHT) Then myGravBox.xspeed:+ .05
	
	If MouseDown(1) Then
		myGravBox.xspeed:+ (((MouseX() - myGravBox.x) / 1000) * -1)
		myGravBox.yspeed:+ (((MouseY() - myGravBox.y) / 1000) * -1)
	End If
	
	myGravBox.Update()
	
	Flip

Wend

EndGraphics

End


Function DrawArrow(x1%, y1%, x2%, y2%, _arrowHeadLength% = 20, _arrowHeadWidth% = 10)

	DrawLine x1, y1, x2, y2
	
	Local lineAngle% = ATan2(x1 - x2, y1 - y2)
	
	lineAngle:+ 180
	DrawLine x2, y2, (x2 + (Sin(lineAngle * -1) * _arrowHeadLength%)) + (Cos(lineAngle) * _arrowHeadWidth%), (y2 - (Cos(lineAngle * -1) * _arrowHeadLength%)) - (Sin(lineAngle) * _arrowHeadWidth%)
	DrawLine x2, y2, (x2 + (Sin(lineAngle * -1) * _arrowHeadLength%)) - (Cos(lineAngle) * _arrowHeadWidth%), (y2 - (Cos(lineAngle * -1) * _arrowHeadLength%)) + (Sin(lineAngle) * _arrowHeadWidth%)
	
End Function


enjoy!

EDIT 2: YAE (Yet another example)
Graphics 800, 600, 32, 60

Type GravBox

	Field x#, y#
	Field xspeed#, yspeed#
	
	Function Create:GravBox(_x#, _y#)
	
		Local tempGravBox:GravBox = New GravBox
		
		tempGravBox.x# = _x#
		tempGravBox.y# = _y#
		
		Return tempGravBox
	
	End Function
	
	Method Update()
	
		UpdateMovement()
		
		xspeed:* .99
		yspeed:+ .09
		
		If y > GraphicsHeight() - 20 Then 
			yspeed = -yspeed / 1.05
			UpdateMovement()
		EndIf
		If y < 0 Then 
			yspeed = yspeed * -1
			UpdateMovement()
		EndIf
		If x < 0 Then 
			xspeed:* -1
			UpdateMovement()
		EndIf
		If x > GraphicsWidth() Then 
			xspeed:* -1
			UpdateMovement()
		EndIf
	
	End Method
	
	Method UpdateMovement()
		x:+ xspeed
		y:+ yspeed
	End Method

End Type

Global myGravBox:GravBox = GravBox.Create(GraphicsWidth() / 2, 20)

While Not KeyHit(KEY_ESCAPE)

	Cls

	SetColor 255, 255, 255
	DrawOval myGravBox.x - 5, myGravBox.y - 5, 10, 10
	
	SetColor 0, 0, 255
	DrawArrow myGravBox.x, myGravBox.y,myGravBox.x + ((((MouseX() - myGravBox.x) / 5))), myGravBox.y + ((((MouseY() - myGravBox.y) / 5))), 20, 5
	
	SetColor 255, 0, 0
	DrawArrow myGravBox.x, myGravBox.y,myGravBox.x + (myGravBox.xspeed * 10), myGravBox.y + (myGravBox.yspeed * 10), 20, 5  
	
	SetColor 0, 255, 0
	DrawArrow myGravBox.x, myGravBox.y,myGravBox.x + ((((MouseX() - myGravBox.x) / 5) * -1)), myGravBox.y + ((((MouseY() - myGravBox.y) / 5) * -1)), 20, 5  
	
	SetColor 255, 255, 255
	DrawText "Diablo's Ultra Kool Bouncy Ball Thingy", 10, 10
	DrawText "======================================", 10, 20
	DrawText "Left Mouse Button: Move ball away from cursor", 10, 30
	DrawText "Right Mouse Button: Move ball towards cursor", 10, 40
	SetColor 0, 255, 0
	DrawArrow 10, 60, 40, 60, 20, 2 DrawText " - Away Vector", 45, 55
	SetColor 0, 0, 255
	DrawArrow 10, 80, 40, 80, 20, 2 DrawText " - Towards Vector", 45, 75
	SetColor 255, 0, 0
	DrawArrow 10, 100, 40, 100, 20, 2 DrawText " - Movement Vector", 45, 95
	
	If MouseDown(1) Then
		myGravBox.xspeed:+ (((MouseX() - myGravBox.x) / 1000) * -1)
		myGravBox.yspeed:+ (((MouseY() - myGravBox.y) / 1000) * -1)
	ElseIf MouseDown(2) Then
		myGravBox.xspeed:- (((MouseX() - myGravBox.x) / 1000) * -1)
		myGravBox.yspeed:- (((MouseY() - myGravBox.y) / 1000) * -1)
	End If
	
	myGravBox.Update()
	
	Flip

Wend

EndGraphics

End


Function DrawArrow(x1%, y1%, x2%, y2%, _arrowHeadLength% = 20, _arrowHeadWidth% = 10)

	DrawLine x1, y1, x2, y2
	
	Local lineAngle% = ATan2(x1 - x2, y1 - y2)
	
	lineAngle:+ 180
	DrawLine x2, y2, (x2 + (Sin(lineAngle * -1) * _arrowHeadLength%)) + (Cos(lineAngle) * _arrowHeadWidth%), (y2 - (Cos(lineAngle * -1) * _arrowHeadLength%)) - (Sin(lineAngle) * _arrowHeadWidth%)
	DrawLine x2, y2, (x2 + (Sin(lineAngle * -1) * _arrowHeadLength%)) - (Cos(lineAngle) * _arrowHeadWidth%), (y2 - (Cos(lineAngle * -1) * _arrowHeadLength%)) + (Sin(lineAngle) * _arrowHeadWidth%)
	
End Function




Nice job Diablo, I only looked at the last one, and I think you mixed up the away vector and toward vector. Toward should be blue, away should be green.

Thx Sushimasta, Changed... look above.

Thanks!

And take a look at MaxPhysics if you get bored again, you might be able to help us out some. :)