# How draw a line in cartesian coords a(x1,y1) to b(x2,y2)?

## Recommended Posts

Hello forum! Happy new year! 8D

In this code, I think draw a line is simple, but I can't...

How to draw a line from the point(2,4) until point(18,10)?

(line 26): line(2, 4, 18, 10)

Obs: use GDI to draw its easy, but I need know the code/alghorithm to do this...

I was use some parts of geometry alalitic, like: y - ya = m(x - xa)

But not work fine when you try draw a line like this: (2, 4, 18, 4)

Obs2: if you clieck over square, the code print the current position (line, column).

code

```#include-once
#include <Array.au3>
#include <Misc.au3>
#include <GUIConstantsEx.au3>

OnAutoItExitRegister("on_exit")

Opt('GUIOnEventMode', 1)
Opt('GUIEventOptions', 1)
Opt('MustDeclareVars', 1)
Global \$aGuiSize[2] = [800, 600]
Global \$sGuiTitle = "GuiTitle"
Global \$hGui
Global \$oDot = ObjCreate("Scripting.Dictionary")

\$hGui = GUICreate(\$sGuiTitle, \$aGuiSize[0], \$aGuiSize[1])
GUISetOnEvent(\$GUI_EVENT_CLOSE, 'quit')

For \$ii = 0 To UBound(\$aDot, 1) - 1
For \$jj = 0 To UBound(\$aDot, 2) - 1
\$aDot[\$ii][\$jj] = GUICtrlCreateLabel("", (\$ii) * 8 + \$ii, (\$jj) * 8 + \$jj, 8, 8)
Next
Next

Func tip()
ConsoleWrite(\$oDot.Item(@GUI_CtrlId) & @LF)
EndFunc   ;==>tip

line(2, 4, 8, 9)
Func line(\$xa, \$ya, \$xb, \$yb)
Local \$mm = (\$yb - \$ya) / (\$xb - \$xa)
Local \$yy
For \$ii = \$xa To \$xb
For \$jj = \$ya To \$yb
\$yy = \$mm * (\$ii - \$xa) + \$ya
Next
Next
EndFunc   ;==>line

GUISetState(@SW_SHOW, \$hGui)

While Sleep(10)
WEnd

Func quit()
Exit
EndFunc   ;==>quit

Func on_exit(\$sInput = 0)
GUIDelete(\$hGui)
EndFunc   ;==>on_exit```

Visit my repository

##### Share on other sites

Detefon,

How about using a graphic control?

```#include <GUIConstantsEx.au3>
#include <WinAPI.au3>

\$hGUI = GUICreate("Test", 500, 500)
GUISetBkColor(0xC4C4C4)

\$cGraphic = GUICtrlCreateGraphic(10, 10, 480, 400)
GUICtrlSetColor(\$cGraphic, 0x000000)
GUICtrlSetBkColor(\$cGraphic, 0xCCFFCC)
GUICtrlSetGraphic(\$cGraphic, \$GUI_GR_COLOR, 0xFF0000)

\$cButton = GUICtrlCreateButton("Draw", 10, 450, 80, 30)

GUISetState()

While 1
Switch GUIGetMsg()
Case \$GUI_EVENT_CLOSE
Exit
Case \$cButton
_GR_Move(2, 4)
_GR_Line(18, 10)
_GR_Line(200, 200)
_WinAPI_RedrawWindow(\$hGUI)
EndSwitch
WEnd

Func _GR_Move(\$iX, \$iY)
\$iY = 400 - \$iY
GUICtrlSetGraphic(\$cGraphic, \$GUI_GR_MOVE, \$iX, \$iY)
EndFunc

Func _GR_Line(\$iX, \$iY)
\$iY = 400 - \$iY
GUICtrlSetGraphic(\$cGraphic, \$GUI_GR_LINE, \$iX, \$iY)
EndFunc```
I have used functions for each operation to get the Y coordinate to work in the correct sense.

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

Hi Melba, I need know the code/algorithm to draw a line...

Sorry,

Detefon.

Edited by Detefon

Visit my repository

##### Share on other sites

Detefon,

Then if:

How to draw a line from the point(2,4) until point(18,10)?

is not your question, please explain what exactly you do want to know.

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

This is only a partly solution:

```Func line(\$xa, \$ya, \$xb, \$yb)
Local \$mm = (\$yb - \$ya) / (\$xb - \$xa) * 0.03333, \$fDelta = 0.1
While \$xa < \$xb - \$fDelta Or \$ya < \$yb - \$fDelta
\$xa += (\$xb - \$xa) * \$mm
\$ya += (\$yb - \$ya) * \$mm
WEnd
EndFunc   ;==>line```

for \$xa < \$xb and  \$ya < \$yb.

The rest is homework for you.

Br,

UEZ

Edited by UEZ

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

##### Share on other sites

hi. happy new year guys. Another way.

```Func DrawLine(\$x1,\$y1,\$x2,\$y2)
Local \$x=0,\$y=0
For \$x = \$x1 To \$x2
\$y=\$y1+(((\$y2-\$y1)/(\$x2-\$x1))*(\$x-\$x1))
;DrawDot(\$x,\$y)
Next
EndFunc```

Saludos

Edited by Danyfirex

Sometimes I learn, Sometimes I teach...

AutoIt...

##### Share on other sites

is not your question, please explain what exactly you do want to know.

In parts, not all...

Draw a line is not simple a draw, it's check if there are a collision in an array, and let or not a moviment in this direction.

A full idea is >here and here.

@UEZ, thank you, your code work fine!

Visit my repository

##### Share on other sites

Edited by Detefon

Visit my repository

## Create an account

Register a new account