Post by scalion on Jan 18, 2022 19:23:06 GMT 1
My source :
This particular method of tracing a circle uses integers (and not floats) and above all allows to cover the entire tiling of pixels. This is very useful if you need to scan a surface starting from a central point, as is the case in image analysis in the context of image recognition.
Given that at the moment I am doing a little research in artificial intelligence, precisely in deep learning, this function will surely be of great help to me.
Given that at the moment I am doing a little research in artificial intelligence, precisely in deep learning, this function will surely be of great help to me.
The source code file : Tracer Cercle Andres.G32 (3.1 KB)
Same thing as text :
Global Long R
FullW 1
Global Long X, Y
Global Long CenterX = _X / 2
Global Long CenterY = _Y / 2
Do
GetCercleAndres(CenterX, CenterY, R, X, Y, True)
While GetCercleAndres(CenterX, CenterY, R, X, Y)
Plot X, Y
Wend
Inc R
If R > _Y / 2
R = 0
RGBColor Rnd * &hffffff
EndIf
PeekEvent
Loop Until Me Is Nothing
Function GetCercleAndres(CenterX As Long, CenterY As Long, Radius As Long, ByRef XResult As Long, ByRef YResult As Long, Optional Start As Boolean = False) As Boolean
' Author : Nicolas Rey
' Code GFA-Basic : 18/01/2022
' This function is designed to image recognition for define pixel direction
' It permit to find fastly the smallest distance around pixel
' that indicate the perpendicular direction is the owner segment
' using the Eric Andres method.
' Usage :
' Start a sequence with
' GetCercleAndres(CenterX, CenterY, Radius, X, Y, True)
' Get each pixels of the circle :
' PixelFound = GetCercleAndres(CenterX, CenterY, Radius, X1, Y1)
' PixelFound = GetCercleAndres(CenterX, CenterY, Radius, X2, Y2)
' etc...
' until PixelFound = False.
' Each pair of pixels are opposites on circle
' that mean they have same direction ;).
Static Long D , X, Y, K
If Start = True
D = Radius - 1
X = 0
Y = Radius - 1
K = 0
Return True
EndIf
If Y >= X
If K = 0
XResult = CenterX + X, YResult = CenterY + Y
Else If K = 1
XResult = CenterX - X, YResult = CenterY - Y
Else If K = 2
XResult = CenterX + Y, YResult = CenterY + X
Else If K = 3
XResult = CenterX - Y, YResult = CenterY - X
Else If K = 4
XResult = CenterX - X, YResult = CenterY + Y
Else If K = 5
XResult = CenterX + X, YResult = CenterY - Y
Else If K = 6
XResult = CenterX - Y, YResult = CenterY + X
Else
XResult = CenterX + Y, YResult = CenterY - X
If D >= 2 * X
D -= 2 * X + 1
Inc X
Else If D < 2 * (Radius - Y)
D += 2 * Y - 1
Dec Y
Else
D += 2 * (Y - X - 1)
Dec Y
Inc X
EndIf
EndIf
K = (K + 1) % 8
Return True
Else
Return False
EndIf
EndFunc
Proc CercleAndres(CenterX As Long, CenterY As Long, Radius As Long)
' Trace circle using an Eric Andres method.
' Particularity : use integer and can cover all pixels (no holes).
' This code is adapted from examples found here :
' https://fr.wikipedia.org/wiki/Algorithme_de_trac%C3%A9_de_cercle_d%27Andres
Local Long d = Radius - 1 , X = 0, Y = Radius - 1
While Y >= X
Plot CenterX + X, CenterY + Y
Plot CenterX + Y, CenterY + X
Plot CenterX - X, CenterY + Y
Plot CenterX - Y, CenterY + X
Plot CenterX + X, CenterY - Y
Plot CenterX + Y, CenterY - X
Plot CenterX - X, CenterY - Y
Plot CenterX - Y, CenterY - X
If d >= 2 * X
d -= 2 * X + 1
Inc X
Else If d < 2 * (Radius - Y)
d += 2 * Y - 1
Dec Y
Else
d += 2 * (Y - X - 1)
Dec Y
Inc X
EndIf
Wend
EndProc