Project

General

Profile

Statistics
| Revision:

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

History | View | Annotate | Download (3.11 KB)

1
#include <lcom/lcf.h>
2

    
3
#include "rectangle.h"
4

    
5
#include "graph.h"
6
#include "utils.h"
7

    
8
struct rectangle{
9
    int16_t  x, y;
10
    uint16_t w, h;
11
    uint32_t outline_color;
12
    uint32_t fill_color;
13
    int16_t  outline_width;
14
    uint8_t  fill_alpha;
15
};
16

    
17
rectangle_t* (rectangle_ctor)(int16_t x, int16_t y, uint16_t w, uint16_t h){
18
    rectangle_t *ret = (rectangle_t*)malloc(sizeof(rectangle_t));
19
    if(ret == NULL) return NULL;
20
    ret->x = x;
21
    ret->y = y;
22
    ret->w = w;
23
    ret->h = h;
24
    ret->fill_color    = 0xFFFFFF;
25
    ret->fill_alpha    = 0xFF;
26
    ret->outline_color = 0x000000;
27
    ret->outline_width = 0;
28
    return ret;
29
}
30
void (rectangle_dtor)(rectangle_t *p){
31
    if(p == NULL) return;
32
    free(p);
33
}
34

    
35
void (rectangle_set_pos)          (rectangle_t *p,  int16_t x,  int16_t y){ p->x = x; p->y = y; }
36
void (rectangle_set_size)         (rectangle_t *p, uint16_t w, uint16_t h){ p->w = w; p->h = h; }
37
void (rectangle_set_fill_color)   (rectangle_t *p, uint32_t color        ){ p->fill_color = color; }
38
void (rectangle_set_fill_trans)   (rectangle_t *p, uint8_t  alpha        ){ p->fill_alpha = alpha; }
39
void (rectangle_set_outline_color)(rectangle_t *p, uint32_t color        ){ p->outline_color = color; }
40
void (rectangle_set_outline_width)(rectangle_t *p,  int16_t width        ){ p->outline_width = width; }
41

    
42
int16_t  (rectangle_get_x)(const rectangle_t *p){ return p->x; }
43
int16_t  (rectangle_get_y)(const rectangle_t *p){ return p->y; }
44
uint16_t (rectangle_get_w)(const rectangle_t *p){ return p->w; }
45
uint16_t (rectangle_get_h)(const rectangle_t *p){ return p->h; }
46

    
47
int (rectangle_collide_point)(const rectangle_t *p, int x, int y) {
48
    int16_t x0 = p->x, y0 = p->y;
49
    return (x >= x0 && x <= x0 + p->w) && (y >= y0 && y <= y0 + p->h);
50
}
51

    
52
static void (rectangle_draw_hline)(int16_t x, int16_t y, int16_t l, uint32_t color){
53
    if(l < 0){ rectangle_draw_hline(x+l, y, -l, color); return; }
54
    for(int16_t x_ = max_16(0,x); x_ < min_16(x+l,(int16_t)graph_get_XRes()); ++x_){
55
        graph_set_pixel((uint16_t)x_, (uint16_t)y, color);
56
    }
57
}
58
static void (rectangle_draw_vline)(int16_t x, int16_t y, int16_t l, uint32_t color){
59
    if(l < 0){ rectangle_draw_vline(x, y+l, -l, color); return; }
60
    for(int16_t y_ = max_16(0,y); y_ < min_16(y+l,(int16_t)graph_get_YRes()); ++y_){
61
        graph_set_pixel((uint16_t)x, (uint16_t)y_, color);
62
    }
63
}
64

    
65
void (rectangle_draw)(const rectangle_t *p){
66
    /// fill
67
    if(p->fill_alpha > ALPHA_THRESHOLD)
68
        for(int16_t y = max_16(p->y,0); y < min_16(p->y+p->h, (int16_t)graph_get_YRes()); ++y)
69
            rectangle_draw_hline(p->x, y, (int16_t)p->w, p->fill_color);
70
    /// border
71
    int16_t step = (p->outline_width > 0 ? 1 : -1);
72
    int16_t l = p->x, r = p->x+p->w, t = p->y, b = p->y+p->h;
73
    if(step > 0){
74
        --l; ++r; --t; ++b;
75
    }
76
    for(int16_t i = 0; i != p->outline_width; i += step, l -= step, r += step, t -= step, b += step){
77
        rectangle_draw_hline(l  , t  , r-l, p->outline_color);
78
        rectangle_draw_hline(l  , b-1, r-l, p->outline_color);
79
        rectangle_draw_vline(l  , t  , b-t, p->outline_color);
80
        rectangle_draw_vline(r-1, t  , b-t, p->outline_color);
81
    }
82
}