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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Bitmap/Midpoint circle algorithm step by step in the Wren 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 Wren programming language

Source code in the wren programming language

import "graphics" for Canvas, Color, ImageData
import "dome" for Window

class MidpointCircle {
    construct new(width, height) {
        Window.title = "Midpoint Circle"
        Window.resize(width, height)
        Canvas.resize(width, height)
        _w = width
        _h = height
        _bmp = ImageData.create("midpoint circle", width, height)
    }

    init() {
        fill(Color.pink)
        drawCircle(200, 200, 100, Color.black)
        drawCircle(200, 200,  50, Color.white)
        _bmp.draw(0, 0)
    }

    fill(col) {
        for (x in 0..._w) {
            for (y in 0..._h) pset(x, y, col)
        }
    }

    drawCircle(centerX, centerY, radius, circleColor) {
        var d = ((5 - radius * 4)/4).truncate
        var x = 0
        var y = radius
        while (true) {
            pset(centerX + x, centerY + y, circleColor)
            pset(centerX + x, centerY - y, circleColor)
            pset(centerX - x, centerY + y, circleColor)
            pset(centerX - x, centerY - y, circleColor)
            pset(centerX + y, centerY + x, circleColor)
            pset(centerX + y, centerY - x, circleColor)
            pset(centerX - y, centerY + x, circleColor)
            pset(centerX - y, centerY - x, circleColor)
            if (d < 0) {
                d = d + 2 * x + 1
            } else {
                d = d + 2 * (x - y) + 1
                y = y - 1
            }
            x = x + 1
            if (x > y) break
        }
    }

    pset(x, y, col) { _bmp.pset(x, y, col) }

    pget(x, y) { _bmp.pget(x, y) }
 
    update() {}

    draw(alpha) {}
}

var Game = MidpointCircle.new(400, 400)


  

You may also check:How to resolve the algorithm Luhn test of credit card numbers step by step in the Mathematica/Wolfram Language programming language
You may also check:How to resolve the algorithm Anagrams step by step in the Scheme programming language
You may also check:How to resolve the algorithm Sort an integer array step by step in the Perl programming language
You may also check:How to resolve the algorithm Color of a screen pixel step by step in the Java programming language
You may also check:How to resolve the algorithm Terminal control/Dimensions step by step in the Applesoft BASIC programming language