How to resolve the algorithm Bitmap/Midpoint circle algorithm step by step in the Tcl programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Bitmap/Midpoint circle algorithm step by step in the Tcl programming language

Table of Contents

Problem Statement

Using the data storage type defined on this page for raster images, write an implementation of the midpoint circle algorithm   (also known as Bresenham's circle algorithm). (definition on Wikipedia).

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Bitmap/Midpoint circle algorithm step by step in the Tcl programming language

Source code in the tcl programming language

package require Tcl 8.5
package require Tk

proc drawCircle {image colour point radius} {
    lassign $point x0 y0

    setPixel $image $colour [list $x0 [expr {$y0 + $radius}]]
    setPixel $image $colour [list $x0 [expr {$y0 - $radius}]]
    setPixel $image $colour [list [expr {$x0 + $radius}] $y0]
    setPixel $image $colour [list [expr {$x0 - $radius}] $y0]

    set f [expr {1 - $radius}]
    set ddF_x 1
    set ddF_y [expr {-2 * $radius}]
    set x 0
    set y $radius
    
    while {$x < $y} {
        assert {$ddF_x == 2 * $x + 1}
        assert {$ddF_y == -2 * $y}
        assert {$f == $x*$x + $y*$y - $radius*$radius + 2*$x - $y + 1}
        if {$f >= 0} {
            incr y -1
            incr ddF_y 2
            incr f $ddF_y
        }
        incr x
        incr ddF_x 2
        incr f $ddF_x
        setPixel $image $colour [list [expr {$x0 + $x}] [expr {$y0 + $y}]]
        setPixel $image $colour [list [expr {$x0 - $x}] [expr {$y0 + $y}]]
        setPixel $image $colour [list [expr {$x0 + $x}] [expr {$y0 - $y}]]
        setPixel $image $colour [list [expr {$x0 - $x}] [expr {$y0 - $y}]]
        setPixel $image $colour [list [expr {$x0 + $y}] [expr {$y0 + $x}]]
        setPixel $image $colour [list [expr {$x0 - $y}] [expr {$y0 + $x}]]
        setPixel $image $colour [list [expr {$x0 + $y}] [expr {$y0 - $x}]]
        setPixel $image $colour [list [expr {$x0 - $y}] [expr {$y0 - $x}]]

    }
}

# create the image and display it
set img [newImage 200 100]
label .l -image $img
pack .l
 
fill $img black
drawCircle $img blue {100 50} 49


  

You may also check:How to resolve the algorithm Loops/While step by step in the ActionScript programming language
You may also check:How to resolve the algorithm Loops/For with a specified step step by step in the Elixir programming language
You may also check:How to resolve the algorithm Strip whitespace from a string/Top and tail step by step in the Ada programming language
You may also check:How to resolve the algorithm Euler's sum of powers conjecture step by step in the C++ programming language
You may also check:How to resolve the algorithm Quickselect algorithm step by step in the Factor programming language