【问题标题】:Convert cartesian points to polar将笛卡尔点转换为极坐标
【发布时间】:2018-08-03 08:43:40
【问题描述】:

我想从我的计算机导入的图像(圆)上从笛卡尔表示转到极坐标点,但我不知道该怎么做。

这是我已经尝试过的代码:

private double rho (double t)
{
    return (po-(vrot*tp*t));
}

private double tetha (double t)
{
    return vrot*2*Math.PI*t;
}

private double x (double t)
{
    return rho(t) * Math.Sin(tetha(t));
}

private double y (double t)
{
    return rho(t) * Math.Cos(tetha(t));
}

public void DrawLinePoint(Bitmap bmp)
{

    // Create pen.
    Pen blackPen = new Pen(Color.Red, 3);

    // Create points that define line.
    PointF[] points =
        {
            new PointF((float)x(1.81818182),(float)y(1.81818182)),
            new PointF((float)x(3.63636364), (float)y(3.63636364)),
            
        };

    // Draw line to screen.
    using(var graphics = Graphics.FromImage(bmp))
    {
        graphics.DrawLines(blackPen, points);
    }
}

【问题讨论】:

  • 您了解转换本身吗?你只是向我们展示了一些方法,而没有解释什么是行不通的。
  • 我已经搜索并编辑了答案。我可以代表双点吗?谢谢

标签: c# graphics polar-coordinates cartesian-coordinates pen


【解决方案1】:

第一个:Polar and Cartesian Coordinates and how to convert between them

  • 笛卡尔:x, y
  • 极地:Radius, Angle (Could be 0-360° or 0 - 2*PI)

公式:

  • 半径 = √X²+Y²
  • 角度 = Tan-1 (Y/X)
  • 角度 = (Tan-1 (Y/X) * 360) / (2*PI)

..因为 Tan、Sin、Cos 不适用于 0-360°。

  • X = 半径 * Cos(角度)
  • Y = 半径 * Sin(角度)

现在您可以使用它了。

下面是一些示例代码:

public class CartesianCoord
{
    public double X { get; set; }
    public double Y { get; set; }

    public PolarCoord ToPolarCoord()
    {
        PolarCoord p = new PolarCoord();
        p.Radius = Math.Sqrt(X * X + Y * Y);
        p.Angle = Math.Atan2(Y / X);
        return p;
    }
}

public class PolarCoord
{
    public double Radius { get; set; }
    public double Angle { get; set; }
    public double AngleDegree { get { return (this.Angle * 360) / (Math.PI * 2); } set { this.Angle = (value * 2 * Math.PI) / 360; } }

    public CartesianCoord ToCartesianCoors()
    {
        CartesianCoord c = new CartesianCoord();

        c.X = this.Radius * Math.Cos(Angle);
        c.Y = this.Radius * Math.Sin(Angle);

        return c;
    }
}

static void Main()
{
    CartesianCoord c = new CartesianCoord() { X = 12, Y = 5 };
    Console.WriteLine("Cartesian Coords - X: {0}, Y: {1}", c.X, c.Y);
    PolarCoord p = c.ToPolarCoord();
    Console.WriteLine("Polar Coords - Arc: {0} ({1}°), Radius: {2} ", p.Angle, p.AngleDegree, p.Radius);
    CartesianCoord c2 = p.ToCartesianCoors();
    Console.WriteLine("Cartesian Coords - X: {0}, Y: {1}", c2.X, c2.Y);

}

【讨论】:

  • 最好改用Atan2(Y, X)
  • 呃。你很有钱。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多