Project

General

Profile

Statistics
| Revision:

root / proj / libs / graph / src / rectangle.c @ 285

History | View | Annotate | Download (2.79 KB)

1 178 up20180642
#include <lcom/lcf.h>
2
3
#include "rectangle.h"
4
5 179 up20180642
#include "graph.h"
6
#include "utils.h"
7
8 178 up20180642
struct rectangle{
9 179 up20180642
    int16_t  x, y;
10 178 up20180642
    uint16_t w, h;
11
    uint32_t fill_color;
12
    uint32_t outline_color;
13 179 up20180642
    int16_t  outline_width;
14 178 up20180642
};
15
16
rectangle_t* (rectangle_ctor)(int16_t x, int16_t y, uint16_t w, uint16_t h){
17
    rectangle_t *ret = (rectangle_t*)malloc(sizeof(rectangle_t));
18 179 up20180642
    if(ret == NULL) return NULL;
19
    ret->x = x;
20
    ret->y = y;
21
    ret->w = w;
22
    ret->h = h;
23
    ret->fill_color    = 0xFFFFFF;
24
    ret->outline_color = 0x000000;
25
    ret->outline_width = 0;
26 178 up20180642
    return ret;
27
}
28 179 up20180642
void (rectangle_dtor)(rectangle_t *p){
29 178 up20180642
    if(p == NULL) return;
30
    free(p);
31
}
32 179 up20180642
33
void (rectangle_set_pos)          (rectangle_t *p,  int16_t x,  int16_t y){ p->x = x; p->y = y; }
34
void (rectangle_set_size)         (rectangle_t *p, uint16_t w, uint16_t h){ p->w = w; p->h = h; }
35
void (rectangle_set_fill_color)   (rectangle_t *p, uint32_t color        ){ p->fill_color = color; }
36
void (rectangle_set_outline_color)(rectangle_t *p, uint32_t color        ){ p->outline_color = color; }
37
void (rectangle_set_outline_width)(rectangle_t *p,  int16_t width        ){ p->outline_width = width; }
38
39
int16_t  (rectangle_get_x)(const rectangle_t *p){ return p->x; }
40
int16_t  (rectangle_get_y)(const rectangle_t *p){ return p->y; }
41
uint16_t (rectangle_get_w)(const rectangle_t *p){ return p->w; }
42
uint16_t (rectangle_get_h)(const rectangle_t *p){ return p->h; }
43
44 251 up20180655
int (rectangle_collide_point)(const rectangle_t *p, int x, int y) {
45
    int16_t x0 = p->x, y0 = p->y;
46
    return (x >= x0 && x <= x0 + p->w) && (y >= y0 && y <= y0 + p->h);
47
}
48
49 179 up20180642
static void (rectangle_draw_hline)(int16_t x, int16_t y, int16_t l, uint32_t color){
50
    if(l < 0){ rectangle_draw_hline(x+l, y, -l, color); return; }
51
    for(int16_t x_ = max(0,x); x_ < min(x+l,graph_get_XRes()); ++x_){
52
        graph_set_pixel(x_, y, color);
53
    }
54
}
55
56
static void (rectangle_draw_vline)(int16_t x, int16_t y, int16_t l, uint32_t color){
57
    if(l < 0){ rectangle_draw_vline(x, y+l, -l, color); return; }
58
    for(int16_t y_ = max(0,y); y_ < min(y+l,graph_get_YRes()); ++y_){
59
        graph_set_pixel(x, y_, color);
60
    }
61
}
62
63
void (rectangle_draw)(const rectangle_t *p){
64
    for(int16_t y = max(p->y,0); y < min(p->y+p->h, graph_get_YRes()); ++y)
65
        rectangle_draw_hline(p->x, y, p->w, p->fill_color);
66
67
    int16_t step = (p->outline_width > 0 ? 1 : -1);
68
    int16_t l = p->x, r = p->x+p->w, t = p->y, b = p->y+p->h;
69
    if(step > 0){
70
        --l; ++r; --t; ++b;
71
    }
72
    for(int16_t i = 0; i != p->outline_width; i += step, l -= step, r += step, t -= step, b += step){
73
        rectangle_draw_hline(l  , t  , r-l, p->outline_color);
74
        rectangle_draw_hline(l  , b-1, r-l, p->outline_color);
75
        rectangle_draw_vline(l  , t  , b-t, p->outline_color);
76
        rectangle_draw_vline(r-1, t  , b-t, p->outline_color);
77
    }
78
}