November 24, 2020, 06:45:29 AM

### Author Topic: [bmx] Draw Hollow Circle Pixel by Pixel with Integer Math by ImaginaryHuman [ 1+ years ago ]  (Read 1103 times)

#### BlitzBot

• Jr. Member
• Posts: 1
##### [bmx] Draw Hollow Circle Pixel by Pixel with Integer Math by ImaginaryHuman [ 1+ years ago ]
« on: June 29, 2017, 12:28:38 AM »
Title : Draw Hollow Circle Pixel
Author : Pixel with Integer Math by ImaginaryHuman
Posted : 1+ years ago

Description : This routine is based on the mid-point algorithm to draw an unfilled circle one pixel at a time. It does everything with integer math, not a float in sight. It also doesn't use any trigonometry so no time-consuming cos() or sin().

Code :
Code: BlitzMax
1. 'Midpoint Circle algorithm
2.
3. Strict
4. Graphics 640,480,0
5.
6. Local xCenter:Int=320
7. Local yCenter:Int=240
9. Local p,x,y:Int
10. Repeat
11.         Cls
12.         If MouseDown(1)
13.                 xCenter=MouseX()
14.                 yCenter=MouseY()
15.         EndIf
17.         x=0
19.         Plot xCenter+x,yCenter+y
20.         Plot xCenter-x,yCenter+y
21.         Plot xCenter+x,yCenter-y
22.         Plot xCenter-x,yCenter-y
23.         Plot xCenter+y,yCenter+x
24.         Plot xCenter-y,yCenter+x
25.         Plot xCenter+y,yCenter-x
26.         Plot xCenter-y,yCenter-x
28.         While x<y
29.                 If p<0
30.                         x:+1
31.                 Else
32.                         x:+1
33.                         y:-1
34.                 EndIf
35.                 If p<0
36.                         p=p+(x Shl 1)+1
37.                 Else
38.                         p=p+((x-y) Shl 1)+1
39.                 EndIf
40.                 Plot xCenter+x,yCenter+y
41.                 Plot xCenter-x,yCenter+y
42.                 Plot xCenter+x,yCenter-y
43.                 Plot xCenter-x,yCenter-y
44.                 Plot xCenter+y,yCenter+x
45.                 Plot xCenter-y,yCenter+x
46.                 Plot xCenter+y,yCenter-x
47.                 Plot xCenter-y,yCenter-x
48.         Wend
49.         Flip
50. Until KeyHit(KEY_ESCAPE)
51. End

Rck(Posted 1+ years ago)

Code: [Select]
`;Midpoint Circle algorithmGraphics 640,480,16,2Local xCenter% = 320Local yCenter% = 240Local radius%Local p%, x%, y%Repeat WaitEvent()        Cls         If MouseDown(1)            xCenter = MouseX()            yCenter = MouseY()        EndIf        radius = Abs(xCenter - MouseX())        x = 0        y = radius msStart = MilliSecs()        Plot xCenter + x, yCenter + y        Plot xCenter - x, yCenter + y        Plot xCenter + x, yCenter - y        Plot xCenter - x, yCenter - y        Plot xCenter + y, yCenter + x        Plot xCenter - y, yCenter + x        Plot xCenter + y, yCenter - x        Plot xCenter - y, yCenter - x        p = 1 - radius        While x < y                If p < 0                    x = x + 1                Else                    x = x + 1                    y = y - 1                EndIf                If p < 0                    p = p + (x Shl 1) + 1                Else                    p = p + ((x - y) Shl 1) + 1                EndIf                Plot xCenter + x, yCenter + y                Plot xCenter - x, yCenter + y                Plot xCenter + x, yCenter - y                Plot xCenter - x, yCenter - y                Plot xCenter + y, yCenter + x                Plot xCenter - y, yCenter + x                Plot xCenter + y, yCenter - x                Plot xCenter - y, yCenter - x        Wend drawTime = MilliSecs() - msStart Text 20, 20, "Radius " + radius Text 20, 30, "Draw time " + drawTime + " ms"         Flip(False)Until KeyHit(1)`Port to BlitzPlus

ImaginaryHuman(Posted 1+ years ago)

Groovy!

Jesse(Posted 1+ years ago)

Is anybody having problems running the BMax sample, I can't get it to work right. All I get is a dot that moves along the y axis and relative to the movement of the mouse along the x axis. Some how the plot command is not working right with my version of BMax I am running version 1.14. I replaced plot with oval(just for trouble shooting) and it works fine.

ImaginaryHuman(Posted 1+ years ago)

Must be your version of max or something.

Oddball(Posted 1+ years ago)

Just what I needed. Thanks.

Andres(Posted 1+ years ago)

A bit slow, tried WritePixel?

Oddball(Posted 1+ years ago)

<div class="quote"> A bit slow, tried WritePixel? </div>WritePixel only writes to a PixMap. I needed to draw direct to a canvas, and it seems fast enough for my needs. [/i]