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