Code covered by the BSD License  

Highlights from
Star Rating

image thumbnail
from Star Rating by Dimid Duchovny
A star rating system. Alpha version.

check_star_index (hObject, eventdata, handles)
%% Return the index of the selected/hovered star. Return -1 if none selected
function [star_index] = check_star_index (hObject, eventdata, handles)

star_index = -1;
first_pos = get(handles.star_axes_handles(1, 1),'Position');
star_box_x = first_pos(1);
star_box_y = first_pos(2);
star_x_size = first_pos(3);
star_y_size = first_pos(4);
second_pos = get(handles.star_axes_handles(2, 1),'Position');
star_x_offset = second_pos(1) - first_pos(1) - star_x_size;

star_box_width = handles.N_STARS * star_x_size + (handles.N_STARS - 1) * star_x_offset;
star_box_height =  star_y_size;
% %
cur_point = get(hObject,'CurrentPoint');
x_range = cur_point(1) >= star_box_x && cur_point(1) <= star_box_x + star_box_width;
y_range = cur_point(2) >= star_box_y && cur_point(2) <= star_box_y + star_box_height;

region_width = star_x_size + star_x_offset;
relative_cur_x = cur_point(1) - star_box_x;
div_x = floor(relative_cur_x /  region_width);
mod_x = relative_cur_x - (div_x * region_width);

if (x_range && y_range && mod_x <= star_x_size)
    star_index = div_x + 1;
end

end

Contact us