Hi Laia,
After analyzing your code, I made some minor modifications. Afterwards, executed the modified code in matlab, at high fill levels, the fluid did fill the entire inclined rectangle.
% Define the dimensions of the rectangle length = 0.315; % length in meters height = 0.06; % height in meters width = 0.23; % width in meters
% Define the fill volume and calculate the fluid height fill_volume = 4.2; % fill volume in liters fill_volume_m3 = fill_volume * 1e-3; % convert liters to cubic meters fluid_height_upright = fill_volume_m3 / (length * width); % fluid height in meters if upright
% Calculate the maximum volume maximum_volume = height * length * width;
% Define the angle of rotation in degrees theta = 10; % angle of rotation
% Convert angle to radians for rotation matrix theta_rad = deg2rad(theta);
% Define the four corners of the rectangle x = [-length/2, length/2, length/2, -length/2]; y = [-height/2, -height/2, height/2, height/2];
% Create a rotation matrix R = [cos(theta_rad), -sin(theta_rad); sin(theta_rad), cos(theta_rad)];
% Rotate the corners of the rectangle rotated_corners = R * [x; y];
% Calculate the effective height of the fluid in the rotated frame fluid_height_inclined = fluid_height_upright / cos(theta_rad);
% Calculate the new fluid surface intersection points in the rotated frame fluid_surface_y_inclined = fluid_height_inclined - height/2;
% Inline calculation for intersections intersection_points = []; for i = 1:4 j = mod(i, 4) + 1; if (rotated_corners(2,i) <= fluid_surface_y_inclined && rotated_corners(2,j) >= fluid_surface_y_inclined) ... (rotated_corners(2,i) >= fluid_surface_y_inclined && rotated_corners(2,j) <= fluid_surface_y_inclined) t = (fluid_surface_y_inclined - rotated_corners(2,i)) / (rotated_corners(2,j) - rotated_corners(2,i)); intersection = rotated_corners(:,i) + t * (rotated_corners(:,j) - rotated_corners(:,i)); intersection_points = [intersection_points, intersection]; end end
% Determine the fluid polygon vertices based on the fill height if fill_volume_m3 <= maximum_volume % Fluid fills the entire rectangle fluid_polygon = rotated_corners; elseif fluid_height_upright <= height / 2 % Fluid forms a triangle fluid_polygon = [rotated_corners(:,1), rotated_corners(:,2), intersection_points]; else % Fluid forms a polygon (could be pentagon or other) fluid_polygon = [intersection_points, rotated_corners(:,3), rotated_corners(:,4), rotated_corners(:,1), rotated_corners(:,2)]; end
% Plot the original rectangle (for reference) figure; plot([x, x(1)], [y, y(1)], 'b--', 'LineWidth', 1.5); % dashed blue line hold on;
% Plot the rotated rectangle plot([rotated_corners(1,:), rotated_corners(1,1)], [rotated_corners(2,:), rotated_corners(2,1)], 'r-', 'LineWidth', 2); % solid red line
% Plot the fluid surface fill(fluid_polygon(1,:), fluid_polygon(2,:), 'cyan', 'FaceAlpha', 0.5, 'EdgeColor', 'none');
% Set plot limits axis equal; xlim([-length, length]); ylim([-height, height]);
% Add labels and title xlabel('X'); ylabel('Y'); legend('Original Position', 'Rotated Position', 'Fluid Level', 'Location', 'Best');
% Display grid grid on; hold off;
Please see the attached plot.

Feel free to adjust the parameters based on your preferences. Hope this will help resolve your issue.
