카테고리 없음
[FDF] ft_make_draw_dot
Or71nH
2022. 6. 28. 00:41
일단 2차원 을 그려보자
https://github.com/liebespaar93/FDF
GitHub - liebespaar93/FDF
Contribute to liebespaar93/FDF development by creating an account on GitHub.
github.com
만들어야 할거
- 지금 위치와 다음 위치를 선을 그어야 한다
- 선을 그을떄 비율을 계산해서 긴거에 맞게 그려준다
- 선에 값에 따라 색을 만들어준다
일단 선이 이어져 있는곳을 다 찾는다
#include <ft_draw.h>
#include <stdio.h>
#define COLOR_RATIO 5;
t_dot ft_symtax_pixel(t_dot dot)
{
dot.x *= LINE_SIZE;
dot.y *= LINE_SIZE;
dot.z *= COLOR_RATIO;
return (dot);
}
void ft_draw_dot_auto(t_param *param, t_dot *head_dot)
{
t_dot *tmp_dot;
while (head_dot)
{
tmp_dot = head_dot;
while (tmp_dot)
{
if (tmp_dot->x_p)
ft_auto_line_draw(param, ft_symtax_pixel(*tmp_dot), ft_symtax_pixel(*(tmp_dot->x_p)));
if (tmp_dot->y_p)
ft_auto_line_draw(param, ft_symtax_pixel(*tmp_dot), ft_symtax_pixel(*(tmp_dot->y_p)));
tmp_dot = tmp_dot->x_p;
}
head_dot = head_dot->y_p;
}
}
지금 위치와 목적지를 구하여 하나하나 점을 넣어준다
#include <ft_draw.h>
void ft_auto_pixel_put(t_param *param, t_dot position_dot, int color)
{
mlx_pixel_put(param->mlx_ptr, param->win_ptr, position_dot.x, position_dot.y + 10, ft_draw_color(color));
}
void ft_auto_line_draw(t_param *param, t_dot position_dot, t_dot next_dot)
{
float x_allow;
float y_allow;
float z_allow;
t_ratio c_ratio;
ft_ratio_init(&c_ratio, position_dot, next_dot);
x_allow = -ft_sign(position_dot.x - next_dot.x);
y_allow = -ft_sign(position_dot.y - next_dot.y);
z_allow = -ft_sign(position_dot.z - next_dot.z);
if (ft_abs(position_dot.x - next_dot.x) > ft_abs(position_dot.y - next_dot.y))
{
while (position_dot.x != next_dot.x)
{
ft_auto_pixel_put(param, position_dot, position_dot.z);
position_dot.x += x_allow;
position_dot.y += y_allow * c_ratio.y;
position_dot.z += z_allow * c_ratio.z;
}
}
else
{
while (position_dot.y != next_dot.y)
{
ft_auto_pixel_put(param, position_dot, position_dot.z);
position_dot.y += y_allow;
position_dot.x += x_allow * c_ratio.x;
position_dot.z += z_allow * c_ratio.z;
}
}
}
더 긴부분을 참고하여 비율을 만든다
#include <ft_draw.h>
t_ratio ft_ratio_init(t_ratio *c_ratio, t_dot position_dot, t_dot next_dot)
{
c_ratio->x = ft_ratio(position_dot.x - next_dot.x, position_dot.y - next_dot.y);
c_ratio->y = ft_ratio(position_dot.y - next_dot.y, position_dot.x - next_dot.x);
if (ft_abs(position_dot.x - next_dot.x) > ft_abs(position_dot.y - next_dot.y))
c_ratio->z = ft_ratio(position_dot.z - next_dot.z, position_dot.x - next_dot.x);
else
c_ratio->z = ft_ratio(position_dot.z - next_dot.z, position_dot.y - next_dot.y);
return (*c_ratio);
}
이제 넣을 곳에 색을 비율에 맞게 넣어준다
#include <ft_draw.h>
int ft_draw_color(int num)
{
t_color c_color;
int flag;
flag = 1;
c_color.num = 0x0000FF00;
if (num > 0)
{
while (num--)
{
if (c_color.s_bit.r < 0xFF)
c_color.s_bit.r++;
else if (c_color.s_bit.g > 0 && c_color.s_bit.b == 0)
c_color.s_bit.g--;
else if (c_color.num < 0x00FFFFFF && ++c_color.s_bit.b)
c_color.s_bit.g++;
else
break ;
}
}
else
{
while (num++)
{
if (c_color.s_bit.b < 0xFF)
c_color.s_bit.b++;
else if (c_color.s_bit.g > 0)
c_color.s_bit.g--;
else if (c_color.num > 0x00000000)
c_color.s_bit.b--;
else
break ;
}
}
return (c_color.num);
}
색상은 원하는걸로 만들기 순차적으로 오르거나 내리는것이 이쁘다
끝!