Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make circle drawing more scalable #33

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 21 additions & 46 deletions src/screen.jl
Original file line number Diff line number Diff line change
Expand Up @@ -159,54 +159,29 @@ end
sdl_colors(c::Colorant) = sdl_colors(convert(ARGB{Colors.FixedPointNumbers.Normed{UInt8,8}}, c))
sdl_colors(c::ARGB) = Int.(reinterpret.((red(c), green(c), blue(c), alpha(c))))

# improved circle drawing algorithm. slower but fills completely. needs optimization
function draw(s::Screen, circle::Circle, c::Colorant=colorant"black"; fill=false)
# define the center and needed sides of circle
centerX = Cint(circle.x)
centerY = Cint(circle.y)
int_rad = Cint(circle.r)
left = centerX - int_rad
top = centerY - int_rad

SDL2.SetRenderDrawColor(
s.renderer,
sdl_colors(c)...,
)

# we consider a grid with sides equal to the circle's diameter
for x in left:centerX
for y in top:centerY

# for each pixel in the top left quadrant of the grid we measure the distance from the center.
dist = sqrt( (centerX - x)^2 + (centerY - y)^2 )

# if it is close to the circle's radius it and all associated points in the other quadrants are colored in.
if (dist <= circle.r + 0.5 && dist >= circle.r - 0.5)
rel_x = centerX - x
rel_y = centerY - y

quad1 = (x , y )
quad2 = (centerX + rel_x, y )
quad3 = (x , centerY + rel_y)
quad4 = (quad2[1] , quad3[2] )

SDL2.RenderDrawPoint(s.renderer, quad1[1], quad1[2])
SDL2.RenderDrawPoint(s.renderer, quad2[1], quad2[2])
SDL2.RenderDrawPoint(s.renderer, quad3[1], quad3[2])
SDL2.RenderDrawPoint(s.renderer, quad4[1], quad4[2])

# if we are told to fill in the circle we draw lines between all of the quadrants to completely fill the circle
if (fill == true)
SDL2.RenderDrawLine(s.renderer, quad1[1], quad1[2], quad2[1], quad2[2])
SDL2.RenderDrawLine(s.renderer, quad2[1], quad2[2], quad4[1], quad4[2])
SDL2.RenderDrawLine(s.renderer, quad4[1], quad4[2], quad3[1], quad3[2])
SDL2.RenderDrawLine(s.renderer, quad3[1], quad3[2], quad1[1], quad1[2])
end
end

end
SDL2.SetRenderDrawColor(s.renderer, sdl_colors(c)...)
r = Cint(circle.r)
o = Cint.([circle.x; circle.y])

n = ceil(π * r / 2)
if fill
for j = 0:n
x = round(Cint, r * cos(j / n * π / 2))
y = round(Cint, r * sin(j / n * π / 2))
SDL2.RenderDrawLine(s.renderer, o[1] + x, o[2] + y, o[1] + x, o[2] - y)
SDL2.RenderDrawLine(s.renderer, o[1] - x, o[2] + y, o[1] - x, o[2] - y)
end

else
for j = 0:n
x = round(Cint, r * cos(j / n * π / 2))
y = round(Cint, r * sin(j / n * π / 2))
SDL2.RenderDrawPoint(s.renderer, o[1] + x, o[2] + y)
SDL2.RenderDrawPoint(s.renderer, o[1] - x, o[2] + y)
SDL2.RenderDrawPoint(s.renderer, o[1] + x, o[2] - y)
SDL2.RenderDrawPoint(s.renderer, o[1] - x, o[2] - y)
end
end
end

rect(x::Rect) = x
Expand Down
116 changes: 116 additions & 0 deletions test/circleperformance.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
# This files checks the new way to draw a circle versus the old way

using SimpleDirectMediaLayer
const SDL2 = SimpleDirectMediaLayer

circles = Vector{Int}[]
for j = 1:10
push!(circles, [10 + 4 * j; 50; j])
push!(circles, [100 + 30 * j; 500; 25 * j])
end

function newdraw(ren, circle; fill=false)
r = Cint(circle[3])
o = Cint.(circle[1:2])

n = ceil(π * r / 2)
if fill
for j = 0:n
x = round(Cint, r * cos(j / n * π / 2))
y = round(Cint, r * sin(j / n * π / 2))
SDL2.RenderDrawLine(ren, o[1] + x, o[2] + y, o[1] + x, o[2] - y)
SDL2.RenderDrawLine(ren, o[1] - x, o[2] + y, o[1] - x, o[2] - y)
end
else
for j = 0:n
x = round(Cint, r * cos(j / n * π / 2))
y = round(Cint, r * sin(j / n * π / 2))
SDL2.RenderDrawPoint(ren, o[1] + x, o[2] + y)
SDL2.RenderDrawPoint(ren, o[1] - x, o[2] + y)
SDL2.RenderDrawPoint(ren, o[1] + x, o[2] - y)
SDL2.RenderDrawPoint(ren, o[1] - x, o[2] - y)
end
end
end

function olddraw(ren, circle; fill=false)
# define the center and needed sides of circle
centerX = Cint(circle[1])
centerY = Cint(circle[2])
int_rad = Cint(circle[3])
left = centerX - int_rad
top = centerY - int_rad

# we consider a grid with sides equal to the circle's diameter
for x in left:centerX
for y in top:centerY

# for each pixel in the top left quadrant of the grid we measure the distance from the center.
dist = sqrt( (centerX - x)^2 + (centerY - y)^2 )

# if it is close to the circle's radius it and all associated points in the other quadrants are colored in.
if (dist <= int_rad + 0.5 && dist >= int_rad - 0.5)
rel_x = centerX - x
rel_y = centerY - y

quad1 = (x , y )
quad2 = (centerX + rel_x, y )
quad3 = (x , centerY + rel_y)
quad4 = (quad2[1] , quad3[2] )

SDL2.RenderDrawPoint(ren, quad1[1], quad1[2])
SDL2.RenderDrawPoint(ren, quad2[1], quad2[2])
SDL2.RenderDrawPoint(ren, quad3[1], quad3[2])
SDL2.RenderDrawPoint(ren, quad4[1], quad4[2])

# if we are told to fill in the circle we draw lines between all of the quadrants to completely fill the circle
if (fill == true)
SDL2.RenderDrawLine(ren, quad1[1], quad1[2], quad2[1], quad2[2])
SDL2.RenderDrawLine(ren, quad2[1], quad2[2], quad4[1], quad4[2])
SDL2.RenderDrawLine(ren, quad4[1], quad4[2], quad3[1], quad3[2])
SDL2.RenderDrawLine(ren, quad3[1], quad3[2], quad1[1], quad1[2])
end
end

end
end

end

function myinit()
SDL2.init()
win = SDL2.CreateWindow("",
Int32(0), Int32(0), Int32(800), Int32(600), UInt32(SDL2.WINDOW_SHOWN)
)

renderer = SDL2.CreateRenderer(win,
Int32(-1),
UInt32(SDL2.RENDERER_ACCELERATED | SDL2.RENDERER_PRESENTVSYNC)
)

SDL2.SetRenderDrawColor(renderer, 0, 0, 0, 255)
return renderer
end


function testold(renderer, circles, N, boolfill)
SDL2.SetRenderDrawColor(renderer, 255, 255, 255, 255)
for j = 1:N
SDL2.RenderClear(renderer)
for k = 1:length(circles)
olddraw(renderer, circles[k], fill=boolfill)
end
end
SDL2.Quit()
end

function testnew(renderer, circles, N, boolfill)
SDL2.SetRenderDrawColor(renderer, 255, 255, 255, 255)
for j = 1:N
SDL2.RenderClear(renderer)
for k = 1:length(circles)
newdraw(renderer, circles[k], fill=boolfill)
end
end
SDL2.Quit()
end