-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcpoint.cpp
53 lines (41 loc) · 1.07 KB
/
cpoint.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
#include <iostream>
#include <cmath>
#include "cpoint.h"
cPoint::cPoint () {}
bool cPoint::operator== (cPoint a)
{
return (x == a.x && y == a.y);
}
std::ostream& operator<< (std::ostream &out, cPoint a)
{
out << "(" << a.x << ", " << a.y << ")";
return out;
}
cPoint::cPoint (double _x, double _y)
{
x = _x;
y = _y;
}
int cPoint::point_position (cPoint a, cPoint b)
{
double t = (b.x - a.x) * (y - a.y) - (b.y - a.y) * (x - a.x);
return (fabs(t) < 1e-6) ? (0) : ((t > 0) ? (1) : (-1)); // -1 - справа, 0 - на его прямой и 1 - слева
}
double cPoint::length_to (cPoint a)
{
return sqrt(pow(a.x - x, 2) + pow(a.y - y, 2));
}
double cPoint::length_to (cPoint a, cPoint b)
{
double t;
if (((x - a.x) * (b.x - a.x) + (y - a.y) * (b.y - a.y)) * ((x - b.x) * (b.x - a.x) + (y - b.y) * (b.y - a.y)) > 0)
{
double w = pow(x - b.x, 2) + pow(y - b.y, 2);
t = pow(x - a.x, 2) + pow(y - a.y, 2);
if (w < t)
t = w;
}
else
t = pow((x - a.x) * (b.y - a.y) - (y - a.y) * (b.x - a.x), 2) / (pow(b.x - a.x, 2) + pow(b.y - a.y, 2));
return sqrt(t);
}