Drawing textures as lines.

BlitzMax Forums/BlitzMax Beginners Area/Drawing textures as lines.

Ryan Burnside(Posted 2006) [#1]
Ok, so everyone and their brother is making a vector glow engine. I would like to make my own too. My problem comes when i try to draw rotated textures. How is this done? I know I need to set the rotation before drawing a stretched texture. However, after doing that the angle is off sometimes. can someone provide a function to draw a given image stretched from point x,y to point x2,y2? Basically i would like to draw a texture that imitates a line. Thanks in advance. My problem is that the line is not drawn centered.

here us my code
Function point_direction#(x1#,y1#,x2#,y2#)
	    
	    Local Angle# = ATan2(y2-y1,x2-x1)
	    	If angle<0
	    		angle:+ 360
	    	End If
		
		Return angle
	EndFunction 
Function point_distance#(x1#,y1#,x2#,y2#)

' get the x difference
Local x_distance#=Abs(x1-x2)

' get the y difference
Local y_distance#=Abs(y1-y2)

' find the difference using pythagroin therom
Local distance#=sqr((x_distance*x_distance)+(y_distance*y_distance))

Return distance
EndFunction	
Function draw_texture(x#,y#,x2#,y2#)
    
    Local direction=point_direction(x,y,x2,y2-8)
    Local length=point_distance(x,y,x2,y2)
    SetRotation direction
    
   
    DrawImageRect(texture,x,y-8,length,16)
    SetRotation 0
   
End Function



Ryan Burnside(Posted 2006) [#2]
Perhaps my complete source will help better explain my delimma. The textures are just slightly off.

source:

Strict
graphics 640,480


' this must be smaller than the original image
Global texture:TImage=CreateImage:TImage(1,8)
draw_gradient(128,128,128,255,255,255,0,0,1,4)
draw_gradient(255,255,255,128,128,128,0,4,1,4)
GrabImage(texture,0,0)
MidHandleImage(texture)


Cls
Global texture2:TImage=CreateImage:TImage(16,16)
For Local i=0 To 16
SetColor 16*i,16*i,16*i
SetBlend SOLIDBLEND
DrawOval(i/2,i/2,16-i,16-i) 

MidHandleImage(texture2)
Next
GrabImage(texture2,0,0)
MidHandleImage(texture2)
' here we will keep the drawing function
Function poly_radius#(sidelength:Float,num_sides:Int)' finds circumscribed radius of an ngon
		Local r:Float = sidelength /(2*Sin(180.0/num_sides))
		Return r
End Function
Function draw_ngon#(x#,y#,numsides%,sidelength#,rotation#)' draws an ngon taking into account rotation
	Local small_angle# =360.0/numsides
	Local radius#=poly_radius(sidelength,numsides)
	
		For Local i=0 To numsides-1
		draw_texture(x+Cos(i*small_angle+rotation)*radius,y+Sin(i*small_angle+rotation)*radius,x+Cos((i*small_angle)+small_angle+rotation)*radius,y+Sin((i*small_angle)+small_angle+rotation)*radius)
		
		Next
	
		
End Function
Function draw_gradient(red#,green#,blue#,red2#,green2#,blue2#,x#,y#,width#,height#)
	 Local red_dif#= red-red2
	 Local green_dif#=green-green2
	 Local blue_dif#=blue-blue2
	 ' draw the lines
	 For Local i=0 To height-1
	 	SetColor red-(i*(red_dif/height)),green-(i*(green_dif/height)),blue-(i*(blue_dif/height))
	 	DrawLine x,y+i,x+width-1,y+i
	 Next
End Function
Function point_direction#(x1#,y1#,x2#,y2#)
	    
	    Local Angle# = ATan2(y2-y1,x2-x1)
	    	If angle<0
	    		angle:+ 360
	    	End If
		
		Return angle
	EndFunction 
Function point_distance#(x1#,y1#,x2#,y2#)

' get the x difference
Local x_distance#=Abs(x1-x2)

' get the y difference
Local y_distance#=Abs(y1-y2)

' find the difference using pythagroin therom
Local distance#=sqr((x_distance*x_distance)+(y_distance*y_distance))

Return distance
EndFunction	
Function draw_texture(x#,y#,x2#,y2#)
    
    Local direction=point_direction(x,y,x2,y2)
    Local length=point_distance(x,y,x2,y2)
    SetRotation direction
    
    DrawImageRect(texture,x,y,length,8) 
    
    
    SetRotation 0
   
End Function
'make some objects
Type graphic
	Field x#,y#,numsides%,direction#,color%
	Global obj_list:TList=New TList
	
	Function Create()
		Local temp:graphic=New graphic
		temp.x=360
		temp.y=240
		temp.numsides=Rand(3,8)
		temp.direction=Rand(360)
		temp.color=Rand(0,1)
		ListAddLast(obj_list,temp)
	End Function
	Method Update()
	x:+Cos(direction)*3
	y:+Sin(direction)*3
	If color=0
	SetColor 64,128,255
	Else
	SetColor 255,128,64
	EndIf 
	draw_ngon#(x,y,numsides,64,direction)
	 
	If x<0
		x=0
		direction=Rand(360)
	End If
	If y<0
		y=0
		direction=Rand(360)
	End If
	If x>640
		x=640
		direction=Rand(360)
	End If
	If y>480
		y=480
		direction=Rand(360)
	End If
		
	End Method
End Type



For Local i=0 To  100
	graphic.Create()
	
Next   



While not KeyDown(KEY_ESCAPE)
	Cls
	SetBlend LIGHTBLEND
	SetAlpha .5
	
	For Local i:graphic=EachIn(graphic.obj_list)
	i.Update()
	Next
	draw_texture(320,240,MouseX(),MouseY())
	SetColor 0,0,0
	SetBlend ALPHABLEND

	SetAlpha 1 
	Plot 320,240
	Plot MouseX(),MouseY()
	HideMouse() 
	
	Flip
Wend