-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEllipseExtension.cs
54 lines (51 loc) · 2.52 KB
/
EllipseExtension.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
using System;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
namespace Gile.AutoCAD.R19.Geometry
{
/// <summary>
/// Provides extension methods for the Ellipse type.
/// </summary>
public static class EllipseExtension
{
/// <summary>
/// Generates a polyline to approximate an ellipse.
/// </summary>
/// <param name="ellipse">The instance to which this method applies.</param>
/// <returns>A new Polyline instance.</returns>
/// <exception cref="ArgumentNullException">ArgumentNullException is thrown if <paramref name="ellipse"/> is null.</exception>
public static Polyline ToPolyline(this Ellipse ellipse)
{
Assert.IsNotNull(ellipse, nameof(ellipse));
Polyline pline = new PolylineSegmentCollection(ellipse).ToPolyline();
pline.Closed = ellipse.Closed;
pline.Normal = ellipse.Normal;
pline.Elevation = ellipse.Center.TransformBy(Matrix3d.WorldToPlane(new Plane(Point3d.Origin, ellipse.Normal))).Z;
return pline;
}
/// <summary>
/// Gets the ellipse parameter corresponding to the specified angle.
/// </summary>
/// <param name="ellipse">The instance to which this method applies.</param>
/// <param name="angle">Angle.</param>
/// <returns>The parameter corresponding to the angle.</returns>
/// <exception cref="ArgumentNullException">ArgumentNullException is thrown if <paramref name="ellipse"/> is null.</exception>
public static double GetParamAtAngle(this Ellipse ellipse, double angle)
{
Assert.IsNotNull(ellipse, nameof(ellipse));
return Math.Atan2(ellipse.MajorRadius * Math.Sin(angle), ellipse.MinorRadius * Math.Cos(angle));
}
/// <summary>
/// Gets the ellipse angle corresponding to the specified parameter.
/// </summary>
/// <param name="ellipse">The instance to which this method applies.</param>
/// <param name="param">Parameter.</param>
/// <returns>The angle corresponding to the parameter.</returns>
/// <exception cref="ArgumentNullException">ArgumentNullException is thrown if <paramref name="ellipse"/> is null.</exception>
public static double GetAngleAtParam(this Ellipse ellipse, double param)
{
Assert.IsNotNull(ellipse, nameof(ellipse));
return Math.Atan2(ellipse.MinorRadius * Math.Sin(param), ellipse.MajorRadius * Math.Cos(param));
}
}
}