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.
Dependencies: mbed
flood_fill.cpp@12:4310fb637705, 2018-04-06 (annotated)
- Committer:
- x58alex41
- Date:
- Fri Apr 06 00:06:51 2018 +0000
- Revision:
- 12:4310fb637705
- Parent:
- 11:fb806a599444
started gyro
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
x58alex41 | 11:fb806a599444 | 1 | #include "flood_fill.h" |
x58alex41 | 11:fb806a599444 | 2 | maze::maze() |
x58alex41 | 11:fb806a599444 | 3 | :m_x(0),m_y(0),m_dir(up) |
x58alex41 | 11:fb806a599444 | 4 | { |
x58alex41 | 11:fb806a599444 | 5 | for(int i=0; i<7; i++) { |
x58alex41 | 11:fb806a599444 | 6 | for(int j=0; j<7; j++) { |
x58alex41 | 11:fb806a599444 | 7 | m_cells[i][j].dist=14-i-j; |
x58alex41 | 12:4310fb637705 | 8 | m_cells[i][15-j].dist=14-i-j; |
x58alex41 | 12:4310fb637705 | 9 | m_cells[15-i][j].dist=14-i-j; |
x58alex41 | 12:4310fb637705 | 10 | m_cells[15-i][16-j].dist=14-i-j; |
x58alex41 | 11:fb806a599444 | 11 | } |
x58alex41 | 11:fb806a599444 | 12 | } |
x58alex41 | 11:fb806a599444 | 13 | for(int i=0; i< 16; i++) { |
x58alex41 | 12:4310fb637705 | 14 | m_cells[i][0].down_clear=false; |
x58alex41 | 12:4310fb637705 | 15 | m_cells[i][15].up_clear=false; |
x58alex41 | 11:fb806a599444 | 16 | } |
x58alex41 | 11:fb806a599444 | 17 | for(int i=0; i< 16; i++) { |
x58alex41 | 12:4310fb637705 | 18 | m_cells[0][i].left_clear=false; |
x58alex41 | 12:4310fb637705 | 19 | m_cells[15][i].right_clear=false; |
x58alex41 | 11:fb806a599444 | 20 | } |
x58alex41 | 11:fb806a599444 | 21 | |
x58alex41 | 11:fb806a599444 | 22 | } |
x58alex41 | 11:fb806a599444 | 23 | |
x58alex41 | 11:fb806a599444 | 24 | void maze::update_cells(int x, int y) |
x58alex41 | 11:fb806a599444 | 25 | { |
x58alex41 | 11:fb806a599444 | 26 | int original_dist=m_cells[x][y].dist; |
x58alex41 | 11:fb806a599444 | 27 | bool value_found = false; |
x58alex41 | 11:fb806a599444 | 28 | //find the smallest distance in cardinal direction m_cellss and make current m_cells dist into that |
x58alex41 | 11:fb806a599444 | 29 | if(m_cells[x][y].right_clear) { |
x58alex41 | 11:fb806a599444 | 30 | m_cells[x][y].dist = m_cells[x+1][y].dist + 1; |
x58alex41 | 11:fb806a599444 | 31 | value_found = true; |
x58alex41 | 11:fb806a599444 | 32 | } |
x58alex41 | 11:fb806a599444 | 33 | if((m_cells[x][y].up_clear && m_cells[x][y].dist > m_cells[x][y+1].dist + 1) || !value_found) { |
x58alex41 | 11:fb806a599444 | 34 | m_cells[x][y].dist = m_cells[x][y+1].dist + 1; |
x58alex41 | 11:fb806a599444 | 35 | value_found = true; |
x58alex41 | 11:fb806a599444 | 36 | } |
x58alex41 | 11:fb806a599444 | 37 | if(( m_cells[x][y].left_clear && m_cells[x][y].dist > m_cells[x-1][y].dist + 1) || !value_found) { |
x58alex41 | 11:fb806a599444 | 38 | m_cells[x][y].dist = m_cells[x-1][y].dist + 1; |
x58alex41 | 11:fb806a599444 | 39 | value_found = true; |
x58alex41 | 11:fb806a599444 | 40 | } |
x58alex41 | 11:fb806a599444 | 41 | if((m_cells[x][y].down_clear && m_cells[x][y].dist > m_cells[x][y-1].dist + 1) || !value_found) { |
x58alex41 | 11:fb806a599444 | 42 | m_cells[x][y].dist = m_cells[x][y-1].dist + 1; |
x58alex41 | 11:fb806a599444 | 43 | value_found = true; |
x58alex41 | 11:fb806a599444 | 44 | } |
x58alex41 | 11:fb806a599444 | 45 | |
x58alex41 | 11:fb806a599444 | 46 | // if m_cells dist changed, need to adjust neighboring cell(s) |
x58alex41 | 11:fb806a599444 | 47 | if(original_dist!=m_cells[x][y].dist) { |
x58alex41 | 11:fb806a599444 | 48 | if(m_cells[x][y].up_clear && (m_cells[x][y].dist <= m_cells[x][y+1].dist)) |
x58alex41 | 11:fb806a599444 | 49 | update_cells(x,y+1); |
x58alex41 | 11:fb806a599444 | 50 | if(m_cells[x][y].down_clear && (m_cells[x][y].dist <= m_cells[x][y-1].dist)) |
x58alex41 | 11:fb806a599444 | 51 | update_cells(x,y-1); |
x58alex41 | 11:fb806a599444 | 52 | if(m_cells[x][y].right_clear && (m_cells[x][y].dist <= m_cells[x+1][y].dist)) |
x58alex41 | 11:fb806a599444 | 53 | update_cells(x+1,y); |
x58alex41 | 11:fb806a599444 | 54 | if(m_cells[x][y].left_clear && (m_cells[x][y].dist <= m_cells[x-1][y].dist)) |
x58alex41 | 11:fb806a599444 | 55 | update_cells(x-1,y); |
x58alex41 | 11:fb806a599444 | 56 | } |
x58alex41 | 11:fb806a599444 | 57 | } |