-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add function to check for rectangle intresection and get them
- Loading branch information
Showing
9 changed files
with
148 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#ifndef __SHAPES_H__ | ||
#define __SHAPES_H__ | ||
|
||
#include <stdint.h> | ||
#include <window.h> | ||
|
||
void _fb_draw_rectangle(_fb_window_t main_window, _fb_window_t rectangle, uint32_t color); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#include <framebuffer.h> | ||
#include <shapes.h> | ||
|
||
void _fb_draw_rectangle(_fb_window_t main_window, _fb_window_t rectangle, uint32_t color) { | ||
//0xEB72F9 | ||
uint32_t rectangle_w = rectangle.x_orig + rectangle.width; | ||
uint32_t rectangle_h = rectangle.y_orig + rectangle.height; | ||
for(uint32_t i = rectangle.y_orig; i < rectangle_h; i++) { | ||
for(uint32_t j = rectangle.x_orig; j < rectangle_w; j++) { | ||
_fb_put_pixel(j, i, color); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,80 @@ | ||
#include <logging.h> | ||
#include <window.h> | ||
|
||
|
||
/** | ||
* This function return true if x and y are within the area of the window defined by area_to_interesect | ||
* This function return true if x and y are within the area of the window defined by area_to_intersect | ||
* | ||
* | ||
* @param x x coord of the point to check | ||
* @param pixels_h y cood of the point to check | ||
* @param area_to_interesect the window area we want to check the intersection | ||
* @return true if the point (x,y) is within the rectangle specified by area_to_interesect | ||
* @param area_to_intersect the window area we want to check the intersection | ||
* @return true if the point (x,y) is within the rectangle specified by area_to_intersect | ||
*/ | ||
bool _fb_intersect_window(uint32_t x, uint32_t y, _fb_window_t *area_to_interesect) { | ||
if ( x > area_to_interesect->x_orig + area_to_interesect->width) | ||
bool _fb_intersect_window(uint32_t x, uint32_t y, _fb_window_t *area_to_intersect) { | ||
if ( x > area_to_intersect->x_orig + area_to_intersect->width) | ||
return false; | ||
if ( x < area_to_interesect->x_orig) | ||
if ( x < area_to_intersect->x_orig) | ||
return false; | ||
if (y > area_to_interesect->y_orig + area_to_interesect->height) | ||
if (y > area_to_intersect->y_orig + area_to_intersect->height) | ||
return false; | ||
if (y < area_to_interesect->y_orig) | ||
if (y < area_to_intersect->y_orig) | ||
return false; | ||
return true; | ||
} | ||
|
||
/** | ||
* This function return true if x and y are within the area of the window defined by area_to_intersect | ||
* | ||
* | ||
* @param x x coord of the point to check | ||
* @param pixels_h y cood of the point to check | ||
* @param area_to_intersect the window area we want to check the intersection | ||
* @return true if the point (x,y) is within the rectangle specified by area_to_intersect | ||
*/ | ||
uint8_t _fb_get_rectangles(_fb_window_t *rectangles, _fb_window_t* main_window, _fb_window_t *area_to_intersect) { | ||
//_fb_window_t rectangles[4]; | ||
uint8_t cur_rectangle = 0; | ||
if(area_to_intersect == NULL) { | ||
return 0; | ||
} | ||
// We can have up to 4 rectangles surrounding an area. | ||
// How many depends on where the area. | ||
// The window and the area have the left side with same origin | ||
pretty_logf(Verbose, "Main window: x %d, y %d, w %d, h %d", main_window->x_orig, main_window->y_orig, main_window->width, main_window->height); | ||
pretty_logf(Verbose, "Area to intersect: x %d, y %d, w %d, h %d", area_to_intersect->x_orig, area_to_intersect->y_orig, area_to_intersect->width, area_to_intersect->height); | ||
if (main_window->x_orig < area_to_intersect->x_orig) { | ||
rectangles[cur_rectangle].x_orig = main_window->x_orig; | ||
rectangles[cur_rectangle].y_orig = main_window->y_orig; | ||
rectangles[cur_rectangle].width = area_to_intersect->x_orig - main_window->x_orig; | ||
rectangles[cur_rectangle].height = main_window->height; | ||
pretty_logf(Verbose, "1 rectangle: %d, x_orig: %d, y_orig: %d, widht: %d, height: %d", cur_rectangle, rectangles[cur_rectangle].x_orig, rectangles[cur_rectangle].y_orig, rectangles[cur_rectangle].width, rectangles[cur_rectangle].height); | ||
cur_rectangle++; | ||
} | ||
if (area_to_intersect->x_orig + area_to_intersect->width < main_window->x_orig + main_window->width) { | ||
rectangles[cur_rectangle].x_orig = area_to_intersect->x_orig + area_to_intersect->width; | ||
rectangles[cur_rectangle].y_orig = main_window->y_orig; | ||
rectangles[cur_rectangle].width = (main_window->x_orig + main_window->width) - area_to_intersect->x_orig; | ||
rectangles[cur_rectangle].height = main_window->height; | ||
pretty_logf(Verbose, "2 rectangle: %d, x_orig: %d, y_orig: %d, widht: %d, height: %d", cur_rectangle, rectangles[cur_rectangle].x_orig, rectangles[cur_rectangle].y_orig, rectangles[cur_rectangle].width, rectangles[cur_rectangle].height); | ||
cur_rectangle++; | ||
} | ||
if ( main_window->y_orig < area_to_intersect->y_orig) { | ||
rectangles[cur_rectangle].x_orig = area_to_intersect->x_orig; | ||
rectangles[cur_rectangle].width = area_to_intersect->width; | ||
rectangles[cur_rectangle].y_orig = main_window->y_orig; | ||
rectangles[cur_rectangle].height = area_to_intersect->y_orig - main_window->y_orig; | ||
pretty_logf(Verbose, "3 rectangle: %d, x_orig: %d, y_orig: %d, widht: %d, height: %d", cur_rectangle, rectangles[cur_rectangle].x_orig, rectangles[cur_rectangle].y_orig, rectangles[cur_rectangle].width, rectangles[cur_rectangle].height); | ||
cur_rectangle++; | ||
|
||
} | ||
if ( (area_to_intersect->y_orig + area_to_intersect->height) < (main_window->y_orig + main_window->height) ) { | ||
rectangles[cur_rectangle].x_orig = area_to_intersect->x_orig; | ||
rectangles[cur_rectangle].width = area_to_intersect->width; | ||
rectangles[cur_rectangle].y_orig = (area_to_intersect->y_orig + area_to_intersect->height); | ||
rectangles[cur_rectangle].height = (main_window->y_orig + main_window->height) - (area_to_intersect->y_orig + area_to_intersect->height); | ||
pretty_logf(Verbose, "4 rectangle: %d, x_orig: %d, y_orig: %d, widht: %d, height: %d", cur_rectangle, rectangles[cur_rectangle].x_orig, rectangles[cur_rectangle].y_orig, rectangles[cur_rectangle].width, rectangles[cur_rectangle].height); | ||
cur_rectangle++; | ||
} | ||
return cur_rectangle; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#include <assert.h> | ||
#include <test_common.h> | ||
#include <stdint.h> | ||
#include <stdlib.h> | ||
#include <stdio.h> | ||
#include <window.h> | ||
|
||
void test_get_rectangles(); | ||
|
||
int main() { | ||
test_get_rectangles(); | ||
} | ||
|
||
void test_get_rectangles() { | ||
_fb_window_t rectangles[4]; | ||
_fb_window_t main_window = {0,0, 1280, 800}; | ||
/*main_window.x_orig = 0; | ||
main_window.y_orig = 0; | ||
main_window.width = 1280; | ||
main_window.height = 800;*/ | ||
_fb_window_t area_to_intersect = {1030, 0, 250, 250}; | ||
printf("Testing _fb_get_rectangles function\n"); | ||
printf("\t[%s]: main_window values: x: %d - y: %d - w: %d - h: %d\n", __FUNCTION__, main_window.x_orig, main_window.y_orig, main_window.width, main_window.height); | ||
uint8_t n_squares = _fb_get_rectangles(rectangles, &main_window, NULL); | ||
printf("\t[%s] n_squares should be 0 : %d\n", __FUNCTION__, n_squares); | ||
assert(n_squares == 0); | ||
n_squares = _fb_get_rectangles(rectangles, &main_window, &area_to_intersect); | ||
printf("\t[%s] n_squares should be 2 : %d\n", __FUNCTION__, n_squares); | ||
assert(n_squares == 2); | ||
_fb_window_t area_to_intersect_2 = {500, 300, 200, 120}; | ||
n_squares = _fb_get_rectangles(rectangles, &main_window, &area_to_intersect_2); | ||
printf("\t[%s]: area_to_intersect values: x: %d - y: %d - w: %d - h: %d\n", __FUNCTION__, area_to_intersect_2.x_orig, area_to_intersect_2.y_orig, area_to_intersect_2.width, area_to_intersect_2.height); | ||
printf("\t[%s]: n_squares should be 4: %d\n", __FUNCTION__, n_squares); | ||
for(int i = 0; i < n_squares; i++) { | ||
printf("\t[%s]: x_orig: %d - y_orig: %d - width: %d - height: %d\n", __FUNCTION__, rectangles[i].x_orig, rectangles[i].y_orig, rectangles[i].width, rectangles[i].height); | ||
} | ||
_fb_window_t area_to_intersect_3 = {0, 0, 200, 120}; | ||
n_squares = _fb_get_rectangles(rectangles, &main_window, &area_to_intersect_3); | ||
printf("\t[%s]: area_to_intersect values: x: %d - y: %d - w: %d - h: %d\n", __FUNCTION__, area_to_intersect_3.x_orig, area_to_intersect_3.y_orig, area_to_intersect_3.width, area_to_intersect_3.height); | ||
printf("\t[%s]: n_squares should be 4: %d\n", __FUNCTION__, n_squares); | ||
for(int i = 0; i < n_squares; i++) { | ||
printf("\t[%s]: x_orig: %d - y_orig: %d - width: %d - height: %d\n", __FUNCTION__, rectangles[i].x_orig, rectangles[i].y_orig, rectangles[i].width, rectangles[i].height); | ||
} | ||
} |