Skip to content

Commit

Permalink
fractal graphics project
Browse files Browse the repository at this point in the history
  • Loading branch information
lxqnt committed Apr 4, 2018
1 parent 3ab61d1 commit 97a6f16
Show file tree
Hide file tree
Showing 123 changed files with 10,347 additions and 0 deletions.
77 changes: 77 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#**************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: aquint <aquint@student.42.us.org> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2017/09/05 14:04:01 by aquint #+# #+# #
# Updated: 2017/11/15 16:32:18 by aquint ### ########.fr #
# #
# **************************************************************************** #

PROGRAM = fractol
CC = gcc
CFLAGS = -Wall -Wextra -Werror
GFLAGS = -I minilibx -L minilibx -lmlx -framework OpenGL -framework AppKit
MEMFLAGS = -fsanitize=address

GREEN = \x1b[32;01m
YELLOW = \x1b[33;01m
RED = \x1b[31;01m
NO_COLOR = \x1b[0m

SRCFIL += setup.c
SRCFIL += draw_fractal.c
SRCFIL += usage_error.c
SRCFIL += key_hooks.c
SRCFIL += mouse_hooks.c
SRCFIL += fractal_types.c
SRCFIL += set_color.c

MAIN = main.c

HEADER = fractal.h

OBJ = $(SRCFIL:.c=.o)

MAX := $(words $(OBJ))
increment = $1 x
n := x
COUNTER = $(words $n)$(eval n := $(call increment, $n))

all: $(PROGRAM)

$(OBJ): %.o: %.c
@printf "$(YELLOW)\r>>COMPILING: [$(GREEN)%d$(YELLOW)/$(GREEN)%d$(YELLOW)]" $(COUNTER) $(MAX)
@$(CC) -c $(CFLAGS) $< -o $@

$(PROGRAM): $(OBJ) $(MAIN) $(HEADER)
@printf "\n"
@echo "$(YELLOW)>>CREATING LIBRARY...$(NO_COLOR)"
@$(MAKE) -C ./minilibx
@$(MAKE) -C ./libft
@echo "$(YELLOW)>>CREATING $(PROGRAM)...$(NO_COLOR)"
$(CC) $(MAIN) $(OBJ) -L libft -lft $(CFLAGS) $(GFLAGS) -o $(PROGRAM)
@echo "$(GREEN)>>DONE!"

fractolclean:
@rm -f $(OBJ)

clean:
@echo "$(RED)>>DELETING OBJECT FILES..."
$(MAKE) clean -C ./libft
@rm -f $(OBJ)
@echo "$(GREEN)>>DONE!"

fclean: clean
@$(MAKE) fclean -C ./libft
@echo "$(RED)>>DELETING $(PROGRAM)"
@rm -f $(PROGRAM)
@echo "$(GREEN)>>DONE!"

test: fractolclean all

re: fclean all

.PHONY: all clean fclean re
98 changes: 98 additions & 0 deletions draw_fractal.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* draw_fractal.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aquint <aquint@42.us.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/10/19 12:33:50 by aquint #+# #+# */
/* Updated: 2017/11/22 16:14:55 by aquint ### ########.fr */
/* */
/* ************************************************************************** */

#include "fractal.h"

void node_color(t_sketch *shp, int nd)
{
int i_i;

i_i = (int)shp->map[nd]->i;
if (i_i == 0)
shp->map[nd]->c = shp->clr->back;
else if ((i_i > 20 && i_i < 51) || (i_i > 100 && i_i < 120))
node_color_iter(shp, nd, i_i);
else
{
if ((i_i % 5) == 1)
shp->map[nd]->c = shp->clr->b;
else if ((i_i % 5) == 2)
shp->map[nd]->c = shp->clr->c;
else if ((i_i % 5) == 3)
shp->map[nd]->c = shp->clr->d;
else if ((i_i % 5) == 4)
shp->map[nd]->c = shp->clr->e;
else if ((i_i % 5) == 0)
shp->map[nd]->c = shp->clr->a;
}
}

void fractal_types(t_sketch *shp, int nd)
{
double x;
double y;

x = ((shp->map[nd]->x + shp->ox) / shp->scale);
y = ((shp->map[nd]->y + shp->oy) / shp->scale);
shp->map[nd]->i = 0;
if (shp->f_map == 3)
shp->map[nd]->i = sierpinski(x + 500, y + 500);
else if (shp->f_map == 2)
mandelbrot(shp, x, y, nd);
else if (shp->f_map == 1)
julia(shp, x, y, nd);
else if (shp->f_map == 4)
bunny(shp, x, y, nd);
else if (shp->f_map == 5)
burning_ships(shp, x, y, nd);
}

void color_set(t_sketch *shp, int nd, int color)
{
unsigned int clr;

if (color != 0)
shp->map[nd]->c = color;
clr = mlx_get_color_value(shp->mlx, shp->map[nd]->c);
shp->img_loc[nd] = clr;
}

unsigned int *img_set(t_sketch *shp, int size)
{
int bpp;
int en;

bpp = 0;
en = 1;
shp->img = mlx_new_image(shp->mlx, size, size);
shp->img_loc = (unsigned int*)mlx_get_data_addr(shp->img, &bpp, &size, &en);
return (shp->img_loc);
}

void draw_fractal(t_sketch *shp)
{
int nd;
int size;

size = 1000;
nd = 0;
img_set(shp, size);
while (nd < shp->maxnd)
{
fractal_types(shp, nd);
node_color(shp, nd);
color_set(shp, nd++, 0);
}
mlx_put_image_to_window(shp->mlx, shp->win, shp->img, 0, 0);
mlx_destroy_image(shp->mlx, shp->img);
shp->img_loc = NULL;
}
125 changes: 125 additions & 0 deletions fractal.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* fractal.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aquint <aquint@42.us.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/10/19 10:25:34 by aquint #+# #+# */
/* Updated: 2017/11/22 15:54:17 by aquint ### ########.fr */
/* */
/* ************************************************************************** */

#ifndef FRACTAL_H
# define FRACTAL_H

# include "./libft/libft.h"
# include "./minilibx/mlx.h"
# include <string.h>
# include <unistd.h>
# include <stdlib.h>
# include <math.h>

typedef struct s_color
{
int a;
int b;
int c;
int d;
int e;
int f;
int g;
int h;
int i;
int j;
int back;
} t_color;

typedef struct s_node
{
double x;
double y;
double i;
int xcor;
int ycor;
int c;
} t_node;

typedef struct s_sketch
{
t_color *clr;
t_node **map;
void *mlx;
void *win;
void *img;
int winsz;
int f_map;
int iter_m;
int maxnd;
int s_c;
float ox;
float oy;
float scale;
float c_x;
float c_y;
unsigned int *img_loc;
} t_sketch;

/*
** set_color.c
*/

void albers(t_sketch *shp);
void flavin(t_sketch *shp);
void rothko(t_sketch *shp);
void casablanca(t_sketch *shp);
void node_color_iter(t_sketch *shp, int nd, int i_i);

/*
** key_hooks.c && mouse_hooks.c
*/

void scale(t_sketch *shp, int n);
int x_win(int keycode, t_sketch *shp);
int m_win(int button, int x, int y, t_sketch *shp);
int mov_win(int x, int y, t_sketch *shp);

/*
** Usage_Error.c
*/

void print_error(int err);
void options(t_sketch *shp);
void status(t_sketch *shp);

/*
** draw_fractol.c
*/

void node_color(t_sketch *shp, int nd);
void fractal_types(t_sketch *shp, int nd);
void draw_fractal(t_sketch *shp);
unsigned int *img_set(t_sketch *shp, int size);
void color_set(t_sketch *shp, int nd, int color);

/*
** fractal_types.c
*/

void mandelbrot(t_sketch *shp, double x, double y, int nd);
void julia(t_sketch *shp, double x, double y, int nd);
int sierpinski(long x, long y);
void bunny(t_sketch *shp, double x, double y, int nd);
void burning_ships(t_sketch *shp, double x, double y, int nd);

/*
** setup.c
*/

void free_node(t_sketch *shp);
int read_input(char *str, t_sketch *shp);
void node_filler(t_sketch *shp, int nd);
int set_nodes(t_sketch *shp, int size);
int set_window(t_sketch *shp, char *str);

#endif
91 changes: 91 additions & 0 deletions fractal_types.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* fractal_types.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aquint <aquint@42.us.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/10/19 12:33:50 by aquint #+# #+# */
/* Updated: 2017/11/22 16:14:55 by aquint ### ########.fr */
/* */
/* ************************************************************************** */

#include "fractal.h"

int sierpinski(long x, long y)
{
int c;

c = 0;
x = ft_abs(x);
y = ft_abs(y);
while (x >= 1 || y >= 1)
{
if ((x % 3) == 1 && (y % 3) == 1)
return (5 + c);
x /= 3;
y /= 3;
c++;
}
return (1);
}

void mandelbrot(t_sketch *shp, double x, double y, int nd)
{
double xnew;
double x1;
double y1;

x1 = x;
y1 = y;
while ((x * x) + (y * y) <= 4 && shp->map[nd]->i < shp->iter_m)
{
xnew = (x * x) - (y * y) + ((shp->map[nd]->x + shp->ox) / shp->scale);
y = 2 * x * y + ((shp->map[nd]->y + shp->oy) / shp->scale);
x = xnew;
shp->map[nd]->i++;
}
}

void julia(t_sketch *shp, double x, double y, int nd)
{
double xnew;

while ((x * x) + (y * y) <= 4 && shp->map[nd]->i < shp->iter_m)
{
xnew = (x * x) - (y * y) + shp->c_x;
y = 2 * x * y + shp->c_y;
x = xnew;
shp->map[nd]->i++;
}
}

void bunny(t_sketch *shp, double x, double y, int nd)
{
double xnew;

while ((x * x) + (y * y) <= 4 && shp->map[nd]->i < shp->iter_m)
{
xnew = (x * x) - (y * y) - 0.123;
y = 2 * x * y + (0.745);
x = xnew;
shp->map[nd]->i++;
}
}

void burning_ships(t_sketch *shp, double x, double y, int nd)
{
double xnew;
double x1;
double y1;

x1 = x;
y1 = y;
while ((x * x) + (y * y) <= 4 && shp->map[nd]->i < shp->iter_m)
{
xnew = (x * x) - (y * y) + ((shp->map[nd]->x + shp->ox) / shp->scale);
y = fabs(2 * x * y + ((shp->map[nd]->y + shp->oy) / shp->scale));
x = fabs(xnew);
shp->map[nd]->i++;
}
}
Loading

0 comments on commit 97a6f16

Please sign in to comment.