-
Notifications
You must be signed in to change notification settings - Fork 12
/
Box.cs
159 lines (134 loc) · 3.93 KB
/
Box.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
namespace PhysicalDrag
{
public class Box
{
/// <summary>
/// corner points
/// </summary>
private PointF[] m_C = new PointF[4];
private GraphicsPath m_outline;
#region constructors
public Box() { }
public Box(Point nA, Point nB, Point nC, Point nD)
{
Set(nA, nB, nC, nD);
}
public Box(PointF nA, PointF nB, PointF nC, PointF nD)
{
Set(nA, nB, nC, nD);
}
#endregion
#region shape properties
public PointF A { get { return m_C[0]; } }
public PointF B { get { return m_C[1]; } }
public PointF C { get { return m_C[2]; } }
public PointF D { get { return m_C[3]; } }
/// <summary>
/// Center of mass
/// </summary>
public PointF Center
{
get
{
return new PointF(0.5f * (A.X + C.X),
0.5f * (A.Y + C.Y));
}
}
/// <summary>
/// Test for inclusion of a point.
/// </summary>
/// <param name="pt">Point to test</param>
public bool Contains(Point pt)
{
return m_outline.IsVisible(pt);
}
/// <summary>
/// Override all corners.
/// </summary>
public void Set(PointF nA, PointF nB, PointF nC, PointF nD)
{
m_C[0] = nA;
m_C[1] = nB;
m_C[2] = nC;
m_C[3] = nD;
RecomputeOutline();
}
private void RecomputeOutline()
{
m_outline = new GraphicsPath();
m_outline.AddLine(A, B);
m_outline.AddLine(B, C);
m_outline.AddLine(C, D);
m_outline.CloseFigure();
}
#endregion
#region physical simulations
/// <summary>
/// Perform a single drag operation.
/// </summary>
/// <param name="from">From point.</param>
/// <param name="to">To point</param>
public void Drag(Point from, Point to)
{
PointF M = Center;
double angle_before = Math.Atan2(from.Y - M.Y, from.X - M.X);
double angle_after = Math.Atan2(to.Y - M.Y, to.X - M.X);
double angle_diff = 180.0 * ((angle_after - angle_before) / Math.PI);
if (angle_diff > 180.0) { angle_diff -= 360.0; }
else if (angle_diff < -180.0) { angle_diff += 360.0; }
float dx = to.X - from.X;
float dy = to.Y - from.Y;
float dd = Distance(M, from);
if (dd < 10.0F)
{
angle_diff = 0.0f;
}
else
{
float radius = 0.5f * Distance(A, C);
dd = (dd*dd) / (radius * radius);
angle_diff *= dd;
}
//apply translation
Matrix T0 = new Matrix();
T0.Translate(dx, dy);
T0.TransformPoints(m_C);
//apply rotation
Matrix T1 = new Matrix();
T1.RotateAt((float)(angle_diff), to);
T1.TransformPoints(m_C);
RecomputeOutline();
}
/// <summary>
/// Distance squared between two points.
/// </summary>
private float Distance(PointF A, PointF B)
{
return (float)Math.Sqrt((A.X - B.X) * (A.X - B.X) + (A.Y - B.Y) * (A.Y - B.Y));
}
#endregion
/// <summary>
/// Render the object in a Graphics context.
/// </summary>
/// <param name="G">Graphics object to draw with.</param>
/// <param name="bg">Background color.</param>
/// <param name="fg">Outline color.</param>
public void Render(Graphics G, Color bg, Color fg)
{
if (m_outline == null) { return; }
Pen pen = new Pen(fg, 2.0f);
Brush brush = new SolidBrush(bg);
G.FillPath(brush, m_outline);
G.DrawPath(pen, m_outline);
PointF c = Center;
G.DrawString("Box",new Font(FontFamily.GenericSerif,10f,GraphicsUnit.Pixel),new SolidBrush(Color.Blue),c);
G.DrawLine(pen, c.X - 3, c.Y - 3, c.X + 3, c.Y + 3);
G.DrawLine(pen, c.X - 3, c.Y + 3, c.X + 3, c.Y - 3);
pen.Dispose();
brush.Dispose();
}
}
}