Exemple #1
1
void ft_bresenham(t_id *s)
{
	s->dx = ft_abs(s->x1 - s->x0);
	s->dy = ft_abs(s->y1 - s->y0);
	s->sx = s->x0 < s->x1 ? 1 : -1;
	s->sy = s->y0 < s->y1 ? 1 : -1;
	s->err = (s->dx > s->dy ? s->dx : -s->dy) / 2;
	while (1)
	{	
 	   	mlx_image_put_pixel(s, s->x0, s->y0);
    	if (s->x0 == s->x1 && s->y0 == s->y1)
    		break;
   		s->e2 = s->err;
    	if (s->e2 > -s->dx)
    	{
    		s->err-= s->dy;
    		s->x0+= s->sx;
    	}
    	if (s->e2 < s->dy)
    	{
    		s->err+= s->dx;
    		s->y0+= s->sy; 
    	}
	}
}
Exemple #2
1
void	ft_new_draw_algo(t_pts *pts, t_mlx *mlx)
{
	float	incl;
	int		i;
	int		px;
	int		py;

	i = 0;
	if ((ft_abs(pts->mx2 - pts->mx1) >= ft_abs(pts->my2 - pts->my1)
		 && pts->mx1 != pts->mx2) || pts->my1 == pts->my2)
	{
		incl = (float)(pts->my2 - pts->my1) / (float)(pts->mx2 - pts->mx1);
		while ((i += sgn(pts->mx2 - pts->mx1)) != (pts->mx2 - pts->mx1))
		{
			py = incl * i + pts->my1;
			mlx_pixel_put(mlx->mlx, mlx->win, (i + pts->mx1), py, 0xffffff);
		}
	}
	else
	{
		incl = (float)(pts->mx2 - pts->mx1) / (float)(pts->my2 - pts->my1);
		while ((i += sgn(pts->my2 - pts->my1)) != (pts->my2 - pts->my1))
		{
			px = incl * i + pts->mx1;
			mlx_pixel_put(mlx->mlx, mlx->win, px, (i + pts->my1), 0xffffff);
		}
	}
}
Exemple #3
0
void		draw_floor(t_env *e, t_trac_data *d)
{
	int				floor_tex_x;
	int				floor_tex_y;
	int				y;
	t_colors		col;

	d->dist_wall = d->perp_wall_dist;
	d->dist_player = 0.0;
	if (d->draw_end < 0)
		d->draw_end = SCREEN_HEIGHT;
	y = d->draw_end - 1;
	while (++y < SCREEN_HEIGHT)
	{
		d->current_dist = SCREEN_HEIGHT / (2.0 * y - SCREEN_HEIGHT);
		d->weight = (d->current_dist - d->dist_player)
			/ (d->dist_wall - d->dist_player);
		d->current_floor_x = d->weight * d->floor_x_wall
			+ (1.0 - d->weight) * e->p.pos_x;
		d->current_floor_y = d->weight * d->floor_y_wall
			+ (1.0 - d->weight) * e->p.pos_y;
		floor_tex_x = ft_abs((int)(d->current_floor_x * TEX_WIDTH) % TEX_WIDTH);
		floor_tex_y = ft_abs((int)(d->current_floor_y
									* TEX_HEIGHT) % TEX_HEIGHT);
		floor_pixel_color(e, floor_tex_x, floor_tex_y, &col);
		print_floor(e, d, &col, y);
	}
}
Exemple #4
0
char			*ft_itoa_base(int value, int base)
{
	char	*str;
	int		i;
	int		isneg;

	if (base < 2 || base > 16 || value == 0)
		return (ft_returnzero());
	if (!(str = ft_allocstr(value, base)))
		return (NULL);
	i = 0;
	isneg = 0;
	if (base == 10 && value < 0)
		isneg = 1;
	while (value != 0)
	{
		if (value % base <= 9)
			str[i++] = ft_abs(value % base) + '0';
		else
			str[i++] = (ft_abs(value % base) - 10) + 'a';
		value /= base;
	}
	if (isneg)
		str[i++] = '-';
	str[i] = '\0';
	ft_strrev(str);
	return (str);
}
static void	line(t_glob *g)
{
	t_ligne	l;

	l.dx = ft_abs(g->e->x_1 - g->e->x_0);
	l.sx = g->e->x_0 < g->e->x_1 ? 1 : -1;
	l.dy = ft_abs(g->e->y_1 - g->e->y_0);
	l.sy = g->e->y_0 < g->e->y_1 ? 1 : -1;
	l.err = (l.dx > l.dy ? l.dx : -l.dy) / 2;
	while (1)
	{
		ft_pixel_put_image(g->e->img[0], g->e->x_0, g->e->y_0, 0x808080);
		if (g->e->x_0 == g->e->x_1 && g->e->y_0 == g->e->y_1)
			break ;
		l.e2 = l.err;
		if (l.e2 > -l.dx)
		{
			l.err -= l.dy;
			g->e->x_0 += l.sx;
		}
		if (l.e2 < l.dy)
		{
			l.err += l.dx;
			g->e->y_0 += l.sy;
		}
	}
}
Exemple #6
0
void			calc_light(t_param *param, t_info *info, t_list *spot)
{
	t_info	*light;
	t_spot	*o_spot;
	t_lum	lum;
	int		*s_color;

	if (info->distance < 0)
		return ;
	s_color = init_color();
	while (spot)
	{
		o_spot = (t_spot *)spot->content;
		light = init_light(info, o_spot);
		calc_intersection(param, light);
		if (point_cmp(info->r_pos, light->r_pos) == 1)
		{
			lum.fading = ft_abs(calc_fading(light->r_line.vec, info->vec_n));
			lum.shining = ft_abs(calc_shining(info->vec_n, light->r_line.vec));
			info->light += o_spot->value * lum.fading;
			info->light += o_spot->value * lum.shining * lum.fading;
			calc_color(&s_color, o_spot->color, o_spot->value, lum.fading);
		}
		spot = spot->next;
	}
	info->color = retrieve_col(s_color, damer(param, info, info->s_pos),
		get_shine(info));
}
Exemple #7
0
int			mlx_putline(t_mlxdata *data, t_ipoint a, t_ipoint b)
{
	int x;

	if (a.x > WIDTH || b.x > WIDTH || a.y > HEIGHT || b.y > HEIGHT
		|| a.x < 0 || b.x < 0 || a.y < 0 || b.y < 0)
		return (0);
	b.color = a.color;
	if (ft_abs(b.y - a.y) > ft_abs(b.x - a.x))
	{
		if (a.y > b.y)
			swap_points(&a, &b);
		swap_xy(&a);
		swap_xy(&b);
		x = a.x;
		while (x <= b.x)
		{
			mlx_putpxl_img(data->img,
						(int)(a.y + ((b.y - a.y) * (x - a.x)) / (b.x - a.x)),
						(int)x, a.color);
			x++;
		}
	}
	else
		mlx_putlinetwo(data, a, b);
	return (1);
}
char	*ft_itoa_base_long(long long value, int base)
{
	int			len;
	int			i;
	char		*ret;
	long long	tmp;

	if (base < 2 || base > 16)
		return (NULL);
	len = 1;
	tmp = value;
	while ((tmp = tmp / base))
		len++;
	if (!(ret = (char*)malloc(sizeof(char) * len + 1)))
		return (NULL);
	ret[len--] = '\0';
	while (len >= 0 && value / base != 0)
	{
		i = ft_abs(value % base);
		ret[len] = i < 10 ? i + '0' : i + '7';
		value /= base;
		len--;
	}
	i = ft_abs(value % base);
	ret[len--] = i < 10 ? i + '0' : i + '7';
	return (ret);
}
Exemple #9
0
void			line(t_pos *p1, t_pos *p2, t_env *map)
{
	t_cor t;

	t.dx = ft_abs(p2->x - p1->x);
	t.sx = p1->x < p2->x ? 1 : -1;
	t.dy = ft_abs(p2->y - p1->y);
	t.sy = p1->y < p2->y ? 1 : -1;
	t.err = (t.dx > t.dy ? t.dx : -t.dy) / 2;
	while (1)
	{
		mlx_pixel_put(map->mlx, map->win, p1->x, p1->y, map->color);
		if (p1->x == p2->x && p1->y == p2->y)
			break ;
		t.e2 = t.err;
		if (t.e2 > -t.dx)
		{
			t.err -= t.dy;
			p1->x += t.sx;
		}
		if (t.e2 < t.dy)
		{
			t.err += t.dx;
			p1->y += t.sy;
		}
	}
}
Exemple #10
0
void	conv_i(t_spec *spec, t_print *print)
{
	int			i;
	int			j;
	intmax_t	p;

	p = get_param(spec->hljz, print->ap);
	if (p >= -9223372036854775807)
	{
		i = (p < 0) || (spec->flags & 3);
		j = !(spec->prec || p) ? 0
			: (ft_max(spec->prec + i, ft_nbrlen(ft_abs(p)) + i));
		++spec->mfw;
		(spec->flags & E_ZERO) ? apply_plusspace(print, spec, p >= 0)
			: --spec->mfw;
		(spec->flags & E_ZERO && p > 0) ? --spec->prec : 0;
		(!(spec->flags & E_DASH)) ? apply_mfw(print, spec, spec->mfw - j) : 0;
		!(spec->flags & E_ZERO) ? apply_plusspace(print, spec, p >= 0) : 0;
		apply_numprec(print, spec, ft_nbrlen(ft_abs(p)));
		(!spec->prec && !p) ? 0 : ft_sitoa(p, print, ft_nbrlen(ft_abs(p)));
		j = (spec->flags & 3) && (p > 0);
		(spec->flags & E_DASH) ? apply_mfw(print, spec, spec->mfw +
			(p >= 0) - j) : 0;
	}
}
Exemple #11
0
int		ft_putnbr_base(long nb, const char *base)
{
	long			div;
	long			i;
	long			result;
	long			size_base;

	i = 0;
	size_base = ft_strlen(base);
	if (nb < 0)
	{
		ft_putchar('-');
		i++;
	}
	div = 1;
	while (ft_abs((nb / div)) >= size_base)
		div = div * size_base;
	while (div > 0)
	{
		result = ft_abs((nb / div) % size_base);
		ft_putchar(base[result]);
		div = div / size_base;
		i++;
	}
	return (i);
}
Exemple #12
0
void	ft_putnbr_fd(int n, int fd)
{
	if (n < 0)
		ft_putchar_fd('-', fd);
	if (n > 9 || n < -9)
		ft_putnbr_fd(ft_abs(n / 10), fd);
	ft_putchar_fd(ft_abs(n % 10) + 48, fd);
}
Exemple #13
0
t_frac ft_reduce(t_frac frac)
{
    t_frac result;
    int pgcd;
    int sign;

    pgcd = ft_pgcd(frac.nom, frac.denom);
    sign = (frac.nom * frac.denom < 0 ? -1 : 1);
    result.nom = ft_abs(frac.nom) / pgcd * sign;
    result.denom = ft_abs(frac.denom) / pgcd;

    return result;
}
int				ft_ultimate_range(int **range, int min, int max)
{
	int		i;
	int		*result;

	if (min >= max || !(result = (int*)malloc(sizeof(int) * ft_abs(max - min))))
		return (0);
	i = 0;
	while (i++ < ft_abs(max - min))
		result[i - 1] = (min + i - 1);
	*range = result;
	return (ft_abs(max - min));
}
Exemple #15
0
t_line_data		*new_line_data(t_env *e, int x, int y, int x2, int y2)
{
	t_line_data *l;

	if (!(l = (t_line_data*)malloc(sizeof(t_line_data))))
		return (NULL);
	l->dx = ft_abs(x2 - x);
	l->dy = ft_abs(y2 -y);
	l->ix = (x < x2) ? 1 : -1;
	l->iy = (y < y2) ? 1 : -1;
	l->err = (l->dx > l->dy) ? l->dx /2 : -l->dy /2;
	return (l);
}
Exemple #16
0
void	ft_putnbr(int n)
{
	if (n >= 10 || n <= -10)
	{
		ft_putnbr(n / 10);
		ft_putnbr(ft_abs(n % 10));
	}
	else
	{
		if (n < 0)
			ft_putchar('-');
		ft_putchar('0' + ft_abs(n));
	}
}
Exemple #17
0
int		ft_contiguity(int x, int y, int x_p0, int y_p0)
{
	int	p;
	int	p0;
	int	p0_diff;

	p0 = x_p0 + y_p0;
	p = x + y;
	p0_diff = p - p0;
	p = ft_abs(x) + ft_abs(y);
	if (p0_diff > 1 || p0_diff < -1 || p > 3)
		return (0);
	return (1);
}
Exemple #18
0
static void		ft_fill_cone(t_rtv1 *rtv1, char **tab, char **tri, int i)
{
	char	*hexa;

	rtv1->obj.cone[i].posx = ft_atoid(tri[0]);
	rtv1->obj.cone[i].posy = ft_atoid(tri[1]);
	rtv1->obj.cone[i].posz = ft_atoid(tri[2]);
	rtv1->obj.cone[i].base_size = ft_atoid(tab[1]);
	rtv1->obj.cone[i].height = ft_atoid(tab[2]);
	if ((hexa = ft_strstr(tab[3], "0x")))
		rtv1->obj.cone[i].color = ft_abs(ft_atoi_base(&hexa[2], 16));
	else
		rtv1->obj.cone[i].color = ft_abs(ft_atoi_base(tab[3], 16));

}
Exemple #19
0
void			ft_cut_select(t_it *it)
{
	int		length;
	int		temp;
	int		a;
	int		b;

	tputs(tgetstr("me", NULL), 0, my_putchar);
	it->r_video = 0;
	length = ft_abs(it->i - it->saved_i) + 1;
	temp = it->i;
	a = it->i - it->offset;
	b = it->saved_i - it->offset;
	move_begin(it);
	if (temp > it->saved_i)
	{
		ft_memdel((void**)&it->tmp_buffer);
		it->tmp_buffer = ft_strsub(it->line, b, length);
		cut_selection(b, a, it);
	}
	else
	{
		ft_memdel((void**)&it->tmp_buffer);
		it->tmp_buffer = ft_strsub(it->line, a, length);
		cut_selection(a, b, it);
	}
	tputs(tgetstr("cd", NULL), 0, my_putchar);
	ft_putstr(it->line);
	it->i += ft_strlen(it->line);
}
Exemple #20
0
static void		ft_replace_vid(t_it *it, int rewrite)
{
	int		length;
	int		temp;

	if (rewrite)
	{
		tputs(tgetstr("mr", NULL), 0, my_putchar);
		ft_putchar(it->line[(it->i - return_offset() - 2)]);
		tputs(tgetstr("le", NULL), 0, my_putchar);
	}
	else
	{
		temp = it->i;
		tputs(tgetstr("me", NULL), 0, my_putchar);
		length = ft_abs(it->i - it->saved_i) + 1;
		ft_move_saved(it);
		if (temp > it->saved_i)
		{
			ft_memdel((void**)&it->tmp_buffer);
			it->tmp_buffer = ft_strsub(it->line,
				((it->i - return_offset() - 2)), length);
		}
		else
		{
			ft_memdel((void**)&it->tmp_buffer);
			it->tmp_buffer = ft_strsub(it->line,
				((temp - return_offset() - 2)), length);
		}
		ft_putstr(it->tmp_buffer);
		it->i += ft_strlen(it->tmp_buffer);
	}
}
Exemple #21
0
char			*ft_itoa(int n)
{
	char	*str;
	int		len;
	int		sign;

	len = ft_nblen(n);
	sign = (n < 0 ? 1 : 0);
	str = NULL;
	str = (char*)malloc(sizeof(char) * (len + sign + 1));
	if (str)
	{
		str = str + len + sign;
		*str = '\0';
		if (n == 0)
			*--str = '0';
		while (n != 0)
		{
			*--str = ft_abs(n % 10) + 48;
			n = n / 10;
		}
		if (sign)
			*--str = '-';
	}
	return (str);
}
Exemple #22
0
int		ft_check(int row, int column)	// check if the position is valid or not
{
	int		i;

	i = 1;
	while (i <= row - 1)
	{
		if (board[i] == column)
			return (0);
		else
			if (ft_abs(board[i] - column) == ft_abs(i - row))
				return (0);
		i++;
	}
	return (1);
}
Exemple #23
0
char			*ft_itoabase(int n, int base, int maj)
{
	int			psc;
	int			res;
	int			i;
	char		*str;

	psc = 0;
	i = 0;
	res = (n < 0) ? -1 : 1;
	n = ft_abs(n);
	while (n >= puissance(base, psc))
		psc++;
	psc--;
	str = ft_strnew(psc + 1);
	if (n == 0 || base == 0)
		str[0] = '0';
	res == -1 ? str[i++] = '-' : 0;
	while (psc >= 0)
	{
		str[i] = how_n(&n, base, psc, maj);
		psc--;
		i++;
	}
	return (str);
}
Exemple #24
0
int			det_paths2(int nant, t_l_data *d, int **paths, int ibck)
{
	int	tot;

	d->i2 = 0;
	if (paths[d->i + 1])
		d->i2 = ft_abs(paths[d->i][0] - paths[d->i + 1][0]);
	ibck = d->i;
	d->i = -1;
	tot = 0;
	while (++d->i <= ibck && d->i2 && nant)
	{
		d->i = d->i > ibck ? 0 : d->i;
		if (nant - d->i2 >= 0)
		{
			nant -= d->i2;
			d->plen[d->i] += d->i2;
		}
		else if (nant - 1 >= 0)
		{
			add_to_dplen(tot, d, nant);
		}
	}
	d->i = ibck;
	d->i++;
	return (nant);
}
Exemple #25
0
char		*ft_itoa(int n)
{
	int		nblen;
	int		sign;
	char	*res;

	sign = ft_signint(n);
	nblen = ft_lenint(n);
	res = NULL;
	res = (char *)malloc(sizeof(char) * (nblen + sign + 1));
	if (res)
	{
		res = res + nblen + sign;
		*res = '\0';
		if (!n)
			*--res = '0';
		while (n != 0)
		{
			*--res = ft_abs(n % 10) + '0';
			n = n / 10;
		}
		if (sign)
			*--res = '-';
	}
	return (res);
}
/* checking for attacks */
int	ft_check(int row, int col)
{
	int	i;

	i = 1;
	while (i <= row - 1)
	{
		if (board[i] == col)
			return (0);
		else
			if (ft_abs(board[i] - col) == ft_abs(i - row))
				return (0);
	i++;
	}
	return (1); /* no attacks */
}
Exemple #27
0
int	is_pseudo_ordered(t_s *list)
{
	int	ret;
	int	tmpval;
	int	firstval;
	t_s	*tmp;

	ret = 0;
	tmp = list;
	tmpval = tmp->val;
	firstval = tmpval;
	while (tmp)
	{
		while ((tmp->val) <= tmp->n->val)
		{
			tmp = tmp->n;
			if (tmp->n->val == tmpval)
				return (l(list) - ret > ret ? ret : -(ft_abs(l(list) - ret)));
		}
		list = list->n;
		tmp = list;
		tmpval = tmp->val;
		ret++;
		if (tmpval == firstval)
			return (0);
	}
	return (ret);
}
Exemple #28
0
void		calculating_distances(t_env *e, t_trac_data *d, t_obj *ray)
{
	if (d->side == 0)
		d->perp_wall_dist = fabs((d->map_x - ray->pos_x
									+ (1 - d->step_x) / 2) / ray->dir_x);
	else
		d->perp_wall_dist = fabs((d->map_y - ray->pos_y
									+ (1 - d->step_y) / 2) / ray->dir_y);
	d->line_height = ft_abs((int)(SCREEN_HEIGHT / d->perp_wall_dist));
	d->wall_type = ft_atoi(e->world[d->map_x][d->map_y]);
	if (d->side == 1)
		d->wall_x = ray->pos_x
			+ ((d->map_y - ray->pos_y
				+ (1 - d->step_y) / 2) / ray->dir_y) * ray->dir_x;
	else
		d->wall_x = ray->pos_y
			+ ((d->map_x - ray->pos_x
				+ (1 - d->step_x) / 2) / ray->dir_x) * ray->dir_y;
	d->wall_x -= floor((d->wall_x));
	d->tex_x = (int)(d->wall_x * (double)(TEX_WIDTH));
	if (d->side == 0 && ray->dir_x > 0)
		d->tex_x = TEX_WIDTH - d->tex_x - 1;
	if (d->side == 1 && ray->dir_y < 0)
		d->tex_x = TEX_WIDTH - d->tex_x - 1;
}
Exemple #29
0
void		calc_door_dist(t_env *e, t_trac_data *d, t_obj *ray)
{
	d->door_hit = 1;
	if (d->side == 0)
		d->perp_door_dist = fabs((d->map_x - ray->pos_x
									+ (1 - d->step_x) / 2) / ray->dir_x);
	else
		d->perp_door_dist = fabs((d->map_y - ray->pos_y
									+ (1 - d->step_y) / 2) / ray->dir_y);
	d->dline_height = ft_abs((int)(SCREEN_HEIGHT / d->perp_door_dist));
	d->door_type = ft_atoi((char *)&e->world[d->map_x][d->map_y][1]);
	if (d->side == 1)
		d->door_x = ray->pos_x
			+ ((d->map_y - ray->pos_y
				+ (1 - d->step_y) / 2) / ray->dir_y) * ray->dir_x;
	else
		d->door_x = ray->pos_y
			+ ((d->map_x - ray->pos_x
				+ (1 - d->step_x) / 2) / ray->dir_x) * ray->dir_y;
	d->door_x -= floor((d->door_x));
	d->dtex_x = (int)(d->door_x * (double)(TEX_WIDTH));
	if (d->side == 0 && ray->dir_x > 0)
		d->dtex_x = TEX_WIDTH - d->dtex_x - 1;
	if (d->side == 1 && ray->dir_y < 0)
		d->dtex_x = TEX_WIDTH - d->dtex_x - 1;
}
Exemple #30
0
void				ft_modulo_key(int *n, const int max, const int dir)
{
	if (!dir)
		*n = (*n - 1) < 0 ? max - 1 : *n - 1;
	if (dir)
		*n = ft_abs((*n + 1) % max);
}