-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMappedLine.cpp
121 lines (103 loc) · 3.38 KB
/
MappedLine.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
#include "StdAfx.h"
#include "MappedLine.h"
MappedLine::MappedLine() {
StartPoint.x = StartPoint.y = 0;
EndPoint.x = EndPoint.y = 0;
}
MappedLine::MappedLine(const MappedLine& CopyLine) {
*this = CopyLine;
}
MappedLine::MappedLine(const POINT SP, const POINT EP) {
StartPoint = SP;
EndPoint = EP;
}
MappedLine::MappedLine(const LONG Sx, const LONG Sy, const LONG Ex, const LONG Ey) {
StartPoint.x = Sx;
StartPoint.y = Sy;
EndPoint.x = Ex;
EndPoint.y = Ey;
}
MappedLine::~MappedLine() {
}
const bool MappedLine::IsVertical() const {
return StartPoint.x == EndPoint.x;
}
const bool MappedLine::IsHorizontal() const {
return StartPoint.y == EndPoint.y;
}
const POINT MappedLine::Midpoint() const {
POINT ReturnPoint;
ReturnPoint.x = (StartPoint.x + EndPoint.x) / 2;
ReturnPoint.y = (StartPoint.y + EndPoint.y) / 2;
return ReturnPoint;
}
//returns true if it is vertical
const double MappedLine::Slope() const {
if (IsVertical()) return 0; //don't even waste time calculating
return (double)(EndPoint.y - StartPoint.y) / (double)(EndPoint.x - StartPoint.x);
}
const double MappedLine::PerpDist(const POINT Perp) const {
if (IsVertical()) { //the line is vertical
return (double)abs(Perp.x - StartPoint.x);
}
else if (IsHorizontal()) { //the line is horizontal
return (double)abs(Perp.y - StartPoint.y);
}
else { //the line is normal
double S = Slope();
double NormalB = EndPoint.y - (S * EndPoint.x);
double PerpendicularSlope = ((double)-1) / S;
double PerpendicularB = (double)Perp.y - (double)((double)PerpendicularSlope * (double)Perp.x);
POINT IntersectPoint;
IntersectPoint.x = (LONG)((double)(PerpendicularB - NormalB) / (double)(S - PerpendicularSlope));
IntersectPoint.y = (LONG)(S * IntersectPoint.x + NormalB);
return GeometricObject::Range(IntersectPoint, Perp);
}
}
const MappedLine& MappedLine::operator=(const MappedLine& CopyData) {
if (*this != CopyData) {
StartPoint = CopyData.StartPoint;
EndPoint = CopyData.EndPoint;
}
return *this;
}
const bool MappedLine::operator==(const MappedLine& CompData) const {
return (StartPoint == CompData.StartPoint) &&
(EndPoint == CompData.EndPoint);
}
const bool MappedLine::operator!=(const MappedLine& CompData) const {
return !(*this == CompData);
}
//greater than or less than only applies when the lines are horizontal or vertical
const bool MappedLine::operator<(const MappedLine& CompData) const {
if (IsVertical() && CompData.IsVertical()) { //x coordinates are equal
return StartPoint.y < CompData.StartPoint.y;
}
else if (IsHorizontal() && CompData.IsHorizontal()) { //y coordinates are equal
return StartPoint.x < CompData.StartPoint.x;
}
return false;
}
const bool MappedLine::operator<=(const MappedLine& CompData) const {
return (*this < CompData) || (*this == CompData);
}
const bool MappedLine::operator>(const MappedLine& CompData) const {
if (IsVertical() && CompData.IsVertical()) { //x coordinates are equal
return StartPoint.y > CompData.StartPoint.y;
}
else if (IsHorizontal() && CompData.IsHorizontal()) { //y coordinates are equal
return StartPoint.x > CompData.StartPoint.x;
}
return false;
}
const bool MappedLine::operator>=(const MappedLine& CompData) const {
return (*this > CompData) || (*this == CompData);
}
void MappedLine::ReadData(FileStream& File) {
File >> StartPoint;
File >> EndPoint;
}
void MappedLine::WriteData(FileStream& File) const {
File << StartPoint;
File << EndPoint;
}