# Draw line through 2 points.

## Recommended Posts

I know using _GDIPlus_GraphicsDrawLine you can defined the start and stop of a line, but I want the line to go through these points, not just stop.

something similar to this applet:

http://www.cut-the-knot.org/Curriculum/Calculus/StraightLine.shtml

Can this be done?

##### Share on other sites

I know using _GDIPlus_GraphicsDrawLine you can defined the start and stop of a line, but I want the line to go through these points, not just stop.

something similar to this applet:

http://www.cut-the-knot.org/Curriculum/Calculus/StraightLine.shtml

Can this be done?

I am thinking this should go in the GUI help...

Please move it if you can

##### Share on other sites

Well, use the user's screen width and height to draw a complete line using the information you already have.

You want to draw a line from (0,0) to (500,500) but you want to continue. The user has a screen resolution of 1000,1000 (this is all purely theoretical speculation), so use some math to get it 1000,1000.

##### Share on other sites

I mayby know a method, but it take's some math and time.

And time is what I don't have atm.

EDIT: This is what i'm thinking:

1. Get the 2 points

2. Calculate the angle

3. Calculate the line down/up to screen width/height

4. Draw the line on the new calculated points.

Edited by AlmarM

Minesweeper

A minesweeper game created in autoit, source available.

_Mouse_UDF

An UDF for registering functions to mouse events, made in pure autoit.

2D Hitbox Editor

A 2D hitbox editor for quick creation of 2D sphere and rectangle hitboxes.

##### Share on other sites

ParoXsitiC et al,

Do you guys no longer do basic geometry at school!

```#include <GUIConstantsEx.au3>

Global \$X, \$Y

; Define original points
\$X1 = 100
\$Y1 = 100

\$X2 = 400
\$Y2 = 200

\$nGradient = ATan((\$Y2 - \$Y1) / (\$X2 - \$X1))

; Offset of line from origin
\$nOffset = \$Y1 - \$Y_Base

; Line equation is therefore:
\$Y = (\$X * Tan(\$nGradient)) + \$nOffset

; Create GUI to draw line
\$hGUI = GUICreate("Test", 500, 500)

; Draw line using the equation from above
For \$i = 0 To 500
GUICtrlCreateLabel("", \$i, (\$i * Tan(\$nGradient)) + \$nOffset, 2, 2)
GUICtrlSetBkColor(-1, 0x0000FF)
Next
; Show original points
GUICtrlCreateLabel("", 100, 100, 4, 4)
GUICtrlSetBkColor(-1, 0xFF0000)
GUICtrlCreateLabel("", 400, 200, 4, 4)
GUICtrlSetBkColor(-1, 0xFF0000)

GUISetState()

While 1
Switch GUIGetMsg()
Case \$GUI_EVENT_CLOSE
Exit
EndSwitch
WEnd```

M23

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

##### Share on other sites

I ruined my math classes.

I never was interested by math, and now, since a while... I am.

Minesweeper

A minesweeper game created in autoit, source available.

_Mouse_UDF

An UDF for registering functions to mouse events, made in pure autoit.

2D Hitbox Editor

A 2D hitbox editor for quick creation of 2D sphere and rectangle hitboxes.

##### Share on other sites

Do you guys no longer do basic geometry at school!

It was a long time ago. Nice trig example, thanks!

I think I'll use this to create an assortment of different shaped GUI controls, or just to paint.

Edited by czardas

##### Share on other sites

Here a GDI+ version without trigonometry

```;fast hack by UEZ 2010
#include <GDIPlus.au3>
#include <GUIConstantsEx.au3>
Opt("MustDeclareVars", 1)
Opt("GUIOnEventMode", 1)

_GDIPlus_Startup()

Local \$Width = 800
Local \$Height = 600
Local \$hGUI = GUICreate("GDI+ Math", \$Width, \$Height)
Local \$GUI_gbc = 0xF0F0F0
GUISetBkColor(\$GUI_gbc)
GUISetState()

Local \$hGraphics = _GDIPlus_GraphicsCreateFromHWND(\$hGUI)
Local \$hBitmap = _GDIPlus_BitmapCreateFromGraphics(\$Width, \$Height, \$hGraphics)
Local \$hBackbuffer = _GDIPlus_ImageGetGraphicsContext(\$hBitmap)

_GDIPlus_GraphicsSetSmoothingMode(\$hBackbuffer, 2)

Local \$hPen = _GDIPlus_PenCreate(0xFF0000FF, 1)
Local \$hBrush = _GDIPlus_BrushCreateSolid(0xFFFF0000)
Local \$hBrush_size = 6

GUISetOnEvent(\$GUI_EVENT_CLOSE, "_Exit")

Local \$rX1 = Random(\$Width * 0.2, \$Width * 0.8)
Local \$rY1 = Random(\$Height * 0.2, \$Height * 0.8)
Local \$rX2 = Random(\$Width * 0.2, \$Width * 0.8)
Local \$rY2 = Random(\$Height * 0.2, \$Height * 0.8)

_GDIPlus_GraphicsClear(\$hBackbuffer, "0xFF" & \$GUI_gbc)

Local \$slope_x = \$rX2 - \$rX1
Local \$slope_y = \$rY2 - \$rY1

Local \$len = 0.75

_GDIPlus_GraphicsDrawLine(\$hBackbuffer, \$rX1, \$rY1, \$rX1 - \$len * \$slope_x, \$rY1 - \$len * \$slope_y, \$hPen)
_GDIPlus_GraphicsDrawLine(\$hBackbuffer, \$rX2, \$rY2, \$rX2 + \$len * \$slope_x, \$rY2 + \$len * \$slope_y, \$hPen)
_GDIPlus_GraphicsDrawLine(\$hBackbuffer, \$rX1, \$rY1, \$rX2, \$rY2, \$hPen)

_GDIPlus_GraphicsFillEllipse(\$hBackbuffer, \$rX1 - \$hBrush_size / 2, \$rY1 - \$hBrush_size / 2, \$hBrush_size, \$hBrush_size, \$hBrush)
_GDIPlus_GraphicsFillEllipse(\$hBackbuffer, \$rX2 - \$hBrush_size / 2, \$rY2 - \$hBrush_size / 2, \$hBrush_size, \$hBrush_size, \$hBrush)

_GDIPlus_GraphicsDrawImageRect(\$hGraphics, \$hBitmap, 0, 0, \$Width, \$Height)

While Sleep(1000)
WEnd

Func _Exit()
_GDIPlus_PenDispose(\$hPen)
_GDIPlus_BrushDispose(\$hBrush)
_GDIPlus_BitmapDispose(\$hBitmap)
_GDIPlus_GraphicsDispose(\$hBackbuffer)
_GDIPlus_GraphicsDispose(\$hGraphics)
_GDIPlus_Shutdown()
GUIDelete(\$hGUI)
Exit
EndFunc```

Br,

UEZ

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

##### Share on other sites

Hi, there is no need to use the (slow) trigonometric functions!

The function of a Line is y=m*x+b. So all you have to do is calculate m=deltay/deltax, put it into the transformed (is this the right expression in math?) equation, and calculate b.

b=y-m*x

so you have a formula, which calculates the right y to each x.

```#include <GUIConstantsEx.au3>

Global \$X, \$Y

; Define original points
\$X1 = 100
\$Y1 = 100

\$X2 = 400
\$Y2 = 200

;y=mx+b

\$m=(\$y2-\$y1)/(\$x2-\$x1)  ;deltay/deltax
\$b=\$y1-\$m*\$x1

; Create GUI to draw line
\$hGUI = GUICreate("Test", 500, 500)

; Draw line using the equation from above
For \$x = 0 To 500
\$y=\$m*\$x+\$b
GUICtrlCreateLabel("", \$x,\$y , 2, 2)
GUICtrlSetBkColor(-1, 0x0000FF)
Next
; Show original points
GUICtrlCreateLabel("", 100, 100, 4, 4)
GUICtrlSetBkColor(-1, 0xFF0000)
GUICtrlCreateLabel("", 400, 200, 4, 4)
GUICtrlSetBkColor(-1, 0xFF0000)

GUISetState()

While 1
Switch GUIGetMsg()
Case \$GUI_EVENT_CLOSE
Exit
EndSwitch
WEnd```

/edit/ this is also a quick way to determine whether a point is on a line

Edited by AndyG

##### Share on other sites

And to think that the Greeks did all this with a compas and an uncalibrated ruler.

Examples like these are really clear. I must try to do something with GDI+, it seems so awesome. I do need some clickable controls of different shapes though. I was thinking that combining coloured labels as in Melba23's example would be a possibility, although perhaps there are easier solutions. Good thread!

Edited by czardas

## Create an account

Register a new account