-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTriVec2D.cs
64 lines (58 loc) · 1.72 KB
/
TriVec2D.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
namespace _3D_Renderer
{
class TriVec2D
{
//Triangle of 2d vectors
public Vec2D[] Verts { get; }
public TriVec2D(Vec2D Vert1, Vec2D Vert2, Vec2D Vert3)
{
Verts = new Vec2D[] { Vert1, Vert2, Vert3 };
}
public Vec2D Vert1
{
get { return Verts[0]; }
}
public Vec2D Vert2
{
get { return Verts[1]; }
}
public Vec2D Vert3
{
get { return Verts[2]; }
}
public Vec2D Edge1
{
get { return Vert2 - Vert1; }
}
public Vec2D Edge2
{
get { return Vert3 - Vert1; }
}
public double SignedArea
{
get { return Vec2D.PerpDotProd(Edge1, Edge2); }
}
public double Area
{
get { return Vec2D.AreaBetween(Edge1, Edge2); }
}
public Bary ComputeInternalBaryCoord(Vec2D Point)
{
double U = Vec2D.AreaBetween(Point - Vert2, Point - Vert3) / Area;
double V = Vec2D.AreaBetween(Point - Vert3, Point - Vert1) / Area;
double W = 1 - U - V;
return new Bary(U, V, W);
}
public Bary ComputeBaryCoord(Vec2D Point)
{
double U = Vec2D.PerpDotProd(Point - Vert2, Point - Vert3) / SignedArea;
double V = Vec2D.PerpDotProd(Point - Vert3, Point - Vert1) / SignedArea;
double W = 1 - U - V;
return new Bary(U, V, W);
}
public Vec2D BaryInterp(Bary BarycentricCoord)
{
return Vert1.ScalarMult(BarycentricCoord.U) + Vert2.ScalarMult(BarycentricCoord.V) + Vert3.ScalarMult(BarycentricCoord.W);
}
}
}