/
LineInfo.cs
143 lines (120 loc) · 3.66 KB
/
LineInfo.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using static System.Math;
namespace MiniDraw
{
class LineInfo
{
Point startPoint = Point.Empty;
public Point StartPoint {
get { return this.startPoint; }
set
{
if (this.isTheSamePoints(endPoint, value))
this.ThrowEmptyOrSamePointsException();
this.startPoint = value;
this.Refresh();
}
}
Point endPoint = Point.Empty;
public Point EndPoint
{
get { return this.endPoint; }
set
{
if (this.isTheSamePoints(startPoint, value))
this.ThrowEmptyOrSamePointsException();
this.endPoint = value;
this.Refresh();
}
}
float criticalPerpendicularLenght;
public float CriticalPerpendicularLenght {
get
{
return this.criticalPerpendicularLenght;
}
set
{
this.criticalPerpendicularLenght = value;
}
}
public LineInfo(Point startPoint, Point endPoint)
{
if (isTheSamePoints(startPoint, endPoint))
this.ThrowEmptyOrSamePointsException();
this.startPoint = startPoint;
this.endPoint = endPoint;
this.Refresh();
}
public float Slope {
get
{
float deltaY = startPoint.Y - endPoint.Y;
float deltaX = startPoint.X - endPoint.X;
return (deltaY / deltaX);
}
}
public float YNod {
get
{
Point point = getNonEmptyPoint();
float y = point.Y;
float x = point.X;
float slope = this.Slope;
//Y = X(slope) + YNod
float d = slope * x;
float f = y - d;
return (y - slope * x);
}
}
public float PerpendicularLength(Point point)
{
float A = 1;
float B = -(this.Slope);
float C = -(this.YNod);
int x = point.X, y = point.Y;
//Look for equation of length of perpendicular from point to line
return (Abs(A * y + B * x + C) / (float)Sqrt((Pow(A, 2) + (Pow(B, 2)))));
}
public bool IsCriticalPerpendicularLength(Point point)
{
float l = PerpendicularLength(point);
if (l <= this.CriticalPerpendicularLenght)
{
int minY = Min(startPoint.Y, endPoint.Y);
int maxY = Max(startPoint.Y, endPoint.Y);
int currentY = point.Y;
if (currentY >= minY && currentY <= maxY)
return true;
}
return false;
}
public void Move(Size size)
{
this.startPoint = Point.Add(this.startPoint, size);
this.endPoint = Point.Add(this.endPoint, size);
}
public void Refresh()
{
}
bool isTheSamePoints(Point point1, Point point2)
{
return (point1 == point2);
}
void ThrowEmptyOrSamePointsException()
{
throw new ArgumentException("You send the same point, that's not valid");
}
Point getNonEmptyPoint()
{
if (this.startPoint == Point.Empty)
return this.endPoint;
else
return this.startPoint;
}
}
}