-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLine.cs
More file actions
107 lines (89 loc) · 3.25 KB
/
Line.cs
File metadata and controls
107 lines (89 loc) · 3.25 KB
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
using System;
using System.Drawing;
namespace ShapesGraphics
{
[Serializable]
public class Line : Shape
{
// ----
// Data
// ----
private Point endPoint;
// -------
// Methods
// -------
public Line(Point startPoint, Color color, Point endPoint, Pen pen, Brush brush) : base(startPoint, color, true, pen, brush)
{
this.endPoint = endPoint;
area = CalcArea();
}
public override void Draw(Graphics g)
{
if (Show)
g.DrawLine(Pen, base.Position.X, base.Position.Y, endPoint.X, endPoint.Y);
}
public override void MoveLocation(Point newStartPosition)
{
// Calc new End Point
int newEndX = endPoint.X + (newStartPosition.X - Position.X);
int newEndY = endPoint.Y + (newStartPosition.Y - Position.Y);
endPoint = new Point(newEndX, newEndY);
// Set the new Start Point
base.MoveLocation(newStartPosition);
}
public override void Resize(int percent)
{
// Resize line length... Change EndPoint values...
int distanceX = endPoint.X - Position.X;
int distanceY = endPoint.Y - Position.Y;
Point p = new Point(( Convert.ToInt32((float)distanceX / 100 * percent) + endPoint.X),
( Convert.ToInt32((float)distanceY / 100 * percent) + endPoint.Y));
endPoint = p; // copy by value, since it is struct..
//EndPoint.X += (EndPoint.X / 100 * percent);
//EndPoint.Y += (EndPoint.Y / 100 * percent);
CalcArea();
}
public override double CalcArea()
{
// Consider area as: line length
area = CalcPerimeter();
return area;
}
public override double CalcPerimeter()
{
// NOTE:
// Perimeter of a line is actually its length....
// As a line is actually a rectangle with height == 0 ....
return base.Position.Distance(endPoint);
}
public override bool Contains(Point p)
{
// Find the distance of point P from both line end points A, B.
// If AB = AP + PB, then P lies on the line segment AB.
float a = Position.Distance(p);
float b = endPoint.Distance(p);
if ((int)(a + b) == (int)(area))
return true;
else
return false;
}
public override string ToString()
{
return "Line :: " + base.ToString() + String.Format(", End Point: {0}", endPoint);
}
public override bool Equals(object obj)
{
if (obj == null)
return false;
if (obj.GetType().Name != "Line")
return false;
Line l = (Line)obj;
return (Point.Equals(this.endPoint, l.endPoint)) &&
base.Equals(l);
}
public override int GetHashCode()
{
return this.ToString().GetHashCode();
}
}
}