Drawing a line through 2 points

Hi I am fairly new to Xojo and programming in general, could someone assist me with how to draw a line that extends through Two Points( That i already have created) placed in a canvas. The code will be in a method

Can’t you use graphics.drawLine? You pass it the two points and it draws a line between them.


i want the line line to extend past the two given points instead of just between them

Then you need to calculate the ending point that lies beyond the two.

To do so, look at the proportion of x2/y2 and apply to x3 to find the value of y3 and the line will extend from x1/y1 through x2/y2 to reach x3/y3 in a straight line.

This is a math problem.

You would need to calculate the coordinates of the line.

Create a module in your Xojo project and copy/paste this code:

[code]Sub DrawLineExtended(extends g As Graphics, x1 As Integer, y1 As Integer, x2 As Integer, y2 As Integer)

Dim m As Single
Dim p As Single

//Calculating coordinates of the line. The line math is y = mx+p
m = (y2-y1)/(x2-x1)
p = -m
x1 + y1

g.DrawLine(0, 0*m+p, g.Width-1, (g.Width-1)*m+p)
End Sub

Then in the Canvas.Paint event place this code:

//Visual feedback of the two points
g.ForeColor = &cFF0000
g.DrawLine(45, 45, 55, 55)
g.DrawLine(195, 95, 205, 105)

//Drawing the line
g.ForeColor= &c0
g.DrawLineExtended(50, 50, 200, 100)[/code]