-
Notifications
You must be signed in to change notification settings - Fork 0
/
vectorangles.cpp
152 lines (115 loc) · 2.77 KB
/
vectorangles.cpp
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include "TXLib.h"
#include <math.h>
class vector
{
private:
double x, y;
//double r = sqrt(x*x + y*y);
public:
double getx()
{
return x;
}
double gety()
{
return y;
}
/*double getr()
{
return r;
} */
void setx(double temp)
{
x = temp;
}
void sety(double temp)
{
y = temp;
}
vector normalize();
vector operator+(vector a);
vector operator*(double multiplicator);
vector operator-();
void drawVector(struct CoordSys coord);
void drawLine(double x0, double y0)
{
txLine(x0, y0, x, y);
}
vector() : x(0), y(0) {}
vector(double xx, double yy) : x(xx), y(yy) {}
};
struct CoordSys
{
int x0, y0;
int x1, y1;
void createSys()
{
txLine(x0, y0, x0, y1);
txLine(x0, y1, x1, y1);
}
CoordSys() : x0(0), y0(0), x1(0), y1(0) { }
CoordSys(int xi0, int yi0, int xi1, int yi1) : x0(xi0), y0(yi0), x1(xi1), y1(yi1) { }
};
vector vector::operator+(vector a)
{
return vector(x + a.x, y + a.y);
}
vector vector::operator*(double multiplicator)
{
return vector(x * multiplicator, y * multiplicator);
}
vector vector::operator-()
{
return vector((-1)*x, (-1)*y);
}
vector vector::normalize()
{
double r = sqrt(x*x + y*y);
x = x/r;
y = y/r;
return vector(x, y);
}
void vector::drawVector(struct CoordSys coord)
{
int scale = 8;
double m = x - coord.x0;
if (m != 0)
{
m = (y - coord.y1) / (x - coord.x0);
}
double degree = atan(m);
bool change = x < coord.x0;
double add = 0;
if (change == true)
{
add = txPI;
}
double degree1 = degree + 5 * txPI / 6 + add;
double degree2 = degree + 7 * txPI / 6 + add;
double ptx1 = x + cos(degree1) * scale;
double pty1 = y + sin(degree1) * scale;
double ptx2 = x + cos(degree2) * scale;
double pty2 = y + sin(degree2) * scale;
txSetColor(TX_WHITE);
txLine( coord.x0, coord.y1, x, y );
txLine( x, y, ptx1, pty1);
txLine( x, y, ptx2, pty2);
}
int main()
{
txCreateWindow(800, 600);
txSetColor(TX_LIGHTGREEN);
CoordSys Sys(50, 50, 750, 550);
Sys.createSys();
vector v;
while(txMouseButtons() != 3)
{
if (txMouseButtons() & 1)
{
v.setx(txMouseX());
v.sety(txMouseY());
v.drawVector(Sys);
}
}
return 0;
}