-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vector.cs
50 lines (46 loc) · 1.76 KB
/
Vector.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
using System;
namespace Mandelmesh
{
public struct Vector
{
public double X { get; }
public double Y { get; }
public double Z { get; }
public Vector(double x, double y, double z)
{
X = x;
Y = y;
Z = z;
}
public static Vector operator +(Vector l, Vector r) => new Vector(l.X + r.X, l.Y + r.Y, l.Z + r.Z);
public static Vector operator -(Vector l, Vector r) => new Vector(l.X - r.X, l.Y - r.Y, l.Z - r.Z);
public static Vector operator -(Vector v) => new Vector(-v.X, -v.Y, -v.Z);
public static Vector operator *(Vector l, Vector r) => new Vector(l.X * r.X, l.Y * r.Y, l.Z * r.Z);
public static Vector operator *(Vector l, double r) => new Vector(l.X * r, l.Y * r, l.Z * r);
public static Vector operator /(Vector l, double r) => new Vector(l.X / r, l.Y / r, l.Z / r);
public static Vector operator /(Vector l, Vector r) => new Vector(l.X / r.X, l.Y / r.Y, l.Z / r.Z);
public static double Dot(Vector l, Vector r) => l.X * r.X + l.Y * r.Y + l.Z * r.Z;
public double Length2() => X * X + Y * Y + Z * Z;
public double Length() => Math.Sqrt(Length2());
public Vector Normalized() => this / Length();
public Vector Clamp(double min, double max) => new Vector(X.Clamp(min, max), Y.Clamp(min, max), Z.Clamp(min, max));
}
public static class Ext
{
public static double Clamp(this double value, double min, double max)
{
if (value < min)
{
return min;
}
else if (value > max)
{
return max;
}
else
{
return value;
}
}
}
}