Log inRegister an accountBrowse CSDbHelp & documentationFacts & StatisticsThe forumsAvailable RSS-feeds on CSDbSupport CSDb Commodore 64 Scene Database
You are not logged in - nap
CSDb User Forums


Forums > C64 Coding > Bresenham Circle Routine
2010-01-13 17:53
Scout

Registered: Dec 2002
Posts: 1570
Bresenham Circle Routine

Because Codebase64.org doesn't like me or something, I might as well post it here: a Bresenham Circle Routine.

This example is textmode (40x25) only but can easily be converted into any other mode.
Enjoy and don't forget to credit me if you use it ;-)

I hope those solid circle tunnels show up now in a demo.

; Draws a circle using the Bresenham algorithm in textmode 
; [C]2006 Scout/Silicon Ltd.

Radius	= 12
CenterX	= 20
CenterY = 12
Char    = 160
Cx	= $50
Cy	= $51
Cd	= $52

*=$0810

circle
		lda #Radius
		sta Cy
		asl	 ; 2 * radius
		sta CalcD

		lda #0
		sta Cx

		lda #3
		sec
CalcD = *+1
		sbc #$00
		sta Cd

C_loop
		lda Cx
		cmp Cy
		bmi C_Points
		jmp E_rts
C_points

		lda Cx
		sta x1
		sta y2
		sta x4
		sta y7

		eor #$ff	; Maak 
		clc		; getal
		adc #1		; negatief
		sta y3
		sta x5
		sta y6
		sta x8

		lda Cy

		sta y1
		sta x2
		sta x3
		sta y8

		eor #$ff
		clc
		adc #1
		sta y4
		sta y5
		sta x6
		sta x7

y1=*+1
		lda #0			; y
		clc
		adc #CenterY
		tax
		lda screenlo,x
		sta $f0
		lda screenhi,x
		sta $f1 
x1=*+1
		lda #0			; x
		clc
		adc #CenterX
		tay
		lda #Char
		sta ($f0),y
;-----

y2=*+1
		lda #0			; x
		clc
		adc #CenterY
		tax
		lda screenlo,x
		sta $f0
		lda screenhi,x
		sta $f1 
x2=*+1
		lda #0			; y
		clc
		adc #CenterX
		tay
		lda #Char
		sta ($f0),y

;-----

y3=*+1
		lda #0			; x
		clc
		adc #CenterY
		tax
		lda screenlo,x
		sta $f0
		lda screenhi,x
		sta $f1 
x3=*+1
		lda #0			; y
		clc
		adc #CenterX
		tay
		lda #Char
		sta ($f0),y
;----

y4=*+1
		lda #0			; x
		clc
		adc #CenterY
		tax
		lda screenlo,x
		sta $f0
		lda screenhi,x
		sta $f1 
x4=*+1
		lda #0			; y
		clc
		adc #CenterX
		tay
		lda #Char
		sta ($f0),y
		
;----

y5=*+1
		lda #0			; x
		clc
		adc #CenterY
		tax
		lda screenlo,x
		sta $f0
		lda screenhi,x
		sta $f1 
x5=*+1
		lda #0			; y
		clc
		adc #CenterX
		tay
		lda #Char
		sta ($f0),y

y6=*+1
		lda #0			; x
		clc
		adc #CenterY
		tax
		lda screenlo,x
		sta $f0
		lda screenhi,x
		sta $f1 
x6=*+1
		lda #0			; y
		clc
		adc #CenterX
		tay
		lda #Char
		sta ($f0),y

y7=*+1
		lda #0			; x
		clc
		adc #CenterY
		tax
		lda screenlo,x
		sta $f0
		lda screenhi,x
		sta $f1 
x7=*+1
		lda #0			; y
		clc
		adc #CenterX
		tay
		lda #Char
		sta ($f0),y

y8=*+1
		lda #0			; x
		clc
		adc #CenterY
		tax
		lda screenlo,x
		sta $f0
		lda screenhi,x
		sta $f1 
x8=*+1
		lda #0			; y
		clc
		adc #CenterX
		tay
		lda #Char
		sta ($f0),y

		lda Cd
		bpl D_pos	; d >= 0
		
		lda Cx
		asl
		asl	; 4*x
		clc
		adc #6
		adc Cd
		sta Cd
		inc Cx
		jmp C_loop

D_pos
		lda Cx
		sec
		sbc Cy
		asl
		asl	;4*(x-y)
		clc
		adc #10
		clc
		adc Cd
		sta Cd
		dec Cy

E_next
		inc Cx
		jmp C_loop

E_rts
		lda Cx
		sta xx1
		sta yy2
		sta xx4
		sta yy7

		eor #$ff
		clc
		adc #1
		sta yy3
		sta xx5
		sta yy6
		sta xx8

		lda Cy

		sta yy1
		sta xx2
		sta xx3
		sta yy8

		eor #$ff
		clc
		adc #1
		sta yy4
		sta yy5
		sta xx6
		sta xx7

yy1=*+1
		lda #0			; y
		clc
		adc #CenterY
		tax
		lda screenlo,x
		sta $f0
		lda screenhi,x
		sta $f1 
xx1=*+1
		lda #0			; x
		clc
		adc #CenterX
		tay
		lda #Char
		sta ($f0),y
;-----

yy2=*+1
		lda #0			; x
		clc
		adc #CenterY
		tax
		lda screenlo,x
		sta $f0
		lda screenhi,x
		sta $f1 
xx2=*+1
		lda #0			; y
		clc
		adc #CenterX
		tay
		lda #Char
		sta ($f0),y

;-----

yy3=*+1
		lda #0			; x
		clc
		adc #CenterY
		tax
		lda screenlo,x
		sta $f0
		lda screenhi,x
		sta $f1 
xx3=*+1
		lda #0			; y
		clc
		adc #CenterX
		tay
		lda #Char
		sta ($f0),y
;----

yy4=*+1
		lda #0			; x
		clc
		adc #CenterY
		tax
		lda screenlo,x
		sta $f0
		lda screenhi,x
		sta $f1 
xx4=*+1
		lda #0			; y
		clc
		adc #CenterX
		tay
		lda #Char
		sta ($f0),y
		
;----

yy5=*+1
		lda #0			; x
		clc
		adc #CenterY
		tax
		lda screenlo,x
		sta $f0
		lda screenhi,x
		sta $f1 
xx5=*+1
		lda #0			; y
		clc
		adc #CenterX
		tay
		lda #Char
		sta ($f0),y

yy6=*+1
		lda #0			; x
		clc
		adc #CenterY
		tax
		lda screenlo,x
		sta $f0
		lda screenhi,x
		sta $f1 
xx6=*+1
		lda #0			; y
		clc
		adc #CenterX
		tay
		lda #Char
		sta ($f0),y

yy7=*+1
		lda #0			; x
		clc
		adc #CenterY
		tax
		lda screenlo,x
		sta $f0
		lda screenhi,x
		sta $f1 
xx7=*+1
		lda #0			; y
		clc
		adc #CenterX
		tay
		lda #Char
		sta ($f0),y

yy8=*+1
		lda #0			; x
		clc
		adc #CenterY
		tax
		lda screenlo,x
		sta $f0
		lda screenhi,x
		sta $f1 
xx8=*+1
		lda #0			; y
		clc
		adc #CenterX
		tay
		lda #Char
		sta ($f0),y

		rts

.align 256

screenlo

		.byte <$0400+(40*0), <$0400+(40*1),<$0400+(40*2),<$0400+(40*3),<$0400+(40*4),<$0400+(40*5)
		.byte <$0400+(40*6), <$0400+(40*7),<$0400+(40*8),<$0400+(40*9),<$0400+(40*10),<$0400+(40*11)
		.byte <$0400+(40*12), <$0400+(40*13),<$0400+(40*14),<$0400+(40*15),<$0400+(40*16),<$0400+(40*17)
		.byte <$0400+(40*18), <$0400+(40*19),<$0400+(40*20),<$0400+(40*21),<$0400+(40*22),<$0400+(40*23)
		.byte <$0400+(40*24)
      	
screenhi
		.byte >$0400+(40*0), >$0400+(40*1),>$0400+(40*2),>$0400+(40*3),>$0400+(40*4),>$0400+(40*5)
		.byte >$0400+(40*6), >$0400+(40*7),>$0400+(40*8),>$0400+(40*9),>$0400+(40*10),>$0400+(40*11)
		.byte >$0400+(40*12), >$0400+(40*13),>$0400+(40*14),>$0400+(40*15),>$0400+(40*16),>$0400+(40*17)
		.byte >$0400+(40*18), >$0400+(40*19),>$0400+(40*20),>$0400+(40*21),>$0400+(40*22),>$0400+(40*23)
		.byte >$0400+(40*24)
	
2010-01-13 19:06
JackAsser

Registered: Jun 2002
Posts: 2014
Nice! :D

FYI, the end part fade out circle in Andropolis uses this algorithm aswell, but highly table based for speed up and also with a hard-coded center point for further speed up.
2010-01-13 20:03
Oswald

Registered: Apr 2002
Posts: 5094
.. and the 5 circles part in Desert Dream uses it aswell to generate speedcode which plots circles in various sizes. ;)
2010-01-13 20:18
JackAsser

Registered: Jun 2002
Posts: 2014
Quote: .. and the 5 circles part in Desert Dream uses it aswell to generate speedcode which plots circles in various sizes. ;)

Aha! Nice trivia! :D
2010-01-13 22:01
Ninja

Registered: Jan 2002
Posts: 411
4k-3 uses a heavily size-optimized version for creating the ornament-thingie which "drops" off the screen. (Ahh, the old days...)
2010-01-14 03:03
A Life in Hell
Account closed

Registered: May 2002
Posts: 204
nice one :)

btw, like everyone else I also used such a routine to generate speedcode for the circle picture drawing thingy in chase that feeling. this is much better than mine tho!
RefreshSubscribe to this thread:

You need to be logged in to post in the forum.

Search the forum:
Search   for   in  
All times are CET.
Search CSDb
Advanced
Users Online
grennouille
MWR/Visdom
deetsay
WVL/Xenon
rime/Fancy Rats
Mike
Peacemaker/CENSOR/Hi..
Mibri/ATL^MSL^PRX
icon/The Silents, Sp..
csabanw
Guests online: 122
Top Demos
1 Next Level  (9.7)
2 13:37  (9.7)
3 Mojo  (9.7)
4 Coma Light 13  (9.6)
5 Edge of Disgrace  (9.6)
6 What Is The Matrix 2  (9.6)
7 The Demo Coder  (9.6)
8 Uncensored  (9.6)
9 Comaland 100%  (9.6)
10 Wonderland XIV  (9.6)
Top onefile Demos
1 No Listen  (9.6)
2 Layers  (9.6)
3 Cubic Dream  (9.6)
4 Party Elk 2  (9.6)
5 Copper Booze  (9.6)
6 Dawnfall V1.1  (9.5)
7 Rainbow Connection  (9.5)
8 Onscreen 5k  (9.5)
9 Morph  (9.5)
10 Libertongo  (9.5)
Top Groups
1 Performers  (9.3)
2 Booze Design  (9.3)
3 Oxyron  (9.3)
4 Censor Design  (9.3)
5 Triad  (9.3)
Top Organizers
1 Burglar  (9.9)
2 Sixx  (9.8)
3 hedning  (9.7)
4 Irata  (9.7)
5 Tim  (9.7)

Home - Disclaimer
Copyright © No Name 2001-2024
Page generated in: 0.045 sec.