File: line_clip.cpp

package info (click to toggle)
mazeofgalious 0.62.dfsg2-3
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 12,668 kB
  • ctags: 803
  • sloc: cpp: 23,568; makefile: 69
file content (107 lines) | stat: -rw-r--r-- 2,189 bytes parent folder | download | duplicates (6)
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include "math.h"
#include "stdio.h"

bool line_clip(int &x1,int &y1,int &x2,int &y2,int min_x,int min_y,int max_x,int max_y)
{
	int dx=x2-x1,dy=y2-y1;
	float f;

	/* Recortar por x_min: */ 
	if (x1<min_x) {
		if (x2<min_x) {
			return false;
		} else {
			/* Buscar el punto de corte: */ 
			f=float(dy)/float(dx);
			x1=min_x;
			y1=int(f*min_x+(y1-x1*f));
		} /* if */ 
	} else {
		if (x2<min_x) {
			/* Buscar el punto de corte: */ 
			f=float(dy)/float(dx);
			x2=min_x;
			y2=int(f*min_x+(y1-x1*f));
		} else {
			/* Nada que hacer... */ 
		} /* if */ 
	} /* if */ 

	/* Recortar por x_max: */ 
	if (x1>=max_x) {
		if (x2>=max_x) {
			return false;
		} else {
			/* Buscar el punto de corte: */ 
			f=float(dy)/float(dx);
			x1=max_x-1;
			y1=int(f*(max_x-1)+(y1-x1*f));
		} /* if */ 
	} else {
		if (x2>max_x) {
			/* Buscar el punto de corte: */ 
			f=float(dy)/float(dx);
			x2=max_x-1;
			y2=int(f*(max_x-1)+(y1-x1*f));
		} else {
			/* Nada que hacer... */ 
		} /* if */ 
	} /* if */ 

	/* Recortar por y_min: */ 
	if (y1<min_y) {
		if (y2<min_y) {
			return false;
		} else {
			/* Buscar el punto de corte: */ 
			f=float(dx)/float(dy);
			y1=min_y;
			x1=int(f*min_y+(x1-y1*f));
		} /* if */ 
	} else {
		if (y2<min_y) {
			/* Buscar el punto de corte: */ 
			f=float(dx)/float(dy);
			y2=min_y;
			x2=int(f*min_y+(x1-y1*f));
		} else {
			/* Nada que hacer... */ 
		} /* if */ 
	} /* if */ 

	/* Recortar por y_max: */ 
	if (y1>=max_y) {
		if (y2>=max_y) {
			return false;
		} else {
			/* Buscar el punto de corte: */ 
			f=float(dx)/float(dy);
			y1=max_y-1;
			x1=int(f*(max_y-1)+(x1-y1*f));
		} /* if */ 
	} else {
		if (y2>=max_y) {
			/* Buscar el punto de corte: */ 
			f=float(dx)/float(dy);
			y2=max_y-1;
			x2=int(f*(max_y-1)+(x1-y1*f));
		} else {
			/* Nada que hacer... */ 
		} /* if */ 
	} /* if */ 

	if (x1<min_x) x1=min_x;
	if (x2<min_x) x2=min_x;

	if (x1>=max_x) x1=max_x-1;
	if (x2>=max_x) x2=max_x-1;

	if (y1<min_y) y1=min_y;
	if (y2<min_y) y2=min_y;

	if (y1>=max_y) y1=max_y-1;
	if (y2>=max_y) y2=max_y-1;

	return true;
} /* line_clip */