Skip to content

Instantly share code, notes, and snippets.

@intzaaa
Last active February 24, 2024 09:56
Show Gist options
  • Save intzaaa/4678bad1e29a63265316c194b5ae294a to your computer and use it in GitHub Desktop.
Save intzaaa/4678bad1e29a63265316c194b5ae294a to your computer and use it in GitHub Desktop.
enum Quadrant
{
None,
I,
II,
III,
IV
}
class Point(double x, double y)
{
public double X { get; set; } = x;
public double Y { get; set; } = y;
public static Point Zero { get => new(0, 0); }
public Quadrant Quadrant
{
get
{
if (X > 0)
{
if (Y > 0) return Quadrant.I;
else if (Y < 0) return Quadrant.IV;
else return Quadrant.None;
}
else if (X < 0)
{
if (Y > 0) return Quadrant.II;
else if (Y < 0) return Quadrant.III;
else return Quadrant.None;
}
else return Quadrant.None;
}
}
public static double Distance(Point a, Point b) => Math.Sqrt(Math.Pow((a.X) - (b.X), 2) + Math.Pow((a.Y) - (b.Y), 2));
public override string ToString() => $"P({X},{Y})";
}
enum Octant
{
None,
I,
II,
III,
IV,
V,
VI,
VII,
VIII
}
class Point3D(double x, double y, double z) : Point(x, y)
{
public double Z { get; set; } = z;
public new static Point3D Zero { get => new(0, 0, 0); }
public Octant Octant
{
get
{
if (X == 0 || Y == 0 || Z == 0)
return Octant.None;
else if (X > 0)
{
if (Y > 0)
{
return Z > 0 ? Octant.I : Octant.V;
}
else
{
return Z > 0 ? Octant.IV : Octant.VIII;
}
}
else
{
if (Y > 0)
{
return Z > 0 ? Octant.II : Octant.VI;
}
else
{
return Z > 0 ? Octant.III : Octant.VII;
}
}
}
}
public static double Distance(Point3D a, Point3D b) => Math.Sqrt(Math.Pow((a.X - b.X), 2) + Math.Pow((a.Y - b.Y), 2) + Math.Pow((a.Z - b.Z), 2));
public override string ToString() => $"P({X},{Y},{Z})";
}
class Line
{
public Point A { get; set; }
public Point B { get; set; }
public Line(Point first, Point second)
{
if (first.X < second.X)
{
A = first;
B = second;
}
else if (first.X > second.X)
{
B = first;
A = second;
}
else
{
if (first.Y < second.Y)
{
A = first;
B = second;
}
else if (first.Y > second.Y)
{
B = first;
A = second;
}
else
{
A = first;
B = second;
}
}
}
public double Length { get => Point.Distance(A, B); }
public override string ToString() => $"L({A.X},{A.Y})({B.X},{B.Y})";
}
class Line3D(Point3D a, Point3D b) : Line(a, b)
{
public new Point3D A { get; set; } = a;
public new Point3D B { get; set; } = b;
public new double Length { get => Point3D.Distance(A, B); }
}
class Vector
{
public double X { get; set; }
public double Y { get; set; }
public static Vector Zero { get => new(0, 0); }
public Vector(Point end)
{
X = end.X; Y = end.Y;
}
public Vector(double x, double y)
{
X = x; Y = y;
}
public Vector(Line line, bool reverse = false)
{
var x = line.B.X - line.A.X;
var y = line.B.Y - line.A.Y;
if (!reverse)
{
X = x; Y = y;
}
else
{
X = -x; Y = -y;
}
}
public double Magnitude { get => Point.Distance(Point.Zero, new Point(X, Y)); }
public Vector Normalized { get => this / Magnitude; }
public static double Angle(Vector a, Vector b)
{
double dotProduct = a * b; double magnitudeProduct = a.Magnitude * b.Magnitude;
if (magnitudeProduct == 0) return 0;
double cosTheta = dotProduct / magnitudeProduct;
return Math.Acos(cosTheta);
}
public static Vector operator +(Vector a, Vector b) => new(a.X + b.X, a.Y + b.Y);
public static Vector operator -(Vector a, Vector b) => new(a.X - a.X, a.Y - b.Y);
public static double operator *(Vector a, Vector b) => a.X * b.X + a.Y * b.Y;
public static Vector operator *(Vector a, double num) => new(a.X * num, a.Y * num);
public static Vector operator /(Vector a, double num) => new(a.X / num, a.Y / num);
public override string ToString() => $"V({X},{Y})";
}
class Vector3D : Vector
{
public double Z { get; set; }
public new static Vector3D Zero { get => new(0, 0, 0); }
public Vector3D(Point3D end) : base(end)
{
Z = end.Z;
}
public Vector3D(double x, double y, double z) : base(x, y)
{
Z = z;
}
public Vector3D(Line3D line, bool reverse = false) : base(line, reverse)
{
var z = line.B.Z - line.A.Z;
if (reverse)
z = -z;
Z = z;
}
public new double Magnitude { get => Point3D.Distance(new Point3D(0, 0, 0), new Point3D(X, Y, Z)); }
public new Vector3D Normalized { get => this / Magnitude; }
public static double Angle(Vector3D a, Vector3D b)
{
double dotProduct = a * b; double magnitudeProduct = a.Magnitude * b.Magnitude;
if (magnitudeProduct == 0) return 0;
double cosTheta = dotProduct / magnitudeProduct;
return Math.Acos(cosTheta);
}
public static Vector3D operator +(Vector3D a, Vector3D b) => new(a.X + b.X, a.Y + b.Y, a.Z + b.Z);
public static Vector3D operator -(Vector3D a, Vector3D b) => new(a.X - b.X, a.Y - b.Y, a.Z - b.Z);
public static double operator *(Vector3D a, Vector3D b) => a.X * b.X + a.Y * b.Y + a.Z * b.Z;
public static Vector3D operator *(Vector3D a, double num) => new(a.X * num, a.Y * num, a.Z * num);
public static Vector3D operator /(Vector3D a, double num) => new(a.X / num, a.Y / num, a.Z / num);
public override string ToString() => $"V({X},{Y},{Z})";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment