News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Dotted Line... and the .while thing

Started by HSE, November 09, 2019, 06:03:00 AM

Previous topic - Next topic

HSE

Hi!

I found an algorithm for a dotted line. And I thought the problem was solved, but not.

The author write loops: .while (x != x2) but that work if you are lucky, other way you have an infinite loop.
The correct code is:.while (abs(x-x2)>2)

Here a code extended to other point separations (using ObjAsm for 32 and 64) tested in 32 bit:
if TARGET_BITNESS EQ 32
sxword textequ <sdword>
else
sxword textequ <sqword>
endif
Method Pixelmap2.DottedLine, uses xsi, x1:sxword,y1:sxword,x2:sxword,y2:sxword, espaciado:xword, dColor:xword
    local x:sxword,y:sxword,dxx:sxword,dyy:sxword,i:sxword,e:sxword
local xinc:sxword,yinc:sxword
SetObject xsi
    mov xax, x2
    sub xax, x1
    mov dxx, xax
    mov xax, y2
    sub xax, y1
    mov dyy, xax
    mov xax, x1
    .if (xax < x2)
        mov xinc , 1
    .else
        mov xinc , -1
    .endif
    mov xax, y1   
    .if (xax < y2)
        mov yinc, 1
    .else
        mov yinc, -1
    .endif
; Macros ------------------------------------------
salta macro var
            mov xax , &var&inc
            mov xcx , espaciado
            imul xcx
            add xax , &var
            mov &var , xax
        endm
proceso_dot macro v1, v2
    mov xax, d&v2&&v2
    mov xcx, espaciado
    imul xcx
    sub xax, d&v1&&v1
    mov e, xax
        ;.while (eax != x2)
        .while (xax > espaciado)
            .if ( e < 0)
    mov xax, d&v2&&v2
    mov xcx, espaciado
    imul xcx
                add xax, e
                mov e, xax
            .else
            mov xax, d&v2&&v2
            sub xax, d&v1&&v1
    mov xcx, espaciado
    imul xcx
                add xax, e
                mov e, xax
                salta &v2
          .endif
          salta &v1
  OCall xsi.BlendPixel, x, y, dColor
  ; -----------------
  mov xax , &v1
  sub xax , &v1&2
  cdq
  xor xax , xdx
  sub xax , xdx
        .endw
endm

; Algorithm ------------------------------------------
    m2m x, x1;
    m2m y, y1;
    mov xax, dxx
    .if xax >= dyy
    proceso_dot x, y
    .else
    proceso_dot y, x
    .endif
MethodEnd


Look nice horizontal and vertical, but don't look well in angles.

I have to try other thing :eusa_boohoo:

Regards. HSE
Equations in Assembly: SmplMath

jj2007

.while (abs(x-x2)>2)

Beware of macro expansion before the .while - check in the debugger

Use instead:
.While 1
  .Break .if abs(x-x2)<=2
  ...
.Endw

HSE

Yesterday I forget that for a moment  :biggrin:

Now is: .while (xax > espaciado)
  ; -----------------
  code here
                  ; ----------------- 
  mov xax , &v1       ; this is abs(x-x2)       
  sub xax , &v1&2
  cdq
  xor xax , xdx
  sub xax , xdx
        .endw
Equations in Assembly: SmplMath