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: 1568
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: 1989
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: 5017
.. 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: 1989
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: 404
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
Menace/Spaceballs
zscs
Hairdog/BOOM!^Dream
Guests online: 128
Top Demos
1 Next Level  (9.8)
2 Mojo  (9.7)
3 Coma Light 13  (9.7)
4 Edge of Disgrace  (9.6)
5 Comaland 100%  (9.6)
6 No Bounds  (9.6)
7 Uncensored  (9.6)
8 Wonderland XIV  (9.6)
9 Memento Mori  (9.6)
10 Bromance  (9.5)
Top onefile Demos
1 It's More Fun to Com..  (9.7)
2 Party Elk 2  (9.7)
3 Cubic Dream  (9.6)
4 Copper Booze  (9.5)
5 TRSAC, Gabber & Pebe..  (9.5)
6 Rainbow Connection  (9.5)
7 Wafer Demo  (9.5)
8 Dawnfall V1.1  (9.5)
9 Quadrants  (9.5)
10 Daah, Those Acid Pil..  (9.5)
Top Groups
1 Oxyron  (9.3)
2 Nostalgia  (9.3)
3 Booze Design  (9.3)
4 Censor Design  (9.3)
5 Crest  (9.3)
Top Graphicians
1 Sulevi  (10)
2 Mirage  (9.8)
3 Lobo  (9.7)
4 Mikael  (9.7)
5 Archmage  (9.7)

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