/// <summary> /// Converts a point from Cartesian coordinate system to Polar coordinate system /// </summary> /// <param name="x">Cartesian X coordinate</param> /// <param name="y">Cartesian Y coordinate</param> /// <param name="xOffset">x offset</param> /// <param name="yOffset">y offset</param> /// <returns>double[2] array where first is radius and second angle in degree</returns> public static double[] CartesianToPolar(double x, double y, double xOffset = 0, double yOffset = 0) { x = x - xOffset; y = y - yOffset; double r = Math.Sqrt(x * x + y * y); if (x == 0) { return(new double[] { r, (y >= 0 ? 0 : 180) }); } double deg = MathExtension.RadianToDegree(Math.Atan(y / x)); if (x < 0 && y > 0) { deg += 180; } else if (x < 0 && y < 0) { deg += 180; } else if (x > 0 && y < 0) { deg += 360; } return(new double[] { r, deg }); }
private void Generate() { code = new GCodeCollector(); double dist = 0; if (isClockwise) { if (stopDeg < startDeg) { dist = startDeg - stopDeg; } else { dist = 360 - (stopDeg - startDeg); } } else { if (stopDeg < startDeg) { dist = 360 - startDeg + stopDeg; } else { dist = stopDeg - startDeg; } } if (dist == 0) { dist = 360; } var startRad = MathExtension.DegreeToRadian(startDeg); var stopRad = MathExtension.DegreeToRadian(stopDeg); double sign = isClockwise ? -1 : 1; Point p = new Point(center.X + AxisA / 2d * Math.Cos(startRad), center.Y + AxisB / 2d * Math.Sin(startRad)); Code.addCode(new GCodeLine(x: p.X, y: p.Y, speed: Speed, extrude: null)); double a = AxisA / 2; double b = AxisB / 2; double c = Math.PI * (3 * (a + b) - Math.Sqrt((3 * a + b) * (a + 3 * b))); double degStep = Math.Min(360d / 8d, dist / (c * dist / 360 / MinArc)); for (double i = degStep; i < dist; i += degStep) { p = new Point(center.X + AxisA / 2d * Math.Cos(MathExtension.DegreeToRadian(startDeg + i * sign)), center.Y + AxisB / 2d * Math.Sin(MathExtension.DegreeToRadian(startDeg + i * sign))); Code.addCode(new GCodeLine(x: p.X, y: p.Y, speed: Speed, extrude: needExtrude ? GCodeSpecial.ExtrudeLine(p, Code.LastPoint) : (double?)null)); } p = new Point(center.X + AxisA / 2d * Math.Cos(stopRad), center.Y + AxisB / 2d * Math.Sin(stopRad)); Code.addCode(new GCodeLine(x: p.X, y: p.Y, speed: Speed, extrude: needExtrude ? GCodeSpecial.ExtrudeLine(p, Code.LastPoint) : (double?)null)); }
/// <summary> /// Converts a point from Polar coordinate system to Cartesian coordinate system /// </summary> /// <param name="r">distance</param> /// <param name="deg">angle in degree</param> /// <param name="xOffset">x offset</param> /// <param name="yOffset">y offest</param> /// <returns></returns> public static Point PolarToCartesian(double r, double deg, double xOffset = 0, double yOffset = 0) { double rad = MathExtension.DegreeToRadian(deg); double yk = r * Math.Sin(rad); double xk = r * Math.Cos(rad); return(new Point(xk + xOffset, yk + yOffset)); }
public GCodeEllipse(Point center, double axisA, double axisB, double startDeg, double stopDeg, bool isClockwise = true, double Angle = 0, double?Speed = null, bool needExtrude = false) { this.center = new Point(center.X, center.Y); AxisA = axisA; AxisB = axisB; Angle = MathExtension.DegreeToRadian(Angle); this.isClockwise = isClockwise; this.startDeg = startDeg; this.stopDeg = stopDeg; this.Speed = Speed; this.needExtrude = needExtrude; Generate(); }