Refactor move_img function to comply with the Norm
authorLukas Jiriste <ljiriste@student.42prague.com>
Fri, 26 Apr 2024 08:06:15 +0000 (10:06 +0200)
committerLukas Jiriste <ljiriste@student.42prague.com>
Fri, 26 Apr 2024 08:06:15 +0000 (10:06 +0200)
I think this is function would better be left alone
but I have to comply.

src/main.c

index 28e26c0aeb95bbfbfdadedda871477355258027d..2163fc92b25930c0f8169ca7723e7e07cabb4ae1 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2023/10/27 14:29:26 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/04/26 09:41:56 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/04/26 09:59:47 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -138,6 +138,15 @@ void       change_zoom(t_session *s, t_vect2 invariant, double d_zoom)
        return ;
 }
 
+void   move_pixel(t_img *img, t_vect2 old, t_vect2 new)
+{
+       *(double *)ft_mat_access(&img->base, new.y, new.x)
+               = *(double *)ft_mat_access(&img->base, old.y, old.x);
+       *(bool *)ft_mat_access(&img->calced, new.y, new.x)
+               = *(bool *)ft_mat_access(&img->calced, old.y, old.x);
+       return ;
+}
+
 void   move_img(t_img *img, int delta_x, int delta_y)
 {
        int             x;
@@ -155,12 +164,8 @@ void       move_img(t_img *img, int delta_x, int delta_y)
                {
                        if (0 <= x - delta_x && x - delta_x < img->width
                                && 0 <= y - delta_y && y - delta_y < img->height)
-                       {
-                               *(double *)ft_mat_access(&img->base, y - delta_y, x - delta_x)
-                                       = *(double *)ft_mat_access(&img->base, y, x);
-                               *(bool *)ft_mat_access(&img->calced, y - delta_y, x - delta_x) =
-                               *(bool *)ft_mat_access(&img->calced, y, x);
-                       }
+                               move_pixel(img, (t_vect2){.x = x, .y = y},
+                                       (t_vect2){.x = x - delta_x, .y = y - delta_y});
                        if (!(0 <= x + delta_x && x + delta_x < img->width
                                        && 0 <= y + delta_y && y + delta_y < img->height))
                                *(bool *)ft_mat_access(&img->calced, y, x) = false;