Vistas de página en total

Mostrando entradas con la etiqueta CIRCLE. Mostrar todas las entradas
Mostrando entradas con la etiqueta CIRCLE. Mostrar todas las entradas

domingo, 30 de marzo de 2014

EJEMPLO TRAZADO DE CIRCULOS

  Este programa es un ejemplo de ejecución de la librería gráfica de circulos.
   Lo más complejo de esta rutina es calcular el radio de la circunferencia a partir de dos puntos seleccionados en pantallas.
   Para ello, utilizo el teorema de pitágoras, y apoyándome en las funciones matemáticas puestas anteriormente calculo el radio.   Esto lo programé ya en la función puesta anterirometne dist_pixel.

   En este caso para que quede3 más elegante, el programa incrementa el color por cada circulo.

                       

     Se puede bajar el ejemplo compilado en el enlace





EJ_CIRCL.ASM

; Copyright (C) 2013  José Ángel Moneo Fernández

;    This program is free software: you can redistribute it and/or modify
;    it under the terms of the GNU General Public License as published by
;    the Free Software Foundation, either version 3 of the License, or
;   (at your option) any later version.

;    This program is distributed in the hope that it will be useful,
;    but WITHOUT ANY WARRANTY; without even the implied warranty of
;    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;    GNU General Public License for more details.

;    You should have received a copy of the GNU General Public License
;    along with this program.  If not, see <http://www.gnu.org/licenses/>.

include main.mac
include stdio.mac
include mouse.mac
include graphics.mac
include math.mac

.model compact,pascal
.stack 200

.data
x0 dw 100
y0 dw 100
x1 dw 90
y1 dw 90
radio dw 0,0

color dw 1

.code

principal proc far
        main
        modovideo 12h   ;inicia graficos
        resmouse         ;reinicia ratón
        mouse on         ; activa rarón
    
punto1:
        getmouse        ;espera  EST+ SIN PULSAR
        cmp ax,0
        jne punto1
pulsar1:      
        getmouse         ;verifica espera pulsación raton
        cmp ax,2      ;salir?
        jne sig1
        jmp fuera
sig1:      
        cmp ax,1      ;boton izquierdo?
        jne pulsar1
        getmousex
        mov x0,ax
        getmousey
        mov y0,ax
punto2:
        getmouse        ;espera soltar
        cmp ax,0
        jne punto2
pulsar2:     
        getmouse         ;verifica espera pulsación raton
        cmp ax,2      ;salir?
        jNe sg1
        JMP FUERA
SG1:      
        cmp ax,1      ;boton izquierdo?
        jne pulsar2
        getmousex
        mov x1,ax
        getmousey
        mov y1,ax
        dist_pixel x0,y0,x1,y1
     
        mov radio,ax

        mouse off
        mov ax,color        ;incremeta color en cada circulo
        inc ax
        and ax,0fh
        mov color,ax
      
        circle x0,y0,radio,color
        mouse on
      
        jmp punto1
fuera:
        modovideo t80col
        resmouse
        mouse off
        exit  0
principal endp

end

miércoles, 26 de marzo de 2014

CIRCLE

   Siguiendo las funciones gráficas, la siguinete depues de la línea en impritancia es el circulo.
Al igual que he hecho con la línea utilizaré el algoritmo de Bresenham.

 
  El pseudocódigo del algoritmo es:
 
*Se capturan el radio r y el centro de la circunferencia (xc, yc).
 *Se obtiene el primer punto de la circunferencia centrada en origen (xc, yc) como (0, r).
 *Se cacula el valor inicial del parametro de decisión como p0=5/4 - r.
 Para k=0 hasta x>=y incrementa k
    Si pk < 0 
       *Siguiente punto de la circunferencia con centro (0,0) es (xk+1, yk).
       *pk+1=pk+2xk+1+1.
    Sino
        *Siguiente punto de la circunferencia con centro (0,0) es (xk+1, yk-1).
       *pk+1=pk+2xk+1+1-2yk+1.
    //Donde 2xk+1=2xk+2  y  2yk+1=2yk-2
 
 *Se determinan los puntos de simetría para los otros siete octantes.
 *Se mueve cada posición del pixel calculada (x,y) a la trayectoria circular centrada en (xc, yc) 
   y trazamos los valores de las coordenadas: x=x+xc y y=y+yc.
 Fin Para

   Fuente wikipedia.

     Aquí teneis la implementación del algoritmo en ensamblador, comentado en C, para que sirva de entreanmiento a la hora de codificar en lenguaje ensamblador otros algoritmos.




CIRCLE.ASM
; Copyright (C) 2013  José Ángel Moneo Fernández

;    This program is free software: you can redistribute it and/or modify
;    it under the terms of the GNU General Public License as published by
;    the Free Software Foundation, either version 3 of the License, or
;   (at your option) any later version.

;    This program is distributed in the hope that it will be useful,
;    but WITHOUT ANY WARRANTY; without even the implied warranty of
;    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;    GNU General Public License for more details.

;    You should have received a copy of the GNU General Public License
;    along with this program.  If not, see <http://www.gnu.org/licenses/>.


include graphics.mac


.model compact,pascal

        public _circle
.data

e dw 0     ;variable p procedimiento breshenham


.code
       ;algoritmo Bresenham
_circle proc uses ax bx dx si di,x0,y0,radio,color
        mov bx,0     ;x=0
        mov dx,radio      ;y=radio
        mov cx,dx   ;e=radio
        neg cx       ;e=-radio
        add cx,1    ; e=1-radio

      
while:        ;while (x<y)
        cmp bx,dx
        jb sig
        jmp fin
sig:
        call _dibuja8   ;dibuja en los 8 octantes
        inc bx       ;x=x+1
        cmp cx,0    ;if (e>0)
        jl else_if
        dec dx       ;y=y+1
        mov ax,bx
        sub ax,dx   ;x-y
        shl ax,1    ;(x-y)*2
        add ax,1  ;1*(x-y)+1
        add cx,ax    ;e=e+ 2*(x-y)+1
        jmp fin_if
else_if:
        mov ax,bx
        shl ax,1
        add ax,1         
        add cx,ax   ;e=e+2*x+1
fin_if:
     
        jmp while      
fin:
        ret
_circle endp 

;dibuja el punto en cada octante, referido a la coordenada origen del circulo
_dibuja8 proc
        mov si,bx          
        add si,x0           ;xc=x+x0
        mov di,dx          
        add di,y0           ;yc=y+y0
        putpixel si,di,color    ;pixel(x+x0,y+y0)
        mov si,dx          
        add si,x0           ;xc=y+x0
        mov di,bx          
        add di,y0           ;yc=x+y0
        putpixel si,di,color    ;pixel(y+x0,x+y0)
        mov si,bx             
        neg si
        add si,x0            ;xc=-x+x0
        mov di,dx          
        add di,y0           ;yc=y+y0
        putpixel si,di,color          ;pixel(-x+x0,y+y0)
        mov si,dx          
        add si,x0           ;xc=y+x0
        mov di,bx             
        neg di
        add di,y0               ;yc=-x+y0
        putpixel si,di,color           ;pixel(y+x0,-x+y0)
        mov si,bx             
        neg si
        add si,x0               ;xc=-x+x0
        mov di,dx 
        neg di              
        add di,y0               ;yc=-y+y0
        putpixel si,di,color          ;pixel(-x+x0,-y+y0)
        mov si,dx 
        neg si              
        add si,x0               ;yc=-y+x0
        mov di,bx             
        neg di
        add di,y0               ;xc=-x+y0
        putpixel si,di,color           ;pixel(-y+x0,-x+y0)
        mov si,bx          
        add si,x0           ;xc=x+x0
        mov di,dx 
        neg di              
        add di,y0               ;yc=-y+y0
        putpixel si,di,color          ;pixel(x+x0,-y+y0)
        mov si,dx 
        neg si              
        add si,x0               ;yc=-y+x0
        mov di,bx          
        add di,y0           ;yc=x+y0
        putpixel si,di,color           ;pixel(-y+x0,x+y0)
        ret
_dibuja8 endp       

end