Debian Codegraph @

external/cubicvr2/math/vec2.h

raw
1 //
2 // vec2.h
3 // CubicVR2
4 //
5 // Created by Charles J. Cliffe on 2013-02-22.
6 // Copyright (c) 2013 Charles J. Cliffe. All rights reserved.
7 //
8
9 #ifndef __CubicVR2__vec2__
10 #define __CubicVR2__vec2__
11
12 #include <iostream>
13 #include <cmath>
14 #include "cubic_types.h"
15 #include <stddef.h>
16
17 namespace CubicVR {
18 #define vec2SG(c,x,y) \
19 vec2 COMBINE(get,x)() { return y; } \
20 c & COMBINE(set,x)(vec2 value) { y = value; return *this; }
21
22 struct vec2 {
23 __float x, y;
24
25 public:
26 __float& u() { return x; }
27 __float& v() { return y; }
28
29 //access as-array:
30 inline __float& operator [] (size_t i) {
31 __float* as_array = (__float*)this;
32 return (as_array[i]);
33 }
34
35 inline const __float& operator [] (size_t i) const {
36 __float* as_array = (__float*)this;
37 return (as_array[i]);
38 }
39
40 vec2 (__float xi,__float yi) { x = xi; y = yi; }
41 vec2 () { x = y = 0.0f; }
42
43 vec2 operator*(__float v) { return vec2( x*v, y*v ); }
44
45 // vec2 operator*(vec2 v) { return vec2::cross(*this,v); }
46 vec2 operator+(vec2 v) { return vec2::add(*this,v); }
47 vec2 operator-(vec2 v) { return vec2::subtract(*this,v); }
48
49
50 static bool equal(vec2 a, vec2 b, __float epsilon = 0.00000001) {
51 return (fabs(a[0] - b[0]) < epsilon && fabs(a[1] - b[1]) < epsilon);
52 };
53
54 static bool onLine(vec2 a, vec2 b,vec2 c) {
55 __float minx = (a[0]<b[0])?a[0]:b[0];
56 __float miny = (a[1]<b[1])?a[1]:b[1];
57 __float maxx = (a[0]>b[0])?a[0]:b[0];
58 __float maxy = (a[1]>b[1])?a[1]:b[1];
59
60 if ((minx <= c[0] && c[0] <= maxx) && (miny <= c[1] && c[1] <= maxy)) {
61 return true;
62 } else {
63 return false;
64 }
65 };
66
67 static vec2 lineIntersect(vec2 a1, vec2 a2, vec2 b1, vec2 b2) {
68 __float x1 = a1[0], y1 = a1[1], x2 = a2[0], y2 = a2[1];
69 __float x3 = b1[0], y3 = b1[1], x4 = b2[0], y4 = b2[1];
70
71 __float d = ((x1-x2) * (y3-y4)) - ((y1-y2) * (x3-x4));
72 if (d == 0) return vec2(INFINITY,INFINITY);
73
74 __float xi = (((x3-x4) * ((x1*y2)-(y1*x2))) - ((x1-x2) *((x3*y4)-(y3*x4))))/d;
75 __float yi = (((y3-y4) * ((x1*y2)-(y1*x2))) - ((y1-y2) *((x3*y4)-(y3*x4))))/d;
76
77 return vec2( xi,yi );
78 };
79
80 static vec2 add(vec2 a,vec2 b) {
81 return vec2(a[0]+b[0],a[1]+b[1]);
82 };
83
84 static vec2 subtract(vec2 a, vec2 b) {
85 return vec2(a[0]-b[0],a[1]-b[1]);
86 };
87
88 static __float length(vec2 a,vec2 b) {
89 vec2 s(a[0]-b[0],a[1]-b[1]);
90
91 return sqrtf(s[0]*s[0]+s[1]*s[1]);
92 };
93
94 static __float length(vec2 a) {
95 return sqrtf(a[0]*a[0]+a[1]*a[1]);
96 };
97
98 };
99
100 }
101 #endif /* defined(__CubicVR2__vec2__) */