public void FillTriangle(Vertex v1, Vertex v2, Vertex v3) { var verts = SortVertex(v1, v2, v3); if (MathUtility.IsEqual(verts[1].pos.y, verts[2].pos.y)) { FillBottomFlatTriangle(verts[0], verts[1], verts[2]); } else if (verts[0].pos.y == verts[1].pos.y) { FillTopFlatTriangle(verts[0], verts[1], verts[2]); } else //将三角面切割成平顶平底两个三角面绘制 { float t = (verts[1].pos.y - verts[0].pos.y) / (verts[2].pos.y - verts[0].pos.y); Vertex v4 = TransformUtility.LerpVertexInScreenSpace(verts[0], verts[2], t); v4.color = ColorUtility.Lerp(verts[0], verts[1], verts[2], v4); float y = verts[1].pos.y; float x = (int)(verts[0].pos.x + (((float)verts[1].pos.y - verts[0].pos.y) / ((float)verts[2].pos.y - verts[0].pos.y)) * (verts[2].pos.x - verts[0].pos.x)); v4.pos = new Vector(x, y, verts[1].pos.z); FillBottomFlatTriangle(verts[0], verts[1], v4); FillTopFlatTriangle(verts[1], v4, verts[2]); //测试用 //DrawTriangleLine(v1.pos, v2.pos, v3.pos, Color.Black); } }
public void FillTriangle(Vector v1, Vector v2, Vector v3, Color color = default(Color)) { int lineState = CheckInSameLineState(v1, v2, v3); if (lineState != 0) { var vertPoint = SortPoint(v1, v2, v3, lineState == -1); DrawLine(vertPoint[0], vertPoint[2], color); return; } var verts = SortPoint(v1, v2, v3); if (MathUtility.IsEqual(verts[1].y, verts[2].y)) { FillBottomFlatTriangle(verts[0], verts[1], verts[2], color); } else if (verts[0].y == verts[1].y) { FillTopFlatTriangle(verts[0], verts[1], verts[2], color); } else //将三角面切割成平顶平底两个三角面绘制 { Vector v4 = Vector.Zero; v4.y = verts[1].y; v4.x = (int)(verts[0].x + (((float)verts[1].y - verts[0].y) / ((float)verts[2].y - verts[0].y)) * (verts[2].x - verts[0].x)); FillBottomFlatTriangle(verts[0], verts[1], v4, color); FillTopFlatTriangle(verts[1], v4, verts[2], color); DrawTriangleLine(v1, v2, v3, Color.Black); } }
public static bool operator !=(Vector a, Vector b) { return(!MathUtility.IsEqual(a.x, b.x) || !MathUtility.IsEqual(a.y, b.y) || !MathUtility.IsEqual(a.z, b.z) ); }
public static bool operator ==(Vector a, Vector b) { return(MathUtility.IsEqual(a.x, b.x) && MathUtility.IsEqual(a.y, b.y) && MathUtility.IsEqual(a.z, b.z) ); }
// -1 : 同行 , 0 :不等 1 :同列 public int CheckInSameLineState(Vector v1, Vector v2, Vector v3) { if (MathUtility.IsEqual(v1.y - v2.y - v3.y, 0)) { return(1); } else if (MathUtility.IsEqual(v1.x - v2.x - v3.x, 0)) { return(-1); } return(0); }
public List <Vertex> SortVertex(Vertex v1, Vertex v2, Vertex v3, bool sortByX = false) { List <Vertex> result = new List <Vertex>() { v1, v2, v3 }; result.Sort((a, b) => { float value1 = sortByX ? a.pos.x : a.pos.y; float value2 = sortByX ? b.pos.x : b.pos.y; if (value1 > value2) { return(1); } else if (MathUtility.IsEqual(value1, value2)) { return(0); } return(-1); }); return(result); }
public List <Vector> SortPoint(Vector v1, Vector v2, Vector v3, bool sortByX = false) { List <Vector> result = new List <Vector>() { v1, v2, v3 }; result.Sort((a, b) => { float value1 = sortByX ? a.x : a.y; float value2 = sortByX ? b.x : b.y; if (value1 > value2) { return(1); } else if (MathUtility.IsEqual(value1, value2)) { return(0); } return(-1); }); return(result); }