Code archives/Graphics/Line With Width

This code has been declared by its author to be Public Domain code.

Download source code

Line With Width by semar2002
A function that draws a line with a given width. Shows the use of Atan2 to calculate the angle between the given points.

The line width effect is obtained drawing parallel lines.

The code provided contains the function and an example about how to call it.
;draw line with width - by semar
Graphics 640,480,0,2
SetBuffer BackBuffer()

;initial point and angle settings
x1 = GraphicsWidth()/2
y1 = GraphicsHeight()/2
x2 = x1 + 100
y2 = y1 + 100

LINE_WIDTH = 5 ;width of the line

;until esc is pressed
While Not KeyDown(1)

;clear the screen
Cls

If MouseHit(1) Then
	;gets the second point with a left mouse click
	x2 = MouseX()
	y2 = MouseY()
EndIf	

draw_line(x1,y1,x2,y2,LINE_WIDTH)
Text 0,0,"Left Mouse Click to draw a new line - ESC to quit"

Flip

Wend
End

;=========================================
Function draw_line(x1,y1,x2,y2,LINE_WIDTH)
;=========================================

Local n# = 0
Local angle# = 0
Local normal# = 0

;found the angle of the line
angle = ATan2((y2-y1),(x2-x1))

;found the normal to the line
normal = 90 - angle

;show info about the angles found
Text 0,20, "Line Angle: " + angle
Text 0,40,"Line Normal: " + normal

;draw the line with the desired width
For n = -LINE_WIDTH/2 To LINE_WIDTH/2 Step 0.05
	
	x3# = x1 + Cos(normal)*n
	y3# = y1 - Sin(normal)*n

	x4# = x2 + Cos(normal)*n
	y4# = y2 - Sin(normal)*n
	
	Line x3,y3,x4,y4
Next
End Function

Comments

None.

Code Archives Forum