Jump to content

Ellipse drawing algorithm


Recommended Posts

Hi!

I'm trying to script an ellipse drawing algorithm in AutoIT for learning purposes.
I use an edit control to "draw" the ellipse with characters.

The code i have works kinda good but i would like to see if others around here know a better algorithm to do it..
I would like to be able to give the x, y and width, height arguments to the function and not have to start at the middle point and draw the ellipse around it.

side note, the ellipse glitches when it has to be 2 pixels high too..

 

func ellipsePlotPoints ($xc,$yc, $x,$y)

    pixel ($xc + $x, $yc + $y)
    pixel ($xc - $x, $yc + $y)
    pixel ($xc + $x, $yc - $y)
    pixel ($xc - $x, $yc - $y)

endfunc

func ellipse($xc,$yc, $a,$b)

    local $a2 = $a * $a
    local $b2 = $b * $b
    local $twoa2 = 2 * $a2
    local $twob2 = 2 * $b2
    local $p
    local $x = 0
    local $y = $b
    local $px = 0
    local $py = $twoa2 * $y

    ; Plot the initial point in each quadrant.
    ellipsePlotPoints ($xc,$yc, $x,$y)

    ; Region 1
    $p = int($b2 - ($a2 * $b) + (0.25 * $a2))
    while ($px < $py)
        $x+=1
        $px += $twob2
        if ($p < 0) then
            $p += $b2 + $px
        else
            $y-=1
            $py -= $twoa2
            $p += $b2 + $px - $py
        endif
        ellipsePlotPoints ($xc,$yc, $x,$y)
    wend

    ; Region 2
    $p = int ($b2 * ($x+0.5) * ($x+0.5) + $a2 * ($y-1) * ($y-1) - $a2 * $b2)
    while ($y > 0)
        $y-=1
        $py -= $twoa2
        if ($p > 0) then
            $p += $a2 - $py
        else
            $x+=1
            $px += $twob2
            $p += $a2 - $py + $px
        endif
        ellipsePlotPoints ($xc,$yc, $x,$y)
    wend

endfunc

 

Link to comment
Share on other sites

here (https://www.autoitscript.com/forum/topic/181014-a-naive-question/?do=findComment&comment=1299979) you can find a simple implementation of the Bresenham's Algorithm, translated in AutoIt, to plot lines, circles, ellipses and Bézier curves plotted pixel by pixel (or character by character on a text box as well if you prefer)

 

Edited by Chimp

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...