Library for Waveshare 2.7" tri-colour e-Paper display.

Dependents:   ePD_2_7b_example

Committer:
mdroberts1243
Date:
Fri Dec 06 23:24:50 2019 +0000
Revision:
0:9e6a8e3cd8de
First commit of code modified from Kanjia examples for 2.13" Waveshare example

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mdroberts1243 0:9e6a8e3cd8de 1 /**
mdroberts1243 0:9e6a8e3cd8de 2 * @filename : epdpaint.cpp
mdroberts1243 0:9e6a8e3cd8de 3 * @brief : Paint tools
mdroberts1243 0:9e6a8e3cd8de 4 * @author : Yehui from Waveshare
mdroberts1243 0:9e6a8e3cd8de 5 *
mdroberts1243 0:9e6a8e3cd8de 6 * Copyright (C) Waveshare September 9 2017
mdroberts1243 0:9e6a8e3cd8de 7 *
mdroberts1243 0:9e6a8e3cd8de 8 * Permission is hereby granted, free of charge, to any person obtaining a copy
mdroberts1243 0:9e6a8e3cd8de 9 * of this software and associated documnetation files (the "Software"), to deal
mdroberts1243 0:9e6a8e3cd8de 10 * in the Software without restriction, including without limitation the rights
mdroberts1243 0:9e6a8e3cd8de 11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
mdroberts1243 0:9e6a8e3cd8de 12 * copies of the Software, and to permit persons to whom the Software is
mdroberts1243 0:9e6a8e3cd8de 13 * furished to do so, subject to the following conditions:
mdroberts1243 0:9e6a8e3cd8de 14 *
mdroberts1243 0:9e6a8e3cd8de 15 * The above copyright notice and this permission notice shall be included in
mdroberts1243 0:9e6a8e3cd8de 16 * all copies or substantial portions of the Software.
mdroberts1243 0:9e6a8e3cd8de 17 *
mdroberts1243 0:9e6a8e3cd8de 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
mdroberts1243 0:9e6a8e3cd8de 19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
mdroberts1243 0:9e6a8e3cd8de 20 * FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
mdroberts1243 0:9e6a8e3cd8de 21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
mdroberts1243 0:9e6a8e3cd8de 22 * LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
mdroberts1243 0:9e6a8e3cd8de 23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
mdroberts1243 0:9e6a8e3cd8de 24 * THE SOFTWARE.
mdroberts1243 0:9e6a8e3cd8de 25 */
mdroberts1243 0:9e6a8e3cd8de 26
mdroberts1243 0:9e6a8e3cd8de 27 //#include <avr/pgmspace.h> by JH1PJL for mbed-os5 Apr.28,2019
mdroberts1243 0:9e6a8e3cd8de 28 #include "epdpaint.h"
mdroberts1243 0:9e6a8e3cd8de 29
mdroberts1243 0:9e6a8e3cd8de 30 Paint::Paint(unsigned char* image, int width, int height)
mdroberts1243 0:9e6a8e3cd8de 31 {
mdroberts1243 0:9e6a8e3cd8de 32 this->rotate = ROTATE_0;
mdroberts1243 0:9e6a8e3cd8de 33 this->image = image;
mdroberts1243 0:9e6a8e3cd8de 34 /* 1 byte = 8 pixels, so the width should be the multiple of 8 */
mdroberts1243 0:9e6a8e3cd8de 35 this->width = width % 8 ? width + 8 - (width % 8) : width;
mdroberts1243 0:9e6a8e3cd8de 36 this->height = height;
mdroberts1243 0:9e6a8e3cd8de 37 }
mdroberts1243 0:9e6a8e3cd8de 38
mdroberts1243 0:9e6a8e3cd8de 39 Paint::~Paint()
mdroberts1243 0:9e6a8e3cd8de 40 {
mdroberts1243 0:9e6a8e3cd8de 41 }
mdroberts1243 0:9e6a8e3cd8de 42
mdroberts1243 0:9e6a8e3cd8de 43 /**
mdroberts1243 0:9e6a8e3cd8de 44 * @brief: clear the image
mdroberts1243 0:9e6a8e3cd8de 45 */
mdroberts1243 0:9e6a8e3cd8de 46 void Paint::Clear(int colored)
mdroberts1243 0:9e6a8e3cd8de 47 {
mdroberts1243 0:9e6a8e3cd8de 48 for (int x = 0; x < this->width; x++) {
mdroberts1243 0:9e6a8e3cd8de 49 for (int y = 0; y < this->height; y++) {
mdroberts1243 0:9e6a8e3cd8de 50 DrawAbsolutePixel(x, y, colored);
mdroberts1243 0:9e6a8e3cd8de 51 }
mdroberts1243 0:9e6a8e3cd8de 52 }
mdroberts1243 0:9e6a8e3cd8de 53 }
mdroberts1243 0:9e6a8e3cd8de 54
mdroberts1243 0:9e6a8e3cd8de 55 /**
mdroberts1243 0:9e6a8e3cd8de 56 * @brief: this draws a pixel by absolute coordinates.
mdroberts1243 0:9e6a8e3cd8de 57 * this function won't be affected by the rotate parameter.
mdroberts1243 0:9e6a8e3cd8de 58 */
mdroberts1243 0:9e6a8e3cd8de 59 void Paint::DrawAbsolutePixel(int x, int y, int colored)
mdroberts1243 0:9e6a8e3cd8de 60 {
mdroberts1243 0:9e6a8e3cd8de 61 if (x < 0 || x >= this->width || y < 0 || y >= this->height) {
mdroberts1243 0:9e6a8e3cd8de 62 return;
mdroberts1243 0:9e6a8e3cd8de 63 }
mdroberts1243 0:9e6a8e3cd8de 64 if (IF_INVERT_COLOR) {
mdroberts1243 0:9e6a8e3cd8de 65 if (colored) {
mdroberts1243 0:9e6a8e3cd8de 66 image[(x + y * this->width) / 8] |= 0x80 >> (x % 8);
mdroberts1243 0:9e6a8e3cd8de 67 } else {
mdroberts1243 0:9e6a8e3cd8de 68 image[(x + y * this->width) / 8] &= ~(0x80 >> (x % 8));
mdroberts1243 0:9e6a8e3cd8de 69 }
mdroberts1243 0:9e6a8e3cd8de 70 } else {
mdroberts1243 0:9e6a8e3cd8de 71 if (colored) {
mdroberts1243 0:9e6a8e3cd8de 72 image[(x + y * this->width) / 8] &= ~(0x80 >> (x % 8));
mdroberts1243 0:9e6a8e3cd8de 73 } else {
mdroberts1243 0:9e6a8e3cd8de 74 image[(x + y * this->width) / 8] |= 0x80 >> (x % 8);
mdroberts1243 0:9e6a8e3cd8de 75 }
mdroberts1243 0:9e6a8e3cd8de 76 }
mdroberts1243 0:9e6a8e3cd8de 77 }
mdroberts1243 0:9e6a8e3cd8de 78
mdroberts1243 0:9e6a8e3cd8de 79 /**
mdroberts1243 0:9e6a8e3cd8de 80 * @brief: Getters and Setters
mdroberts1243 0:9e6a8e3cd8de 81 */
mdroberts1243 0:9e6a8e3cd8de 82 unsigned char* Paint::GetImage(void)
mdroberts1243 0:9e6a8e3cd8de 83 {
mdroberts1243 0:9e6a8e3cd8de 84 return this->image;
mdroberts1243 0:9e6a8e3cd8de 85 }
mdroberts1243 0:9e6a8e3cd8de 86
mdroberts1243 0:9e6a8e3cd8de 87 int Paint::GetWidth(void)
mdroberts1243 0:9e6a8e3cd8de 88 {
mdroberts1243 0:9e6a8e3cd8de 89 return this->width;
mdroberts1243 0:9e6a8e3cd8de 90 }
mdroberts1243 0:9e6a8e3cd8de 91
mdroberts1243 0:9e6a8e3cd8de 92 void Paint::SetWidth(int width)
mdroberts1243 0:9e6a8e3cd8de 93 {
mdroberts1243 0:9e6a8e3cd8de 94 this->width = width % 8 ? width + 8 - (width % 8) : width;
mdroberts1243 0:9e6a8e3cd8de 95 }
mdroberts1243 0:9e6a8e3cd8de 96
mdroberts1243 0:9e6a8e3cd8de 97 int Paint::GetHeight(void)
mdroberts1243 0:9e6a8e3cd8de 98 {
mdroberts1243 0:9e6a8e3cd8de 99 return this->height;
mdroberts1243 0:9e6a8e3cd8de 100 }
mdroberts1243 0:9e6a8e3cd8de 101
mdroberts1243 0:9e6a8e3cd8de 102 void Paint::SetHeight(int height)
mdroberts1243 0:9e6a8e3cd8de 103 {
mdroberts1243 0:9e6a8e3cd8de 104 this->height = height;
mdroberts1243 0:9e6a8e3cd8de 105 }
mdroberts1243 0:9e6a8e3cd8de 106
mdroberts1243 0:9e6a8e3cd8de 107 int Paint::GetRotate(void)
mdroberts1243 0:9e6a8e3cd8de 108 {
mdroberts1243 0:9e6a8e3cd8de 109 return this->rotate;
mdroberts1243 0:9e6a8e3cd8de 110 }
mdroberts1243 0:9e6a8e3cd8de 111
mdroberts1243 0:9e6a8e3cd8de 112 void Paint::SetRotate(int rotate)
mdroberts1243 0:9e6a8e3cd8de 113 {
mdroberts1243 0:9e6a8e3cd8de 114 this->rotate = rotate;
mdroberts1243 0:9e6a8e3cd8de 115 }
mdroberts1243 0:9e6a8e3cd8de 116
mdroberts1243 0:9e6a8e3cd8de 117 /**
mdroberts1243 0:9e6a8e3cd8de 118 * @brief: this draws a pixel by the coordinates
mdroberts1243 0:9e6a8e3cd8de 119 */
mdroberts1243 0:9e6a8e3cd8de 120 void Paint::DrawPixel(int x, int y, int colored)
mdroberts1243 0:9e6a8e3cd8de 121 {
mdroberts1243 0:9e6a8e3cd8de 122 int point_temp;
mdroberts1243 0:9e6a8e3cd8de 123 if (this->rotate == ROTATE_0) {
mdroberts1243 0:9e6a8e3cd8de 124 if(x < 0 || x >= this->width || y < 0 || y >= this->height) {
mdroberts1243 0:9e6a8e3cd8de 125 return;
mdroberts1243 0:9e6a8e3cd8de 126 }
mdroberts1243 0:9e6a8e3cd8de 127 DrawAbsolutePixel(x, y, colored);
mdroberts1243 0:9e6a8e3cd8de 128 } else if (this->rotate == ROTATE_90) {
mdroberts1243 0:9e6a8e3cd8de 129 if(x < 0 || x >= this->height || y < 0 || y >= this->width) {
mdroberts1243 0:9e6a8e3cd8de 130 return;
mdroberts1243 0:9e6a8e3cd8de 131 }
mdroberts1243 0:9e6a8e3cd8de 132 point_temp = x;
mdroberts1243 0:9e6a8e3cd8de 133 x = this->width - y;
mdroberts1243 0:9e6a8e3cd8de 134 y = point_temp;
mdroberts1243 0:9e6a8e3cd8de 135 DrawAbsolutePixel(x, y, colored);
mdroberts1243 0:9e6a8e3cd8de 136 } else if (this->rotate == ROTATE_180) {
mdroberts1243 0:9e6a8e3cd8de 137 if(x < 0 || x >= this->width || y < 0 || y >= this->height) {
mdroberts1243 0:9e6a8e3cd8de 138 return;
mdroberts1243 0:9e6a8e3cd8de 139 }
mdroberts1243 0:9e6a8e3cd8de 140 x = this->width - x;
mdroberts1243 0:9e6a8e3cd8de 141 y = this->height - y;
mdroberts1243 0:9e6a8e3cd8de 142 DrawAbsolutePixel(x, y, colored);
mdroberts1243 0:9e6a8e3cd8de 143 } else if (this->rotate == ROTATE_270) {
mdroberts1243 0:9e6a8e3cd8de 144 if(x < 0 || x >= this->height || y < 0 || y >= this->width) {
mdroberts1243 0:9e6a8e3cd8de 145 return;
mdroberts1243 0:9e6a8e3cd8de 146 }
mdroberts1243 0:9e6a8e3cd8de 147 point_temp = x;
mdroberts1243 0:9e6a8e3cd8de 148 x = y;
mdroberts1243 0:9e6a8e3cd8de 149 y = this->height - point_temp;
mdroberts1243 0:9e6a8e3cd8de 150 DrawAbsolutePixel(x, y, colored);
mdroberts1243 0:9e6a8e3cd8de 151 }
mdroberts1243 0:9e6a8e3cd8de 152 }
mdroberts1243 0:9e6a8e3cd8de 153
mdroberts1243 0:9e6a8e3cd8de 154 /**
mdroberts1243 0:9e6a8e3cd8de 155 * @brief: this draws a charactor on the frame buffer but not refresh
mdroberts1243 0:9e6a8e3cd8de 156 */
mdroberts1243 0:9e6a8e3cd8de 157 void Paint::DrawCharAt(int x, int y, char ascii_char, sFONT* font, int colored)
mdroberts1243 0:9e6a8e3cd8de 158 {
mdroberts1243 0:9e6a8e3cd8de 159 int i, j;
mdroberts1243 0:9e6a8e3cd8de 160 unsigned int char_offset = (ascii_char - ' ') * font->Height * (font->Width / 8 + (font->Width % 8 ? 1 : 0));
mdroberts1243 0:9e6a8e3cd8de 161 const unsigned char* ptr = &font->table[char_offset];
mdroberts1243 0:9e6a8e3cd8de 162
mdroberts1243 0:9e6a8e3cd8de 163 for (j = 0; j < font->Height; j++) {
mdroberts1243 0:9e6a8e3cd8de 164 for (i = 0; i < font->Width; i++) {
mdroberts1243 0:9e6a8e3cd8de 165 if (*ptr & (0x80 >> (i % 8))) {
mdroberts1243 0:9e6a8e3cd8de 166 DrawPixel(x + i, y + j, colored);
mdroberts1243 0:9e6a8e3cd8de 167 }
mdroberts1243 0:9e6a8e3cd8de 168 if (i % 8 == 7) {
mdroberts1243 0:9e6a8e3cd8de 169 ptr++;
mdroberts1243 0:9e6a8e3cd8de 170 }
mdroberts1243 0:9e6a8e3cd8de 171 }
mdroberts1243 0:9e6a8e3cd8de 172 if (font->Width % 8 != 0) {
mdroberts1243 0:9e6a8e3cd8de 173 ptr++;
mdroberts1243 0:9e6a8e3cd8de 174 }
mdroberts1243 0:9e6a8e3cd8de 175 }
mdroberts1243 0:9e6a8e3cd8de 176 }
mdroberts1243 0:9e6a8e3cd8de 177
mdroberts1243 0:9e6a8e3cd8de 178 /**
mdroberts1243 0:9e6a8e3cd8de 179 * @brief: this displays a string on the frame buffer but not refresh
mdroberts1243 0:9e6a8e3cd8de 180 */
mdroberts1243 0:9e6a8e3cd8de 181 void Paint::DrawStringAt(int x, int y, const char* text, sFONT* font, int colored)
mdroberts1243 0:9e6a8e3cd8de 182 {
mdroberts1243 0:9e6a8e3cd8de 183 const char* p_text = text;
mdroberts1243 0:9e6a8e3cd8de 184 unsigned int counter = 0;
mdroberts1243 0:9e6a8e3cd8de 185 int refcolumn = x;
mdroberts1243 0:9e6a8e3cd8de 186
mdroberts1243 0:9e6a8e3cd8de 187 /* Send the string character by character on EPD */
mdroberts1243 0:9e6a8e3cd8de 188 while (*p_text != 0) {
mdroberts1243 0:9e6a8e3cd8de 189 /* Display one character on EPD */
mdroberts1243 0:9e6a8e3cd8de 190 DrawCharAt(refcolumn, y, *p_text, font, colored);
mdroberts1243 0:9e6a8e3cd8de 191 /* Decrement the column position by 16 */
mdroberts1243 0:9e6a8e3cd8de 192 refcolumn += font->Width;
mdroberts1243 0:9e6a8e3cd8de 193 /* Point on the next character */
mdroberts1243 0:9e6a8e3cd8de 194 p_text++;
mdroberts1243 0:9e6a8e3cd8de 195 counter++;
mdroberts1243 0:9e6a8e3cd8de 196 }
mdroberts1243 0:9e6a8e3cd8de 197 }
mdroberts1243 0:9e6a8e3cd8de 198
mdroberts1243 0:9e6a8e3cd8de 199 /**
mdroberts1243 0:9e6a8e3cd8de 200 * @brief: this draws a line on the frame buffer
mdroberts1243 0:9e6a8e3cd8de 201 */
mdroberts1243 0:9e6a8e3cd8de 202 void Paint::DrawLine(int x0, int y0, int x1, int y1, int colored)
mdroberts1243 0:9e6a8e3cd8de 203 {
mdroberts1243 0:9e6a8e3cd8de 204 /* Bresenham algorithm */
mdroberts1243 0:9e6a8e3cd8de 205 int dx = x1 - x0 >= 0 ? x1 - x0 : x0 - x1;
mdroberts1243 0:9e6a8e3cd8de 206 int sx = x0 < x1 ? 1 : -1;
mdroberts1243 0:9e6a8e3cd8de 207 int dy = y1 - y0 <= 0 ? y1 - y0 : y0 - y1;
mdroberts1243 0:9e6a8e3cd8de 208 int sy = y0 < y1 ? 1 : -1;
mdroberts1243 0:9e6a8e3cd8de 209 int err = dx + dy;
mdroberts1243 0:9e6a8e3cd8de 210
mdroberts1243 0:9e6a8e3cd8de 211 while((x0 != x1) && (y0 != y1)) {
mdroberts1243 0:9e6a8e3cd8de 212 DrawPixel(x0, y0, colored);
mdroberts1243 0:9e6a8e3cd8de 213 if (2 * err >= dy) {
mdroberts1243 0:9e6a8e3cd8de 214 err += dy;
mdroberts1243 0:9e6a8e3cd8de 215 x0 += sx;
mdroberts1243 0:9e6a8e3cd8de 216 }
mdroberts1243 0:9e6a8e3cd8de 217 if (2 * err <= dx) {
mdroberts1243 0:9e6a8e3cd8de 218 err += dx;
mdroberts1243 0:9e6a8e3cd8de 219 y0 += sy;
mdroberts1243 0:9e6a8e3cd8de 220 }
mdroberts1243 0:9e6a8e3cd8de 221 }
mdroberts1243 0:9e6a8e3cd8de 222 }
mdroberts1243 0:9e6a8e3cd8de 223
mdroberts1243 0:9e6a8e3cd8de 224 /**
mdroberts1243 0:9e6a8e3cd8de 225 * @brief: this draws a horizontal line on the frame buffer
mdroberts1243 0:9e6a8e3cd8de 226 */
mdroberts1243 0:9e6a8e3cd8de 227 void Paint::DrawHorizontalLine(int x, int y, int line_width, int colored)
mdroberts1243 0:9e6a8e3cd8de 228 {
mdroberts1243 0:9e6a8e3cd8de 229 int i;
mdroberts1243 0:9e6a8e3cd8de 230 for (i = x; i < x + line_width; i++) {
mdroberts1243 0:9e6a8e3cd8de 231 DrawPixel(i, y, colored);
mdroberts1243 0:9e6a8e3cd8de 232 }
mdroberts1243 0:9e6a8e3cd8de 233 }
mdroberts1243 0:9e6a8e3cd8de 234
mdroberts1243 0:9e6a8e3cd8de 235 /**
mdroberts1243 0:9e6a8e3cd8de 236 * @brief: this draws a vertical line on the frame buffer
mdroberts1243 0:9e6a8e3cd8de 237 */
mdroberts1243 0:9e6a8e3cd8de 238 void Paint::DrawVerticalLine(int x, int y, int line_height, int colored)
mdroberts1243 0:9e6a8e3cd8de 239 {
mdroberts1243 0:9e6a8e3cd8de 240 int i;
mdroberts1243 0:9e6a8e3cd8de 241 for (i = y; i < y + line_height; i++) {
mdroberts1243 0:9e6a8e3cd8de 242 DrawPixel(x, i, colored);
mdroberts1243 0:9e6a8e3cd8de 243 }
mdroberts1243 0:9e6a8e3cd8de 244 }
mdroberts1243 0:9e6a8e3cd8de 245
mdroberts1243 0:9e6a8e3cd8de 246 /**
mdroberts1243 0:9e6a8e3cd8de 247 * @brief: this draws a rectangle
mdroberts1243 0:9e6a8e3cd8de 248 */
mdroberts1243 0:9e6a8e3cd8de 249 void Paint::DrawRectangle(int x0, int y0, int x1, int y1, int colored)
mdroberts1243 0:9e6a8e3cd8de 250 {
mdroberts1243 0:9e6a8e3cd8de 251 int min_x, min_y, max_x, max_y;
mdroberts1243 0:9e6a8e3cd8de 252 min_x = x1 > x0 ? x0 : x1;
mdroberts1243 0:9e6a8e3cd8de 253 max_x = x1 > x0 ? x1 : x0;
mdroberts1243 0:9e6a8e3cd8de 254 min_y = y1 > y0 ? y0 : y1;
mdroberts1243 0:9e6a8e3cd8de 255 max_y = y1 > y0 ? y1 : y0;
mdroberts1243 0:9e6a8e3cd8de 256
mdroberts1243 0:9e6a8e3cd8de 257 DrawHorizontalLine(min_x, min_y, max_x - min_x + 1, colored);
mdroberts1243 0:9e6a8e3cd8de 258 DrawHorizontalLine(min_x, max_y, max_x - min_x + 1, colored);
mdroberts1243 0:9e6a8e3cd8de 259 DrawVerticalLine(min_x, min_y, max_y - min_y + 1, colored);
mdroberts1243 0:9e6a8e3cd8de 260 DrawVerticalLine(max_x, min_y, max_y - min_y + 1, colored);
mdroberts1243 0:9e6a8e3cd8de 261 }
mdroberts1243 0:9e6a8e3cd8de 262
mdroberts1243 0:9e6a8e3cd8de 263 /**
mdroberts1243 0:9e6a8e3cd8de 264 * @brief: this draws a filled rectangle
mdroberts1243 0:9e6a8e3cd8de 265 */
mdroberts1243 0:9e6a8e3cd8de 266 void Paint::DrawFilledRectangle(int x0, int y0, int x1, int y1, int colored)
mdroberts1243 0:9e6a8e3cd8de 267 {
mdroberts1243 0:9e6a8e3cd8de 268 int min_x, min_y, max_x, max_y;
mdroberts1243 0:9e6a8e3cd8de 269 int i;
mdroberts1243 0:9e6a8e3cd8de 270 min_x = x1 > x0 ? x0 : x1;
mdroberts1243 0:9e6a8e3cd8de 271 max_x = x1 > x0 ? x1 : x0;
mdroberts1243 0:9e6a8e3cd8de 272 min_y = y1 > y0 ? y0 : y1;
mdroberts1243 0:9e6a8e3cd8de 273 max_y = y1 > y0 ? y1 : y0;
mdroberts1243 0:9e6a8e3cd8de 274
mdroberts1243 0:9e6a8e3cd8de 275 for (i = min_x; i <= max_x; i++) {
mdroberts1243 0:9e6a8e3cd8de 276 DrawVerticalLine(i, min_y, max_y - min_y + 1, colored);
mdroberts1243 0:9e6a8e3cd8de 277 }
mdroberts1243 0:9e6a8e3cd8de 278 }
mdroberts1243 0:9e6a8e3cd8de 279
mdroberts1243 0:9e6a8e3cd8de 280 /**
mdroberts1243 0:9e6a8e3cd8de 281 * @brief: this draws a circle
mdroberts1243 0:9e6a8e3cd8de 282 */
mdroberts1243 0:9e6a8e3cd8de 283 void Paint::DrawCircle(int x, int y, int radius, int colored)
mdroberts1243 0:9e6a8e3cd8de 284 {
mdroberts1243 0:9e6a8e3cd8de 285 /* Bresenham algorithm */
mdroberts1243 0:9e6a8e3cd8de 286 int x_pos = -radius;
mdroberts1243 0:9e6a8e3cd8de 287 int y_pos = 0;
mdroberts1243 0:9e6a8e3cd8de 288 int err = 2 - 2 * radius;
mdroberts1243 0:9e6a8e3cd8de 289 int e2;
mdroberts1243 0:9e6a8e3cd8de 290
mdroberts1243 0:9e6a8e3cd8de 291 do {
mdroberts1243 0:9e6a8e3cd8de 292 DrawPixel(x - x_pos, y + y_pos, colored);
mdroberts1243 0:9e6a8e3cd8de 293 DrawPixel(x + x_pos, y + y_pos, colored);
mdroberts1243 0:9e6a8e3cd8de 294 DrawPixel(x + x_pos, y - y_pos, colored);
mdroberts1243 0:9e6a8e3cd8de 295 DrawPixel(x - x_pos, y - y_pos, colored);
mdroberts1243 0:9e6a8e3cd8de 296 e2 = err;
mdroberts1243 0:9e6a8e3cd8de 297 if (e2 <= y_pos) {
mdroberts1243 0:9e6a8e3cd8de 298 err += ++y_pos * 2 + 1;
mdroberts1243 0:9e6a8e3cd8de 299 if(-x_pos == y_pos && e2 <= x_pos) {
mdroberts1243 0:9e6a8e3cd8de 300 e2 = 0;
mdroberts1243 0:9e6a8e3cd8de 301 }
mdroberts1243 0:9e6a8e3cd8de 302 }
mdroberts1243 0:9e6a8e3cd8de 303 if (e2 > x_pos) {
mdroberts1243 0:9e6a8e3cd8de 304 err += ++x_pos * 2 + 1;
mdroberts1243 0:9e6a8e3cd8de 305 }
mdroberts1243 0:9e6a8e3cd8de 306 } while (x_pos <= 0);
mdroberts1243 0:9e6a8e3cd8de 307 }
mdroberts1243 0:9e6a8e3cd8de 308
mdroberts1243 0:9e6a8e3cd8de 309 /**
mdroberts1243 0:9e6a8e3cd8de 310 * @brief: this draws a filled circle
mdroberts1243 0:9e6a8e3cd8de 311 */
mdroberts1243 0:9e6a8e3cd8de 312 void Paint::DrawFilledCircle(int x, int y, int radius, int colored)
mdroberts1243 0:9e6a8e3cd8de 313 {
mdroberts1243 0:9e6a8e3cd8de 314 /* Bresenham algorithm */
mdroberts1243 0:9e6a8e3cd8de 315 int x_pos = -radius;
mdroberts1243 0:9e6a8e3cd8de 316 int y_pos = 0;
mdroberts1243 0:9e6a8e3cd8de 317 int err = 2 - 2 * radius;
mdroberts1243 0:9e6a8e3cd8de 318 int e2;
mdroberts1243 0:9e6a8e3cd8de 319
mdroberts1243 0:9e6a8e3cd8de 320 do {
mdroberts1243 0:9e6a8e3cd8de 321 DrawPixel(x - x_pos, y + y_pos, colored);
mdroberts1243 0:9e6a8e3cd8de 322 DrawPixel(x + x_pos, y + y_pos, colored);
mdroberts1243 0:9e6a8e3cd8de 323 DrawPixel(x + x_pos, y - y_pos, colored);
mdroberts1243 0:9e6a8e3cd8de 324 DrawPixel(x - x_pos, y - y_pos, colored);
mdroberts1243 0:9e6a8e3cd8de 325 DrawHorizontalLine(x + x_pos, y + y_pos, 2 * (-x_pos) + 1, colored);
mdroberts1243 0:9e6a8e3cd8de 326 DrawHorizontalLine(x + x_pos, y - y_pos, 2 * (-x_pos) + 1, colored);
mdroberts1243 0:9e6a8e3cd8de 327 e2 = err;
mdroberts1243 0:9e6a8e3cd8de 328 if (e2 <= y_pos) {
mdroberts1243 0:9e6a8e3cd8de 329 err += ++y_pos * 2 + 1;
mdroberts1243 0:9e6a8e3cd8de 330 if(-x_pos == y_pos && e2 <= x_pos) {
mdroberts1243 0:9e6a8e3cd8de 331 e2 = 0;
mdroberts1243 0:9e6a8e3cd8de 332 }
mdroberts1243 0:9e6a8e3cd8de 333 }
mdroberts1243 0:9e6a8e3cd8de 334 if(e2 > x_pos) {
mdroberts1243 0:9e6a8e3cd8de 335 err += ++x_pos * 2 + 1;
mdroberts1243 0:9e6a8e3cd8de 336 }
mdroberts1243 0:9e6a8e3cd8de 337 } while(x_pos <= 0);
mdroberts1243 0:9e6a8e3cd8de 338 }
mdroberts1243 0:9e6a8e3cd8de 339
mdroberts1243 0:9e6a8e3cd8de 340 /* END OF FILE */
mdroberts1243 0:9e6a8e3cd8de 341
mdroberts1243 0:9e6a8e3cd8de 342
mdroberts1243 0:9e6a8e3cd8de 343
mdroberts1243 0:9e6a8e3cd8de 344
mdroberts1243 0:9e6a8e3cd8de 345
mdroberts1243 0:9e6a8e3cd8de 346
mdroberts1243 0:9e6a8e3cd8de 347
mdroberts1243 0:9e6a8e3cd8de 348
mdroberts1243 0:9e6a8e3cd8de 349
mdroberts1243 0:9e6a8e3cd8de 350
mdroberts1243 0:9e6a8e3cd8de 351
mdroberts1243 0:9e6a8e3cd8de 352
mdroberts1243 0:9e6a8e3cd8de 353
mdroberts1243 0:9e6a8e3cd8de 354
mdroberts1243 0:9e6a8e3cd8de 355
mdroberts1243 0:9e6a8e3cd8de 356
mdroberts1243 0:9e6a8e3cd8de 357
mdroberts1243 0:9e6a8e3cd8de 358
mdroberts1243 0:9e6a8e3cd8de 359
mdroberts1243 0:9e6a8e3cd8de 360
mdroberts1243 0:9e6a8e3cd8de 361
mdroberts1243 0:9e6a8e3cd8de 362
mdroberts1243 0:9e6a8e3cd8de 363