-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclipper.c
89 lines (82 loc) · 2.46 KB
/
clipper.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* clipper.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jjacobso <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/05/20 21:11:29 by jjacobso #+# #+# */
/* Updated: 2019/05/20 21:11:40 by jjacobso ### ########.fr */
/* */
/* ************************************************************************** */
#include "fdf.h"
static int compare(double x, double y, char sign)
{
if (sign == '>')
return (x >= y);
if (sign == '<')
return (x <= y);
return (0);
}
void clip_x(t_coord *s, t_coord *e, double x, char sign)
{
if (compare(s->x, x, sign))
{
s->y = floor(fy(x, *s, *e));
s->x = x;
}
if (compare(e->x, x, sign))
{
e->y = floor(fy(x, *s, *e));
e->x = x;
}
}
void clip_y(t_coord *s, t_coord *e, double y, char sign)
{
if (compare(s->y, y, sign))
{
s->x = floor(fx(y, *s, *e));
s->y = y;
}
if (compare(e->y, y, sign))
{
e->x = floor(fx(y, *s, *e));
e->y = y;
}
}
int clip_line(t_view *view, t_coord *s, t_coord *e)
{
int clipped;
clipped = 1;
if (intersect(view->clip_space.a, view->clip_space.b, *s, *e))
{
clip_y(s, e, view->clip_space.a.y, '<');
clipped = 0;
}
if (intersect(view->clip_space.b, view->clip_space.c, *s, *e))
{
clip_x(s, e, view->clip_space.b.x, '>');
clipped = 0;
}
if (intersect(view->clip_space.c, view->clip_space.d, *s, *e))
{
clip_y(s, e, view->clip_space.c.y, '>');
clipped = 0;
}
if (intersect(view->clip_space.d, view->clip_space.a, *s, *e))
{
clip_x(s, e, view->clip_space.d.x, '<');
clipped = 0;
}
return (clipped);
}
int invisible(t_view *view, t_coord *s, t_coord *e)
{
if ((!ft_finrange(s->x, view->clip_space.a.x, view->clip_space.c.x)
|| !ft_finrange(s->y, view->clip_space.a.y, view->clip_space.c.y))
&& (!ft_finrange(e->x, view->clip_space.a.x, view->clip_space.c.x)
|| !ft_finrange(e->y, view->clip_space.a.y, view->clip_space.c.y)))
return (clip_line(view, s, e));
clip_line(view, s, e);
return (0);
}