Sketches
Anti-Aliased Curve Algorithms
home top contents previous up next

#This is a Ruby equivalent of Java program
#Run it in command line as "ruby NoSqrt.rb"
#There are some artifacts like camel-like vars ... enough good for first try ...


    I=255          #Intensity range. Determines algorithms' accuracy.

    #No precalculation is required.
    #Calculation of the 1/8 of circle will take
    # R/sq(2) steps = R/sq(2)*(3mult+1div+8add) ~ 1.4R(3mult+1div)
    # All operations are of integer type.
        
    def drawArch( r )
       r2=r*r
       y=0
       x=r

       bb=x*x
       xTop=x+1
       tt=xTop*xTop

       while y<x  do
           ee=r2-y*y
           ll=ee-bb
           uu=tt-ee
           if ll<0  #We had Wu's lemma before: if( dnew < d ) x--
        	   xTop=x
        	   x-=1
        	   tt=bb
        	   uu=-ll
        	   bb=x*x
               ll=ee-bb
           end
            u=I*uu/(uu+ll)
           
           #good for debug:
           puts ("x=#{x} y=#{y} E=#{ee} B=#{bb} T=#{tt} L=#{ll} U=#{uu} u=#{u} v=#{I-u}")
           
           #These two statements are not a part of the algorithm:
           #Each language, OS, or framework has own ways to put a "pixel".
           #putpixel(x,      y,     u,  doDemo)
           #putpixel(xTop,   y, (I-u), !doDemo)
           
           y+=1
       end #while    
    end
   
    
    #Test:
    rr=20            #Radius.     
    drawArch(rr)



Copyright (C) 2011 Konstantin Kirillov. MIT License