Jump to content

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


Luigi
 Share

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 $aDot[60][40]
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)
        GUICtrlSetBkColor($aDot[$ii][$jj], 0xFFFFFF)
        $oDot.Add($aDot[$ii][$jj], "[" & $ii & "," & $jj & "]")
        GUICtrlSetOnEvent($aDot[$ii][$jj], "tip")
    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
            GUICtrlSetBkColor($aDot[$ii][$yy], 0x0000FF)
        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

Link to comment
Share on other sites

  • Moderators

Detefon,

How about using a graphic control? :huh:

#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

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Link to comment
Share on other sites

  • Moderators

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

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Link to comment
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
        GUICtrlSetBkColor($aDot[Int($xa)][Int($ya)], 0x0000FF)
        $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

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

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

Link to comment
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
Link to comment
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.

https://www.youtube.com/watch?v=dq8UWEDU7q8

@UEZ, thank you, your code work fine! :thumbsup:

Visit my repository

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...