-3

I want to go from the Cartesian representation to Polar points on an image (a circle) that I imported from my computer but I do not know how to do it.

Here is the code I have already tried:

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);
    }
}
BSMP
  • 4,596
  • 8
  • 33
  • 44
gggkg
  • 3
  • 6
  • 1
    Did you understand the conversion itself? You did only show us some methods without any explaination about what doesn't work. – kara Aug 03 '18 at 08:58
  • I have searched and edited to put answer. I it's possible to represent double point? thanks – gggkg Aug 03 '18 at 09:58

1 Answers1

0

1st: Polar and Cartesian Coordinates and how to convert between them

  • Cartesian: x, y
  • Polar: Radius, Angle (Could be 0-360° or 0 - 2*PI)

The Formulas:

  • Radius = √X²+Y²
  • Angle = Tan-1 (Y/X)
  • Angle in Degree = (Tan-1 (Y/X) * 360) / (2*PI)

..because Tan, Sin, Cos don't work with 0-360°.

  • X = Radius * Cos(Angle)
  • Y = Radius * Sin(Angle)

Now you can work with it.

Here is some example code:

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);

}
BSMP
  • 4,596
  • 8
  • 33
  • 44
kara
  • 3,205
  • 4
  • 20
  • 34