Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of gr-peach-opencv-project-sd-card by
graphicFramework/mGraphic.cpp@166:240bc5a0f42a, 2017-07-21 (annotated)
- Committer:
- thedo
- Date:
- Fri Jul 21 01:26:02 2017 +0000
- Revision:
- 166:240bc5a0f42a
gr-peach-opencv-project-sd-card
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| thedo | 166:240bc5a0f42a | 1 | #include "mGraphic.hpp" |
| thedo | 166:240bc5a0f42a | 2 | int counter =0; |
| thedo | 166:240bc5a0f42a | 3 | static void togle_led(DigitalOut led) |
| thedo | 166:240bc5a0f42a | 4 | { |
| thedo | 166:240bc5a0f42a | 5 | led = 1; |
| thedo | 166:240bc5a0f42a | 6 | wait(0.1); |
| thedo | 166:240bc5a0f42a | 7 | led = 0; |
| thedo | 166:240bc5a0f42a | 8 | wait(0.1); |
| thedo | 166:240bc5a0f42a | 9 | } |
| thedo | 166:240bc5a0f42a | 10 | |
| thedo | 166:240bc5a0f42a | 11 | graphicFrameworkCanvas::graphicFrameworkCanvas(uint8_t * bufDraw, uint16_t w,uint16_t h, |
| thedo | 166:240bc5a0f42a | 12 | int8_t bpp, int8_t drawPoint, int8_t sizeHeader) |
| thedo | 166:240bc5a0f42a | 13 | { |
| thedo | 166:240bc5a0f42a | 14 | mBufLcd = bufDraw; |
| thedo | 166:240bc5a0f42a | 15 | mWidth = w; |
| thedo | 166:240bc5a0f42a | 16 | mHeight = h; |
| thedo | 166:240bc5a0f42a | 17 | mBpp = bpp; |
| thedo | 166:240bc5a0f42a | 18 | mDrawPoint = drawPoint; |
| thedo | 166:240bc5a0f42a | 19 | mSizeHeaderImg = sizeHeader; |
| thedo | 166:240bc5a0f42a | 20 | } |
| thedo | 166:240bc5a0f42a | 21 | |
| thedo | 166:240bc5a0f42a | 22 | graphicFrameworkCanvas::~graphicFrameworkCanvas() |
| thedo | 166:240bc5a0f42a | 23 | { |
| thedo | 166:240bc5a0f42a | 24 | } |
| thedo | 166:240bc5a0f42a | 25 | |
| thedo | 166:240bc5a0f42a | 26 | void graphicFrameworkCanvas::draw_pixel(uint8_t * p_buf, int id, int x, int y) |
| thedo | 166:240bc5a0f42a | 27 | { |
| thedo | 166:240bc5a0f42a | 28 | int idx_base; |
| thedo | 166:240bc5a0f42a | 29 | int wk_idx; |
| thedo | 166:240bc5a0f42a | 30 | int i; |
| thedo | 166:240bc5a0f42a | 31 | int j; |
| thedo | 166:240bc5a0f42a | 32 | uint8_t coller_pix[mBpp]; /* ARGB4444 */ |
| thedo | 166:240bc5a0f42a | 33 | |
| thedo | 166:240bc5a0f42a | 34 | /* A coordinate in the upper left is calculated from a central coordinate. */ |
| thedo | 166:240bc5a0f42a | 35 | if ((x - (mDrawPoint / 2)) >= 0) { |
| thedo | 166:240bc5a0f42a | 36 | x -= (mDrawPoint / 2); |
| thedo | 166:240bc5a0f42a | 37 | } |
| thedo | 166:240bc5a0f42a | 38 | if (x > (mWidth - mDrawPoint)) { |
| thedo | 166:240bc5a0f42a | 39 | x = (mWidth - mDrawPoint); |
| thedo | 166:240bc5a0f42a | 40 | } |
| thedo | 166:240bc5a0f42a | 41 | if ((y - (mDrawPoint / 2)) >= 0) { |
| thedo | 166:240bc5a0f42a | 42 | y -= (mDrawPoint / 2); |
| thedo | 166:240bc5a0f42a | 43 | } |
| thedo | 166:240bc5a0f42a | 44 | if (y > (mHeight - mDrawPoint)) { |
| thedo | 166:240bc5a0f42a | 45 | y = (mHeight - mDrawPoint); |
| thedo | 166:240bc5a0f42a | 46 | } |
| thedo | 166:240bc5a0f42a | 47 | idx_base = (x + (mWidth * y)) * mBpp; |
| thedo | 166:240bc5a0f42a | 48 | |
| thedo | 166:240bc5a0f42a | 49 | /* Select color */ |
| thedo | 166:240bc5a0f42a | 50 | if (id == 0) { |
| thedo | 166:240bc5a0f42a | 51 | /* Blue */ |
| thedo | 166:240bc5a0f42a | 52 | coller_pix[0] = 0x4F; /* 4:Green 4:Blue */ |
| thedo | 166:240bc5a0f42a | 53 | coller_pix[1] = 0xF8; /* 4:Alpha 4:Red */ |
| thedo | 166:240bc5a0f42a | 54 | } else { |
| thedo | 166:240bc5a0f42a | 55 | /* Pink */ |
| thedo | 166:240bc5a0f42a | 56 | coller_pix[0] = 0xFF; /* 4:Green 4:Blue */ |
| thedo | 166:240bc5a0f42a | 57 | coller_pix[1] = 0x0F; /* 4:Alpha 4:Red */ |
| thedo | 166:240bc5a0f42a | 58 | } |
| thedo | 166:240bc5a0f42a | 59 | |
| thedo | 166:240bc5a0f42a | 60 | /* Drawing */ |
| thedo | 166:240bc5a0f42a | 61 | for (i = 0; i < mDrawPoint; i++) { |
| thedo | 166:240bc5a0f42a | 62 | wk_idx = idx_base + (mWidth * mBpp * i); |
| thedo | 166:240bc5a0f42a | 63 | for (j = 0; j < mDrawPoint; j++) { |
| thedo | 166:240bc5a0f42a | 64 | p_buf[wk_idx++] = coller_pix[0]; |
| thedo | 166:240bc5a0f42a | 65 | p_buf[wk_idx++] = coller_pix[1]; |
| thedo | 166:240bc5a0f42a | 66 | } |
| thedo | 166:240bc5a0f42a | 67 | } |
| thedo | 166:240bc5a0f42a | 68 | } |
| thedo | 166:240bc5a0f42a | 69 | |
| thedo | 166:240bc5a0f42a | 70 | void graphicFrameworkCanvas::draw_pixel(uint8_t * p_buf, int id, |
| thedo | 166:240bc5a0f42a | 71 | int x, int y,uint8_t *color) |
| thedo | 166:240bc5a0f42a | 72 | { |
| thedo | 166:240bc5a0f42a | 73 | int idx_base; |
| thedo | 166:240bc5a0f42a | 74 | int wk_idx; |
| thedo | 166:240bc5a0f42a | 75 | int i; |
| thedo | 166:240bc5a0f42a | 76 | int j; |
| thedo | 166:240bc5a0f42a | 77 | |
| thedo | 166:240bc5a0f42a | 78 | /* A coordinate in the upper left is calculated from a central coordinate. */ |
| thedo | 166:240bc5a0f42a | 79 | if ((x - (mDrawPoint / 2)) >= 0) { |
| thedo | 166:240bc5a0f42a | 80 | x -= (mDrawPoint / 2); |
| thedo | 166:240bc5a0f42a | 81 | } |
| thedo | 166:240bc5a0f42a | 82 | if (x > (mWidth - mDrawPoint)) { |
| thedo | 166:240bc5a0f42a | 83 | x = (mWidth - mDrawPoint); |
| thedo | 166:240bc5a0f42a | 84 | } |
| thedo | 166:240bc5a0f42a | 85 | if ((y - (mDrawPoint / 2)) >= 0) { |
| thedo | 166:240bc5a0f42a | 86 | y -= (mDrawPoint / 2); |
| thedo | 166:240bc5a0f42a | 87 | } |
| thedo | 166:240bc5a0f42a | 88 | if (y > (mHeight - mDrawPoint)) { |
| thedo | 166:240bc5a0f42a | 89 | y = (mHeight - mDrawPoint); |
| thedo | 166:240bc5a0f42a | 90 | } |
| thedo | 166:240bc5a0f42a | 91 | idx_base = (x + (mWidth * y)) * mBpp; |
| thedo | 166:240bc5a0f42a | 92 | |
| thedo | 166:240bc5a0f42a | 93 | /* Drawing */ |
| thedo | 166:240bc5a0f42a | 94 | for (i = 0; i < mDrawPoint; i++) { |
| thedo | 166:240bc5a0f42a | 95 | wk_idx = idx_base + (mWidth * mBpp * i); |
| thedo | 166:240bc5a0f42a | 96 | for (j = 0; j < mDrawPoint; j++) { |
| thedo | 166:240bc5a0f42a | 97 | p_buf[wk_idx++] = color[0]; |
| thedo | 166:240bc5a0f42a | 98 | p_buf[wk_idx++] = color[1]; |
| thedo | 166:240bc5a0f42a | 99 | } |
| thedo | 166:240bc5a0f42a | 100 | } |
| thedo | 166:240bc5a0f42a | 101 | } |
| thedo | 166:240bc5a0f42a | 102 | |
| thedo | 166:240bc5a0f42a | 103 | void graphicFrameworkCanvas::drawImage(uint8_t * _img, int _x, int _y) |
| thedo | 166:240bc5a0f42a | 104 | { |
| thedo | 166:240bc5a0f42a | 105 | uint8_t *p_buf = _img; |
| thedo | 166:240bc5a0f42a | 106 | uint8_t coller_pix[mBpp]; |
| thedo | 166:240bc5a0f42a | 107 | uint16_t w = (_img[1] << 8) | _img[0]; |
| thedo | 166:240bc5a0f42a | 108 | uint16_t h = (_img[3] << 8) | _img[2]; |
| thedo | 166:240bc5a0f42a | 109 | uint8_t bpp = _img[4]; |
| thedo | 166:240bc5a0f42a | 110 | |
| thedo | 166:240bc5a0f42a | 111 | p_buf+=mSizeHeaderImg; |
| thedo | 166:240bc5a0f42a | 112 | |
| thedo | 166:240bc5a0f42a | 113 | for(int i = 0;i < (int)h*w*bpp; i+=2) |
| thedo | 166:240bc5a0f42a | 114 | { |
| thedo | 166:240bc5a0f42a | 115 | int pos_x = (i/2)%w; |
| thedo | 166:240bc5a0f42a | 116 | int pos_y = (i/2)/w; |
| thedo | 166:240bc5a0f42a | 117 | coller_pix[0] = p_buf[i]; |
| thedo | 166:240bc5a0f42a | 118 | coller_pix[1] = p_buf[i+1]; |
| thedo | 166:240bc5a0f42a | 119 | draw_pixel(mBufLcd, 0, pos_x + _x, pos_y + _y,coller_pix); |
| thedo | 166:240bc5a0f42a | 120 | } |
| thedo | 166:240bc5a0f42a | 121 | } |
| thedo | 166:240bc5a0f42a | 122 | |
| thedo | 166:240bc5a0f42a | 123 | void graphicFrameworkCanvas::drawRect(int x, int y, int width, int height, |
| thedo | 166:240bc5a0f42a | 124 | uint8_t *color) |
| thedo | 166:240bc5a0f42a | 125 | { |
| thedo | 166:240bc5a0f42a | 126 | for(int k = 0; k < width;k++) |
| thedo | 166:240bc5a0f42a | 127 | { |
| thedo | 166:240bc5a0f42a | 128 | draw_pixel(mBufLcd, 0, x + k, y, color); |
| thedo | 166:240bc5a0f42a | 129 | draw_pixel(mBufLcd, 0, x + k, y + height,color); |
| thedo | 166:240bc5a0f42a | 130 | } |
| thedo | 166:240bc5a0f42a | 131 | |
| thedo | 166:240bc5a0f42a | 132 | for(int k = 0; k < height;k++) |
| thedo | 166:240bc5a0f42a | 133 | { |
| thedo | 166:240bc5a0f42a | 134 | draw_pixel(mBufLcd, 0, x, y + k, color); |
| thedo | 166:240bc5a0f42a | 135 | draw_pixel(mBufLcd, 0, x + width, y + k,color); |
| thedo | 166:240bc5a0f42a | 136 | } |
| thedo | 166:240bc5a0f42a | 137 | } |
| thedo | 166:240bc5a0f42a | 138 | |
| thedo | 166:240bc5a0f42a | 139 | void graphicFrameworkCanvas::draw_pixel(int x, int y,uint8_t *color) |
| thedo | 166:240bc5a0f42a | 140 | { |
| thedo | 166:240bc5a0f42a | 141 | int idx_base; |
| thedo | 166:240bc5a0f42a | 142 | int wk_idx; |
| thedo | 166:240bc5a0f42a | 143 | int i; |
| thedo | 166:240bc5a0f42a | 144 | int j; |
| thedo | 166:240bc5a0f42a | 145 | |
| thedo | 166:240bc5a0f42a | 146 | /* A coordinate in the upper left is calculated from a central coordinate. */ |
| thedo | 166:240bc5a0f42a | 147 | if ((x - (mDrawPoint / 2)) >= 0) { |
| thedo | 166:240bc5a0f42a | 148 | x -= (mDrawPoint / 2); |
| thedo | 166:240bc5a0f42a | 149 | } |
| thedo | 166:240bc5a0f42a | 150 | if (x > (mWidth - mDrawPoint)) { |
| thedo | 166:240bc5a0f42a | 151 | x = (mWidth - mDrawPoint); |
| thedo | 166:240bc5a0f42a | 152 | } |
| thedo | 166:240bc5a0f42a | 153 | if ((y - (mDrawPoint / 2)) >= 0) { |
| thedo | 166:240bc5a0f42a | 154 | y -= (mDrawPoint / 2); |
| thedo | 166:240bc5a0f42a | 155 | } |
| thedo | 166:240bc5a0f42a | 156 | if (y > (mHeight - mDrawPoint)) { |
| thedo | 166:240bc5a0f42a | 157 | y = (mHeight - mDrawPoint); |
| thedo | 166:240bc5a0f42a | 158 | } |
| thedo | 166:240bc5a0f42a | 159 | idx_base = (x + (mWidth * y)) * mBpp; |
| thedo | 166:240bc5a0f42a | 160 | |
| thedo | 166:240bc5a0f42a | 161 | /* Drawing */ |
| thedo | 166:240bc5a0f42a | 162 | for (i = 0; i < mDrawPoint; i++) { |
| thedo | 166:240bc5a0f42a | 163 | wk_idx = idx_base + (mWidth * mBpp * i); |
| thedo | 166:240bc5a0f42a | 164 | for (j = 0; j < mDrawPoint; j++) { |
| thedo | 166:240bc5a0f42a | 165 | mBufLcd[wk_idx++] = color[0]; |
| thedo | 166:240bc5a0f42a | 166 | mBufLcd[wk_idx++] = color[1]; |
| thedo | 166:240bc5a0f42a | 167 | } |
| thedo | 166:240bc5a0f42a | 168 | } |
| thedo | 166:240bc5a0f42a | 169 | return; |
| thedo | 166:240bc5a0f42a | 170 | } |
| thedo | 166:240bc5a0f42a | 171 | void graphicFrameworkCanvas::drawLineNormal(int x1, int y1, int x2, int y2, uint8_t* color) |
| thedo | 166:240bc5a0f42a | 172 | { |
| thedo | 166:240bc5a0f42a | 173 | int tmp_x = 0; |
| thedo | 166:240bc5a0f42a | 174 | int tmp_y = 0; |
| thedo | 166:240bc5a0f42a | 175 | int i = 0; |
| thedo | 166:240bc5a0f42a | 176 | |
| thedo | 166:240bc5a0f42a | 177 | if (x1 == x2) // Case equation x = m. |
| thedo | 166:240bc5a0f42a | 178 | { |
| thedo | 166:240bc5a0f42a | 179 | if (y1 > y2) // Swap 2 points |
| thedo | 166:240bc5a0f42a | 180 | { |
| thedo | 166:240bc5a0f42a | 181 | //ptTemp = pt1; |
| thedo | 166:240bc5a0f42a | 182 | tmp_x = x1; |
| thedo | 166:240bc5a0f42a | 183 | tmp_y = y1; |
| thedo | 166:240bc5a0f42a | 184 | |
| thedo | 166:240bc5a0f42a | 185 | //pt1 = pt2; |
| thedo | 166:240bc5a0f42a | 186 | x1 = x2; |
| thedo | 166:240bc5a0f42a | 187 | y1 = y2; |
| thedo | 166:240bc5a0f42a | 188 | |
| thedo | 166:240bc5a0f42a | 189 | //pt2 = ptTemp; |
| thedo | 166:240bc5a0f42a | 190 | x2 = tmp_x; |
| thedo | 166:240bc5a0f42a | 191 | y2 = tmp_y; |
| thedo | 166:240bc5a0f42a | 192 | } |
| thedo | 166:240bc5a0f42a | 193 | |
| thedo | 166:240bc5a0f42a | 194 | for (i = y1; i <= y2; i++) // Draw points |
| thedo | 166:240bc5a0f42a | 195 | { |
| thedo | 166:240bc5a0f42a | 196 | draw_pixel(x1,i,color); |
| thedo | 166:240bc5a0f42a | 197 | } |
| thedo | 166:240bc5a0f42a | 198 | return; |
| thedo | 166:240bc5a0f42a | 199 | } |
| thedo | 166:240bc5a0f42a | 200 | |
| thedo | 166:240bc5a0f42a | 201 | if (y1 == y2) // Case equation y = m. |
| thedo | 166:240bc5a0f42a | 202 | { |
| thedo | 166:240bc5a0f42a | 203 | if (x1 > x2) // Swap 2 points |
| thedo | 166:240bc5a0f42a | 204 | { |
| thedo | 166:240bc5a0f42a | 205 | //ptTemp = pt1; |
| thedo | 166:240bc5a0f42a | 206 | tmp_x = x1; |
| thedo | 166:240bc5a0f42a | 207 | tmp_y = y1; |
| thedo | 166:240bc5a0f42a | 208 | |
| thedo | 166:240bc5a0f42a | 209 | //pt1 = pt2; |
| thedo | 166:240bc5a0f42a | 210 | x1 = x2; |
| thedo | 166:240bc5a0f42a | 211 | y1 = y2; |
| thedo | 166:240bc5a0f42a | 212 | |
| thedo | 166:240bc5a0f42a | 213 | //pt2 = ptTemp; |
| thedo | 166:240bc5a0f42a | 214 | x2 = tmp_x; |
| thedo | 166:240bc5a0f42a | 215 | y2 = tmp_y; |
| thedo | 166:240bc5a0f42a | 216 | } |
| thedo | 166:240bc5a0f42a | 217 | |
| thedo | 166:240bc5a0f42a | 218 | for (i = x1; i <= x2; i++) // Draw points |
| thedo | 166:240bc5a0f42a | 219 | { |
| thedo | 166:240bc5a0f42a | 220 | draw_pixel(i,y1,color); |
| thedo | 166:240bc5a0f42a | 221 | } |
| thedo | 166:240bc5a0f42a | 222 | return; |
| thedo | 166:240bc5a0f42a | 223 | } |
| thedo | 166:240bc5a0f42a | 224 | |
| thedo | 166:240bc5a0f42a | 225 | // Case equation y = ax + b |
| thedo | 166:240bc5a0f42a | 226 | float fA = (((float)(y2 - y1))) / (x2 - x1); // Calculate a. |
| thedo | 166:240bc5a0f42a | 227 | float fB = y1 - fA * x1; // Calculate b. |
| thedo | 166:240bc5a0f42a | 228 | |
| thedo | 166:240bc5a0f42a | 229 | if (x1 > x2) // Swap 2 points |
| thedo | 166:240bc5a0f42a | 230 | { |
| thedo | 166:240bc5a0f42a | 231 | //ptTemp = pt1; |
| thedo | 166:240bc5a0f42a | 232 | tmp_x = x1; |
| thedo | 166:240bc5a0f42a | 233 | tmp_y = y1; |
| thedo | 166:240bc5a0f42a | 234 | |
| thedo | 166:240bc5a0f42a | 235 | //pt1 = pt2; |
| thedo | 166:240bc5a0f42a | 236 | x1 = x2; |
| thedo | 166:240bc5a0f42a | 237 | y1 = y2; |
| thedo | 166:240bc5a0f42a | 238 | |
| thedo | 166:240bc5a0f42a | 239 | //pt2 = ptTemp; |
| thedo | 166:240bc5a0f42a | 240 | x2 = tmp_x; |
| thedo | 166:240bc5a0f42a | 241 | y2 = tmp_y; |
| thedo | 166:240bc5a0f42a | 242 | } |
| thedo | 166:240bc5a0f42a | 243 | |
| thedo | 166:240bc5a0f42a | 244 | // Calculate every points based on the equation |
| thedo | 166:240bc5a0f42a | 245 | for (i = x1; i <= x2; i++) |
| thedo | 166:240bc5a0f42a | 246 | { |
| thedo | 166:240bc5a0f42a | 247 | draw_pixel(i,(int)floor(fA * i + fB),color); |
| thedo | 166:240bc5a0f42a | 248 | } |
| thedo | 166:240bc5a0f42a | 249 | } |
| thedo | 166:240bc5a0f42a | 250 | |
| thedo | 166:240bc5a0f42a | 251 | void graphicFrameworkCanvas::drawBresenham1(int x1, int y1, int x2, int y2, |
| thedo | 166:240bc5a0f42a | 252 | uint8_t* color) |
| thedo | 166:240bc5a0f42a | 253 | { |
| thedo | 166:240bc5a0f42a | 254 | int iDx = abs(x2 - x1); |
| thedo | 166:240bc5a0f42a | 255 | int iDy = abs(y2 - y1); |
| thedo | 166:240bc5a0f42a | 256 | int iTwoDy = 2 * iDy; |
| thedo | 166:240bc5a0f42a | 257 | int iTwoDyMinusDx = 2 * (iDy - iDx); |
| thedo | 166:240bc5a0f42a | 258 | int iP = iTwoDy - iDx; |
| thedo | 166:240bc5a0f42a | 259 | int iX, iY, iXEnd; |
| thedo | 166:240bc5a0f42a | 260 | |
| thedo | 166:240bc5a0f42a | 261 | // Get smaller x to base |
| thedo | 166:240bc5a0f42a | 262 | if (x1 > x2) |
| thedo | 166:240bc5a0f42a | 263 | { |
| thedo | 166:240bc5a0f42a | 264 | iX = x2; |
| thedo | 166:240bc5a0f42a | 265 | iY = y2; |
| thedo | 166:240bc5a0f42a | 266 | iXEnd = x1; |
| thedo | 166:240bc5a0f42a | 267 | } |
| thedo | 166:240bc5a0f42a | 268 | else |
| thedo | 166:240bc5a0f42a | 269 | { |
| thedo | 166:240bc5a0f42a | 270 | iX = x1; |
| thedo | 166:240bc5a0f42a | 271 | iY = y1; |
| thedo | 166:240bc5a0f42a | 272 | iXEnd = x2; |
| thedo | 166:240bc5a0f42a | 273 | } |
| thedo | 166:240bc5a0f42a | 274 | |
| thedo | 166:240bc5a0f42a | 275 | // Draw first point |
| thedo | 166:240bc5a0f42a | 276 | draw_pixel(iX,iY,color); |
| thedo | 166:240bc5a0f42a | 277 | |
| thedo | 166:240bc5a0f42a | 278 | while (iX < iXEnd) |
| thedo | 166:240bc5a0f42a | 279 | { |
| thedo | 166:240bc5a0f42a | 280 | iX++; |
| thedo | 166:240bc5a0f42a | 281 | if (iP < 0) |
| thedo | 166:240bc5a0f42a | 282 | iP += iTwoDy; |
| thedo | 166:240bc5a0f42a | 283 | else |
| thedo | 166:240bc5a0f42a | 284 | { |
| thedo | 166:240bc5a0f42a | 285 | iY++; |
| thedo | 166:240bc5a0f42a | 286 | iP += iTwoDyMinusDx; |
| thedo | 166:240bc5a0f42a | 287 | } |
| thedo | 166:240bc5a0f42a | 288 | |
| thedo | 166:240bc5a0f42a | 289 | // Draw points |
| thedo | 166:240bc5a0f42a | 290 | draw_pixel(iX,iY,color); |
| thedo | 166:240bc5a0f42a | 291 | } |
| thedo | 166:240bc5a0f42a | 292 | } |
| thedo | 166:240bc5a0f42a | 293 | |
| thedo | 166:240bc5a0f42a | 294 | void graphicFrameworkCanvas::drawBresenham2(int x1, int y1, int x2, int y2, uint8_t* color) |
| thedo | 166:240bc5a0f42a | 295 | { |
| thedo | 166:240bc5a0f42a | 296 | int iDx = abs(x2 - x1); |
| thedo | 166:240bc5a0f42a | 297 | int iDy = abs(y2 - y1); |
| thedo | 166:240bc5a0f42a | 298 | int iTwoDy = 2 * iDy; |
| thedo | 166:240bc5a0f42a | 299 | int iTwoDyMinusDx = 2 * (iDy - iDx); |
| thedo | 166:240bc5a0f42a | 300 | int iP = -iTwoDy - iDx; |
| thedo | 166:240bc5a0f42a | 301 | int iX, iY, iXEnd; |
| thedo | 166:240bc5a0f42a | 302 | |
| thedo | 166:240bc5a0f42a | 303 | // Get smaller x to base |
| thedo | 166:240bc5a0f42a | 304 | if (x1 > x2) |
| thedo | 166:240bc5a0f42a | 305 | { |
| thedo | 166:240bc5a0f42a | 306 | iX = x2; |
| thedo | 166:240bc5a0f42a | 307 | iY = y2; |
| thedo | 166:240bc5a0f42a | 308 | iXEnd = x1; |
| thedo | 166:240bc5a0f42a | 309 | } |
| thedo | 166:240bc5a0f42a | 310 | else |
| thedo | 166:240bc5a0f42a | 311 | { |
| thedo | 166:240bc5a0f42a | 312 | iX = x1; |
| thedo | 166:240bc5a0f42a | 313 | iY = y1; |
| thedo | 166:240bc5a0f42a | 314 | iXEnd = x2; |
| thedo | 166:240bc5a0f42a | 315 | } |
| thedo | 166:240bc5a0f42a | 316 | |
| thedo | 166:240bc5a0f42a | 317 | // Draw first point |
| thedo | 166:240bc5a0f42a | 318 | draw_pixel(iX,iY,color); |
| thedo | 166:240bc5a0f42a | 319 | |
| thedo | 166:240bc5a0f42a | 320 | while (iX < iXEnd) |
| thedo | 166:240bc5a0f42a | 321 | { |
| thedo | 166:240bc5a0f42a | 322 | iX++; |
| thedo | 166:240bc5a0f42a | 323 | if (iP < 0) |
| thedo | 166:240bc5a0f42a | 324 | iP += iTwoDy; |
| thedo | 166:240bc5a0f42a | 325 | else |
| thedo | 166:240bc5a0f42a | 326 | { |
| thedo | 166:240bc5a0f42a | 327 | iY--; |
| thedo | 166:240bc5a0f42a | 328 | iP += iTwoDyMinusDx; |
| thedo | 166:240bc5a0f42a | 329 | } |
| thedo | 166:240bc5a0f42a | 330 | |
| thedo | 166:240bc5a0f42a | 331 | // Draw points |
| thedo | 166:240bc5a0f42a | 332 | draw_pixel(iX,iY,color); |
| thedo | 166:240bc5a0f42a | 333 | } |
| thedo | 166:240bc5a0f42a | 334 | } |
| thedo | 166:240bc5a0f42a | 335 | |
| thedo | 166:240bc5a0f42a | 336 | void graphicFrameworkCanvas::drawBresenham3(int x1, int y1, int x2, int y2, |
| thedo | 166:240bc5a0f42a | 337 | uint8_t* color) |
| thedo | 166:240bc5a0f42a | 338 | { |
| thedo | 166:240bc5a0f42a | 339 | int iDx = abs(x2 - x1); |
| thedo | 166:240bc5a0f42a | 340 | int iDy = abs(y2 - y1); |
| thedo | 166:240bc5a0f42a | 341 | int iTwoDx = 2 * iDx; |
| thedo | 166:240bc5a0f42a | 342 | int iTwoDxMinusDy = 2 * (iDx - iDy); |
| thedo | 166:240bc5a0f42a | 343 | int iP = iTwoDx - iDy; |
| thedo | 166:240bc5a0f42a | 344 | int iX, iY, iYEnd; |
| thedo | 166:240bc5a0f42a | 345 | |
| thedo | 166:240bc5a0f42a | 346 | // Get smaller x to base |
| thedo | 166:240bc5a0f42a | 347 | if (y1 > y2) |
| thedo | 166:240bc5a0f42a | 348 | { |
| thedo | 166:240bc5a0f42a | 349 | iX = x2; |
| thedo | 166:240bc5a0f42a | 350 | iY = y2; |
| thedo | 166:240bc5a0f42a | 351 | iYEnd = y1; |
| thedo | 166:240bc5a0f42a | 352 | } |
| thedo | 166:240bc5a0f42a | 353 | else |
| thedo | 166:240bc5a0f42a | 354 | { |
| thedo | 166:240bc5a0f42a | 355 | iX = x1; |
| thedo | 166:240bc5a0f42a | 356 | iY = y1; |
| thedo | 166:240bc5a0f42a | 357 | iYEnd = y2; |
| thedo | 166:240bc5a0f42a | 358 | } |
| thedo | 166:240bc5a0f42a | 359 | |
| thedo | 166:240bc5a0f42a | 360 | // Draw first point |
| thedo | 166:240bc5a0f42a | 361 | draw_pixel(iX,iY,color); |
| thedo | 166:240bc5a0f42a | 362 | |
| thedo | 166:240bc5a0f42a | 363 | while (iY < iYEnd) |
| thedo | 166:240bc5a0f42a | 364 | { |
| thedo | 166:240bc5a0f42a | 365 | iY++; |
| thedo | 166:240bc5a0f42a | 366 | if (iP < 0) |
| thedo | 166:240bc5a0f42a | 367 | iP += iTwoDx; |
| thedo | 166:240bc5a0f42a | 368 | else |
| thedo | 166:240bc5a0f42a | 369 | { |
| thedo | 166:240bc5a0f42a | 370 | iX++; |
| thedo | 166:240bc5a0f42a | 371 | iP += iTwoDxMinusDy; |
| thedo | 166:240bc5a0f42a | 372 | } |
| thedo | 166:240bc5a0f42a | 373 | // Draw points |
| thedo | 166:240bc5a0f42a | 374 | draw_pixel(iX,iY,color); |
| thedo | 166:240bc5a0f42a | 375 | } |
| thedo | 166:240bc5a0f42a | 376 | } |
| thedo | 166:240bc5a0f42a | 377 | |
| thedo | 166:240bc5a0f42a | 378 | void graphicFrameworkCanvas::drawBresenham4(int x1, int y1, int x2, int y2, |
| thedo | 166:240bc5a0f42a | 379 | uint8_t* color) |
| thedo | 166:240bc5a0f42a | 380 | { |
| thedo | 166:240bc5a0f42a | 381 | int iDx = abs(x2 - x1); |
| thedo | 166:240bc5a0f42a | 382 | int iDy = abs(y2 - y1); |
| thedo | 166:240bc5a0f42a | 383 | int iTwoDx = 2 * iDx; |
| thedo | 166:240bc5a0f42a | 384 | int iTwoDxMinusDy = 2 * (iDx - iDy); |
| thedo | 166:240bc5a0f42a | 385 | int iP = -iTwoDx - iDy; |
| thedo | 166:240bc5a0f42a | 386 | int iX, iY, iYEnd; |
| thedo | 166:240bc5a0f42a | 387 | |
| thedo | 166:240bc5a0f42a | 388 | // Get smaller x to base |
| thedo | 166:240bc5a0f42a | 389 | if (y1 > y2) |
| thedo | 166:240bc5a0f42a | 390 | { |
| thedo | 166:240bc5a0f42a | 391 | iX = x1; |
| thedo | 166:240bc5a0f42a | 392 | iY = y1; |
| thedo | 166:240bc5a0f42a | 393 | iYEnd = y2; |
| thedo | 166:240bc5a0f42a | 394 | } |
| thedo | 166:240bc5a0f42a | 395 | else |
| thedo | 166:240bc5a0f42a | 396 | { |
| thedo | 166:240bc5a0f42a | 397 | iX = x2; |
| thedo | 166:240bc5a0f42a | 398 | iY = y2; |
| thedo | 166:240bc5a0f42a | 399 | iYEnd = y1; |
| thedo | 166:240bc5a0f42a | 400 | } |
| thedo | 166:240bc5a0f42a | 401 | |
| thedo | 166:240bc5a0f42a | 402 | // Draw first point |
| thedo | 166:240bc5a0f42a | 403 | draw_pixel(iX,iY,color); |
| thedo | 166:240bc5a0f42a | 404 | |
| thedo | 166:240bc5a0f42a | 405 | while (iY > iYEnd) |
| thedo | 166:240bc5a0f42a | 406 | { |
| thedo | 166:240bc5a0f42a | 407 | iY--; |
| thedo | 166:240bc5a0f42a | 408 | if (iP < 0) |
| thedo | 166:240bc5a0f42a | 409 | iP += iTwoDx; |
| thedo | 166:240bc5a0f42a | 410 | else |
| thedo | 166:240bc5a0f42a | 411 | { |
| thedo | 166:240bc5a0f42a | 412 | iX++; |
| thedo | 166:240bc5a0f42a | 413 | iP += iTwoDxMinusDy; |
| thedo | 166:240bc5a0f42a | 414 | } |
| thedo | 166:240bc5a0f42a | 415 | |
| thedo | 166:240bc5a0f42a | 416 | // Draw points |
| thedo | 166:240bc5a0f42a | 417 | draw_pixel(iX,iY,color); |
| thedo | 166:240bc5a0f42a | 418 | } |
| thedo | 166:240bc5a0f42a | 419 | } |
| thedo | 166:240bc5a0f42a | 420 | |
| thedo | 166:240bc5a0f42a | 421 | void graphicFrameworkCanvas::drawLine(int x1, int y1, int x2, int y2, |
| thedo | 166:240bc5a0f42a | 422 | uint8_t* color) |
| thedo | 166:240bc5a0f42a | 423 | { |
| thedo | 166:240bc5a0f42a | 424 | if (x1 == x2 || y1 == y2) // Case equation x = m or y = m. |
| thedo | 166:240bc5a0f42a | 425 | { |
| thedo | 166:240bc5a0f42a | 426 | drawLineNormal(x1,y1,x2,y2,color); |
| thedo | 166:240bc5a0f42a | 427 | return; |
| thedo | 166:240bc5a0f42a | 428 | } |
| thedo | 166:240bc5a0f42a | 429 | |
| thedo | 166:240bc5a0f42a | 430 | // Case equation y = ax + b |
| thedo | 166:240bc5a0f42a | 431 | float fA = ((float)(y2 - y1)) / (x2 - x1); // Calculate a. |
| thedo | 166:240bc5a0f42a | 432 | |
| thedo | 166:240bc5a0f42a | 433 | // Case (0 < m < 1). |
| thedo | 166:240bc5a0f42a | 434 | if (fA > 0 && fA < 1) |
| thedo | 166:240bc5a0f42a | 435 | { |
| thedo | 166:240bc5a0f42a | 436 | drawBresenham1(x1,y1,x2,y2,color); |
| thedo | 166:240bc5a0f42a | 437 | return; |
| thedo | 166:240bc5a0f42a | 438 | } |
| thedo | 166:240bc5a0f42a | 439 | |
| thedo | 166:240bc5a0f42a | 440 | // Case (-1 < m < 0). |
| thedo | 166:240bc5a0f42a | 441 | if (fA > -1 && fA < 0) |
| thedo | 166:240bc5a0f42a | 442 | { |
| thedo | 166:240bc5a0f42a | 443 | drawBresenham2(x1,y1,x2,y2,color); |
| thedo | 166:240bc5a0f42a | 444 | return; |
| thedo | 166:240bc5a0f42a | 445 | } |
| thedo | 166:240bc5a0f42a | 446 | |
| thedo | 166:240bc5a0f42a | 447 | // Case (m >= 1). |
| thedo | 166:240bc5a0f42a | 448 | if (fA >= 1) |
| thedo | 166:240bc5a0f42a | 449 | { |
| thedo | 166:240bc5a0f42a | 450 | drawBresenham3(x1,y1,x2,y2,color); |
| thedo | 166:240bc5a0f42a | 451 | return; |
| thedo | 166:240bc5a0f42a | 452 | } |
| thedo | 166:240bc5a0f42a | 453 | |
| thedo | 166:240bc5a0f42a | 454 | // Case (m =< -1). |
| thedo | 166:240bc5a0f42a | 455 | if (fA <= -1) |
| thedo | 166:240bc5a0f42a | 456 | { |
| thedo | 166:240bc5a0f42a | 457 | drawBresenham4(x1,y1,x2,y2,color); |
| thedo | 166:240bc5a0f42a | 458 | return; |
| thedo | 166:240bc5a0f42a | 459 | } |
| thedo | 166:240bc5a0f42a | 460 | } |
| thedo | 166:240bc5a0f42a | 461 | |
| thedo | 166:240bc5a0f42a | 462 | void graphicFrameworkCanvas::drawCircle(int x0,int y0, int radius, uint8_t* color) |
| thedo | 166:240bc5a0f42a | 463 | { |
| thedo | 166:240bc5a0f42a | 464 | int x = radius; |
| thedo | 166:240bc5a0f42a | 465 | int y = 0; |
| thedo | 166:240bc5a0f42a | 466 | int err = 0; |
| thedo | 166:240bc5a0f42a | 467 | |
| thedo | 166:240bc5a0f42a | 468 | while (x >= y) |
| thedo | 166:240bc5a0f42a | 469 | { |
| thedo | 166:240bc5a0f42a | 470 | // Draw points |
| thedo | 166:240bc5a0f42a | 471 | draw_pixel(x0 + x,y0 + y,color); |
| thedo | 166:240bc5a0f42a | 472 | |
| thedo | 166:240bc5a0f42a | 473 | draw_pixel(x0 + y, y0 + x,color); |
| thedo | 166:240bc5a0f42a | 474 | draw_pixel(x0 - y, y0 + x,color); |
| thedo | 166:240bc5a0f42a | 475 | draw_pixel(x0 - x, y0 + y,color); |
| thedo | 166:240bc5a0f42a | 476 | draw_pixel(x0 - x, y0 - y,color); |
| thedo | 166:240bc5a0f42a | 477 | draw_pixel(x0 - y, y0 - x,color); |
| thedo | 166:240bc5a0f42a | 478 | draw_pixel(x0 + y, y0 - x,color); |
| thedo | 166:240bc5a0f42a | 479 | draw_pixel(x0 + x, y0 - y,color); |
| thedo | 166:240bc5a0f42a | 480 | |
| thedo | 166:240bc5a0f42a | 481 | y += 1; |
| thedo | 166:240bc5a0f42a | 482 | if (err <= 0) |
| thedo | 166:240bc5a0f42a | 483 | { |
| thedo | 166:240bc5a0f42a | 484 | err += 2*y + 1; |
| thedo | 166:240bc5a0f42a | 485 | } else |
| thedo | 166:240bc5a0f42a | 486 | { |
| thedo | 166:240bc5a0f42a | 487 | x -= 1; |
| thedo | 166:240bc5a0f42a | 488 | err += 2 * (y - x) + 1; |
| thedo | 166:240bc5a0f42a | 489 | } |
| thedo | 166:240bc5a0f42a | 490 | } |
| thedo | 166:240bc5a0f42a | 491 | } |
