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