-
Notifications
You must be signed in to change notification settings - Fork 0
/
linear-main.c
115 lines (103 loc) · 2.61 KB
/
linear-main.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <stdio.h>
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
class fPoint {
public:
float x=0, y=0;
fPoint() { x=0; y=0; }
fPoint(float sx, float sy) { x=sx; y=sy; }
void set(float sx, float sy) { x=sx; y=sy; }
void set(fPoint p) { x=p.x; y=p.y; }
};
float sign(fPoint p1, fPoint p2, fPoint p3) {
return (p1.x - p3.x) * (p2.y - p3.y) - (p2.x - p3.x) * (p1.y - p3.y);
}
bool PointInTriangle(fPoint pt, fPoint v1, fPoint v2, fPoint v3) {
float d1, d2, d3;
bool has_neg, has_pos;
d1 = sign(pt, v1, v2);
d2 = sign(pt, v2, v3);
d3 = sign(pt, v3, v1);
has_neg = (d1 < 0) || (d2 < 0) || (d3 < 0);
has_pos = (d1 > 0) || (d2 > 0) || (d3 > 0);
return !(has_neg && has_pos);
}
/*
-3 -1
1 0
2 0
5 1
6 0
*/
template <typename T>
class Point {
public:
T x=0, y=0;
Point() { x=0; y=0; }
Point(T sx, T sy) { x=sx; y=sy; }
void set(T sx, T sy) { x=sx; y=sy; }
void set(Point p) { x=p.x; y=p.y; }
};
enum { T_LEFT, T_RIGHT, T_TOP, T_BOTTOM, T_CENTER };
#define POINTS 5
template <typename T>
class LinearTilterpolator {
public:
Point<T> left = Point<T>(0,0);
Point<T> right = Point<T>(0,0);
Point<T> up = Point<T>(0,0);
Point<T> down = Point<T>(0,0);
Point<T> center = Point<T>(0,0);
Point<T> *sortx = new Point<T>[POINTS];
Point<T> *sorty = new Point<T>[POINTS];
static bool pt_lesserx(Point<T> p1, Point<T> p2) {
return p1.x < p2.x;
}
static bool pt_lessery(Point<T> p1, Point<T> p2) {
return p1.y < p2.y;
}
Tilterpolator(void) {
for (int i=0; i<POINTS; i++) {
sortx[i] = Point<T>(0,0);
sorty[i] = Point<T>(0,0);
}
}
void print(void) {
printf("X values...\n");
for (int i=0; i<POINTS; i++) {
printf(" [%d] %d, %d\n", i, sortx[i].x, sortx[i].y);
}
printf("Y values...\n");
for (int i=0; i<POINTS; i++) {
printf(" [%d] %d, %d\n", i, sorty[i].x, sorty[i].y);
}
}
void prep(void) {
sortx[T_LEFT].set(left);
sortx[T_LEFT] = sorty[T_LEFT] = left;
sortx[T_RIGHT] = sorty[T_RIGHT] = right;
sortx[T_TOP] = sorty[T_TOP] = up;
sortx[T_BOTTOM] = sorty[T_BOTTOM] = down;
sortx[T_CENTER] = sorty[T_CENTER] = center;
std::sort(sortx, sortx+POINTS, pt_lesserx);
std::sort(sorty, sorty+POINTS, pt_lessery);
}
};
int main(int argc, char *argv[]) {
Tilterpolator<int16_t> mouse;
mouse.left.set(-3,-1);
mouse.right.set(1,0);
mouse.up.set(-2,3);
mouse.center.set(5,1);
mouse.down.set(6,0);
mouse.prep();
mouse.print();
fPoint a,b,c,p;
a = fPoint(mouse.left.x, mouse.left.y);
b = fPoint(mouse.up.x, mouse.up.y);
c = fPoint(mouse.center.x, mouse.center.y);
p = fPoint(2,0);
printf("In tri: %d\n", PointInTriangle(p, a, b, c));
return 0;
}