Skip to content

Instantly share code, notes, and snippets.

@seklyza
Created February 28, 2020 08:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seklyza/0478fcd85d5e39bbc12331cf094d4ce1 to your computer and use it in GitHub Desktop.
Save seklyza/0478fcd85d5e39bbc12331cf094d4ce1 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication3
{
class Cartesian
{
double x;
double y;
public Cartesian() { }
public Cartesian(double x, double y)
{
this.x = x;
this.y = y;
}
public double GetX()
{
return x;
}
public void SetX(double x)
{
this.x = x;
}
public double GetY()
{
return y;
}
public void SetY(double y)
{
this.y = y;
}
public Polar ToPolar()
{
var v = Math.Sqrt(Math.Pow(x, 2) + Math.Pow(y, 2));
var a = Math.Atan(y / x) * (180 / Math.PI);
return new Polar(v, a);
}
public static Cartesian Add(Cartesian A, Cartesian B)
{
return new Cartesian(A.GetX() + B.GetX(), B.GetX() + B.GetY());
}
public static Cartesian Subtract(Cartesian A, Cartesian B)
{
return new Cartesian(A.GetX() - B.GetX(), B.GetX() - B.GetY());
}
public static Cartesian Multiply(Cartesian A, Cartesian B)
{
var Pa = A.ToPolar();
var Pb = B.ToPolar();
return Polar.Multiply(Pa, Pb).ToCartesian();
}
public static Cartesian Divide(Cartesian A, Cartesian B)
{
var Pa = A.ToPolar();
var Pb = B.ToPolar();
return Polar.Divide(Pa, Pb).ToCartesian();
}
}
class Polar
{
double v;
double a;
public Polar() { }
public Polar(double v, double a)
{
this.v = v;
this.a = a;
}
public double GetV()
{
return v;
}
public void SetV(double v)
{
this.v = v;
}
public double GetA()
{
return a;
}
public void SetA(double a)
{
this.a = a;
}
public Cartesian ToCartesian()
{
var x = v * Math.Cos(a * (Math.PI / 180));
var y = v * Math.Sin(a * (Math.PI / 180));
return new Cartesian(x, y);
}
public static Polar Add(Polar A, Polar B)
{
var Ca = A.ToCartesian();
var Cb = B.ToCartesian();
return Cartesian.Add(Ca, Cb).ToPolar();
}
public static Polar Subtract(Polar A, Polar B)
{
var Ca = A.ToCartesian();
var Cb = B.ToCartesian();
return Cartesian.Subtract(Ca, Cb).ToPolar();
}
public static Polar Multiply(Polar A, Polar B) {
return new Polar(A.GetV() * B.GetV(), A.GetA() + B.GetA());
}
public static Polar Divide(Polar A, Polar B)
{
return new Polar(A.GetV() / B.GetV(), A.GetA() - B.GetA());
}
}
class Program
{
static void Main(string[] args)
{
var Vi = new Polar(10, 30); // 10|_30
double W = 500; // [rad/sec]
double R = 100; // [Ohm]
double L = 0.1; // [Hy]
Cartesian Xl = new Cartesian(0, W * L);
Cartesian Z = Cartesian.Add(new Cartesian(R, 0), Xl);
Cartesian I = Cartesian.Divide(Vi.ToCartesian(), Z);
Cartesian Vr = Cartesian.Multiply(I, new Cartesian(R, 0));
Cartesian Vl = Cartesian.Multiply(I, Xl);
while (true)
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("1) Z\n2) I\n3) Vr\n4) Vl");
Console.ForegroundColor = ConsoleColor.Green;
Console.Write("Make a decision: ");
int i;
if (!int.TryParse(Console.ReadLine(), out i)) continue;
switch (i)
{
case 1:
Print("Z", Z);
break;
case 2:
Print("I", I);
break;
case 3:
Print("Vr", Vr);
break;
case 4:
Print("Vl", Vl);
break;
}
}
}
private static void Print(string A, Cartesian B)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("{0} = {1}+{2}j OR {0} = {3}({4})\n", A, B.GetX(), B.GetY(), B.ToPolar().GetV(), B.ToPolar().GetA());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment