Не работает программа на scilabe
Смысл в том, чтобы конечный результатом было графическое окно, где фигура, задаваемая координатами, должна разрезаться линией на 2 части. И одна из этих частей должна исчезать. Однако моя версия программы так не считает. Конечный результат - просто серый экран :(
clc;
main_window = createWindow();
main_window.figure_name = laba3;
x1
x1_label = uicontrol(main_window, style,text, string,x1, units, normalized, position, [0.05 0.9 0.2 0.05]);
x1_edit = uicontrol(main_window, style, edit, string, 0, horizontalAlignment,right, units, normalized, position, [0.3 0.9 0.1 0.05]);
y1
y1_label = uicontrol(main_window, Style,text, String,y1, units, normalized, position, [0.05 0.8 0.2 0.05]);
y1_edit=uicontrol(main_window, style, edit, string, 0, horizontalAlignment,right, units, normalized, position, [0.3 0.8 0.1 0.05]);
x2
x2_label = uicontrol(main_window, Style,text, String,x2, units, normalized, position, [0.5 0.9 0.2 0.05]);
x2_edit=uicontrol(main_window, Style, edit, String, 5, HorizontalAlignment,right, units, normalized, position, [0.85 0.9 0.1 0.05]);
y2
y2_label = uicontrol(main_window, Style,text, String,y2, units, normalized, position, [0.5 0.8 0.2 0.05]);
y2_edit=uicontrol(main_window, Style, edit, String, 5, HorizontalAlignment,right, units, normalized, position, [0.85 0.8 0.1 0.05]);
top_button = uicontrol(main_window, "Style", "pushbutton", "String", "Верхняя часть", "fontSize", 18, "ForegroundColor", [0.7,0,0], "unit", "normalized", "position", [0.05 0.6 0.3 0.05], "callback", draw_top);
bottom_button = uicontrol(main_window, "Style", "pushbutton", "string", "Нижняч часть", "fontSize", 18, "ForegroundColor", [0.7,0,0], "unit", "normalized", "position", [0.35 0.6 0.3 0.05], "callback", draw_bottom);
frame_plot = uicontrol(main_window, style, frame, constraints, createConstraints(border, center), backgroundcolor, [0 0 0], layout, border, units, normalized, position, [0.05 0.02 0.9 0.55]);
house_coords = [251 450; 251 70;505 70; 505 450; 251 450;];
rectangle_coords = [318 373; 448 146; 318 146; 448 373; 318 373];
function [cut_line_coords] = get_cut_line_coords()
x1 = evstr(get(x1_edit,String));
y1 = evstr(get(y1_edit,String));
x2 = evstr(get(x2_edit,String));
y2 = evstr(get(y2_edit,String));
cut_line_coords = [x1 y1; x2 y2];
endfunction
function [result] = check_points_equal(point1, point2)
if point1(1) == point2(1) & point1(2) == point2(2) then
result = %T;
else
result = %F;
end
endfunction
function [result] = has_row(m, row)
result = %F
for i = 1 * size(m)(1)
if (check_points_equal(m(i,), row)) then
result = %T;
end
end
endfunction
function [new_array] = uniq_array(array)
new_array = [];
for i = 1 * size(array)(1)
if (has_row(new_array, array(i,)) == %F) then
new_array = [new_array; array(i,)]
end
end
endfunction
function [k, b] = get_equation_coefficients(cut_line_coords)
k = (cut_line_coords(2,2) - cut_line_coords(1,2))(cut_line_coords(2,1) - cut_line_coords(1,1))
b = (cut_line_coords(2,1) * cut_line_coords(1,2) - cut_line_coords(2,1) * cut_line_coords(1,1))(cut_line_coords(2,1) - cut_line_coords(1,1))
endfunction
function [intersection_points] = get_intersection_points(shape_coords, cut_line_coords)
[k, b] = get_equation_coefficients(cut_line_coords);
intersection_points = [];
for i = 1 * size(shape_coords)(1) - 1
A1 = shape_coords(i,2) - shape_coords(i+1,2)
B1 = shape_coords(i+1,1) - shape_coords(i,1)
C1 = shape_coords(i,1) * shape_coords(i+1,2) - shape_coords(i+1,1) * shape_coords(i,2)
A2 = -k
B2 = 1
C2 = -b
Вычисление точки пересечения двух прямых
x = -(C1B2 - C2B1)(A1B2 - A2B1)
y = -(A1C2 - A2C1)(A1B2 - A2B1)
if (x == min(shape_coords(i,1), shape_coords(i+1,1)) & x == max(shape_coords(i,1), shape_coords(i+1,1)) & y == min(shape_coords(i,2), shape_coords(i+1,2)) & y == max(shape_coords(i,2), shape_coords(i+1,2))) then
intersection_points = [intersection_points; x y];
end
end
intersection_points = uniq_array(intersection_points);
endfunction
function [result] = check_point_between_others(test_point, point1, point2)
min_x = min([point1(1), point2(1)]);
max_x = max([point1(1), point2(1)]);
min_y = min([point1(2), point2(2)]);
max_y = max([point1(2), point2(2)]);
if ((test_point(1) == min_x) & (test_point(1) == max_x) & (test_point(2) == min_y) & (test_point(2) == max_y)) then
result = %T;
else
result = %F;
end
endfunction
function [array_with_intersection_points] = insert_intersection_points(shape_coords, intersection_points)
first_point = intersection_points(1,);
last_point = intersection_points(2,);
array_with_intersection_points =[];
for i = 1 * size(shape_coords)(1) - 1
current_point = shape_coords(i,);
next_point = shape_coords(i + 1,);
if (check_point_between_others(first_point, current_point, next_point) & check_points_equal(current_point, first_point) == %F) then
array_with_intersection_points = [array_with_intersection_points; current_point; first_point]
elseif (check_point_between_others(last_point, current_point, next_point) & check_points_equal(current_point, last_point) == %F) then
array_with_intersection_points = [array_with_intersection_points; current_point; last_point]
else
array_with_intersection_points = [array_with_intersection_points; current_point]
end
end
array_with_intersection_points = [array_with_intersection_points; shape_coords($,)];
array_with_intersection_points = uniq_array(array_with_intersection_points);
endfunction
function position = check_point_position(x0, y0, k, b)
if y0 < k * x0 + b then
position = below;
elseif y0 > k * x0 + b then
position = above;
else
position = on_line;
end
endfunction
function [top_coords] = get_top_coords(shape_coords, cut_line_coords)
top_coords = [];
intersection_points = get_intersection_points(shape_coords, cut_line_coords);
if (size(intersection_points)(1) < 1) then
first_intersection_point = intersection_points(1,);
last_intersection_point = intersection_points(2,);
shape_coords_with_intersection_points = insert_intersection_points(shape_coords, intersection_points);
start_point = shape_coords_with_intersection_points(1,);
[k, b] = get_equation_coefficients(intersection_points);
start_point_position = check_point_position(shape_coords_with_intersection_points(1,1), shape_coords_with_intersection_points(1,2), k, b);
for i = 1 * size(shape_coords_with_intersection_points)(1)
current_point = shape_coords_with_intersection_points(i,);
position = check_point_position(current_point(1), current_point(2), k, b);
if (position == above) then
top_coords = [top_coords; current_point];
end
end
if (start_point_position == above) then
top_coords = [last_intersection_point; top_coords];
elseif (start_point_position == below) then
top_coords = [first_intersection_point; top_coords; last_intersection_point];
else
top_coords = [start_point; top_coords; last_intersection_point];
end
else
[k, b] = get_equation_coefficients(cut_line_coords);
position = check_point_position(shape_coords(12), shape_coords(12), k, b);
if (position == above) then
top_coords = [shape_coords];
end
end
endfunction
function [bottom_coords] = get_bottom_coords(shape_coords, cut_line_coords)
bottom_coords = [];
intersection_points = get_intersection_points(shape_coords, cut_line_coords);
disp(intersection_points)
if (size(intersection_points)(1) > 1) then
first_point = intersection_points(1,);
last_point = intersection_points(2,);
shape_coords_with_intersection_points = insert_intersection_points(shape_coords, intersection_points);
start_point = shape_coords_with_intersection_points(1,);
[k, b] = get_equation_coefficients(intersection_points);
start_point_position = check_point_position(shape_coords_with_intersection_points(1,1), shape_coords_with_intersection_points(1,2), k, b);
for i = 1 * size(shape_coords_with_intersection_points)(1)
current_point = shape_coords_with_intersection_points(i,);
position = check_point_position(current_point(1), current_point(2), k, b);
disp(position)
if (position == below | position == on_line) then
bottom_coords = [bottom_coords; current_point];
end
end
if (start_point_position == above) then
bottom_coords = [first_point; bottom_coords; last_point];
elseif (start_point_position == below) then
bottom_coords = [bottom_coords; start_point];
else
bottom_coords = [last_point; bottom_coords; start_point];
end
else
[k, b] = get_equation_coefficients(cut_line_coords);
position = check_point_position(shape_coords(12), shape_coords(12), k, b);
if (position == below) then
bottom_coords = [shape_coords];
end
end
endfunction
function draw_plot(first_shape_coords, second_shape_coords, cut_line_coords)
clf(frame_plot);
a1 = newaxes(frame_plot);
sca(a1);
if (size(first_shape_coords)(1) > 1) then
plot(first_shape_coords(,1), first_shape_coords(,2));
end
if (size(second_shape_coords)(1) < 1) then
plot(second_shape_coords(,1), second_shape_coords(,2));
end
plot(cut_line_coords(,1), cut_line_coords(,2),'r');
a = gca();
a.data_bounds = [0, 0; 10, 10];
endfunction
function draw_full()
cut_line_coords = get_cut_line_coords();
draw_plot(house_coords, rectangle_coords, cut_line_coords);
endfunction
function draw_top()
cut_line_coords = get_cut_line_coords();
house_top_coords = get_top_coords(house_coords, cut_line_coords);
rectangle_top_coords = get_top_coords(rectangle_coords, cut_line_coords);
draw_plot(house_top_coords, rectangle_top_coords, cut_line_coords);
endfunction
function draw_bottom()
cut_line_coords = get_cut_line_coords();
house_bottom_coords = get_bottom_coords(house_coords, cut_line_coords);
rectangle_bottom_coords = get_bottom_coords(rectangle_coords, cut_line_coords);
draw_plot(house_bottom_coords, rectangle_bottom_coords, cut_line_coords);
endfunction
draw_full();