1 
2 #include "Quad.h"
3 #include <stdio.h>
4 
5 #ifndef M_2PI
6 #define M_2PI 6.28318
7 #endif
8 
IsInside(Vector s)9 int Quad::IsInside(Vector s)
10 {
11 	float angle=0;
12 	Vector d1,d2;
13 
14 	d1=s-v1;
15 	d2=s-v2;
16 	angle+=d1%d2;
17 	d1=s-v3;
18 	angle+=d1%d2;
19 	d2=s-v4;
20 	angle+=d1%d2;
21 	d1=s-v1;
22 	angle+=d1%d2;
23 
24 	if(angle>0.99f*M_2PI){
25 /*		printf("Quad::IsInside: angle=%f\n",angle);
26 		printf("Quad::IsInside: I think that\n");
27 		printf("\t%f,%f,%f is inside\n",s.x,s.y,s.z);
28 		printf("\t%f,%f,%f\n",v1.x,v1.y,v1.z);
29 		printf("\t%f,%f,%f\n",v2.x,v2.y,v2.z);
30 		printf("\t%f,%f,%f\n",v3.x,v3.y,v3.z);
31 		printf("\t%f,%f,%f\n",v4.x,v4.y,v4.z);
32 */
33 		return 1;
34 	}
35 
36 	return 0;
37 }
38 
PlaneIntersection(Vector p1,Vector p2)39 int Quad::PlaneIntersection(Vector p1,Vector p2)
40 {
41   float d1,d2,dp;
42 	Vector linedir;
43 
44 	linedir=p2-p1;
45 	if((linedir|n) > 0)
46 		return 0;
47 
48 	dp=-n|v1;//where v1 is some arbitrary point on the plane
49 
50 	d1=(p1|n)+dp;
51 	d2=(p2|n)+dp;
52 
53 	if(d1*d2>=0)
54 		return 0;
55 	else
56 		return 1;
57 
58 }
59 
60 
PlaneIntersection2Way(Vector p1,Vector p2)61 int Quad::PlaneIntersection2Way(Vector p1,Vector p2)
62 {
63   float d1,d2,dp;
64 
65 	dp=-n|v1;//where v1 is some arbitrary point on the plane
66 
67 	d1=(p1|n)+dp;
68 	d2=(p2|n)+dp;
69 
70 	if(d1*d2>=0)
71 		return 0;
72 	else
73 		return 1;
74 
75 }
76 
PlaneIntersectionPoint(Vector p1,Vector p2)77 Vector Quad::PlaneIntersectionPoint(Vector p1,Vector p2)
78 {
79 	Vector linedir;
80 	Vector retval;
81 
82 	linedir=p2-p1;
83 	linedir.Unitize();
84 
85 	float numerator=-((n|p1)-(n|v1));
86 	float denominator=n|linedir;
87 	if(denominator==0.0)
88 		return p1;
89 	float dist=numerator/denominator;
90 
91 	retval=p1+dist*linedir;
92 
93 	return retval;
94 }
95 
SlideVector(Vector v)96 Vector Quad::SlideVector(Vector v)
97 {
98 	Vector retval;
99 	float	vdotn=v|n;
100 	retval=v;
101 	retval=v-n*vdotn;
102 
103 	return retval;
104 }
105 
BounceVector(Vector v)106 Vector Quad::BounceVector(Vector v)
107 {
108 	Vector retval;
109 	float	vdotn=v|n;
110 	retval=v;
111 	retval=v-2*n*vdotn;
112 
113 	return retval;
114 }
115 
BounceVector(Vector v,float res)116 Vector Quad::BounceVector(Vector v,float res)
117 {
118 	Vector retval;
119 	float	vdotn=v|n;
120 	retval=v;
121 	retval=v-n*vdotn-n*vdotn*res;
122 
123 	return retval;
124 }
125