Example #1
0
 public void ToTwoTrangles(out Triangle t1, out Triangle t2)
 {
     {
         Vectorx vt = GetPoint(DirectionType.top)[0];
         Vectorx vb = GetPoint(DirectionType.bottom)[0];
         Vectorx vc = GetOtherPoint(vt.point_2D, vb.point_2D);
         Vector2 v  = vt.point_2D - vb.point_2D;
         float   k  = v.y / v.x;
         if (v.x == 0)
         {
             t1 = new Triangle(new Vectorx(new Vector2(vt.point_2D.x, vc.point_2D.y), Math.Lerp(vt, vb, new Vector2(vt.point_2D.x, vc.point_2D.y))), vc, vt, Object);
             t2 = new Triangle(new Vectorx(new Vector2(vt.point_2D.x, vc.point_2D.y), Math.Lerp(vt, vb, new Vector2(vt.point_2D.x, vc.point_2D.y))), vc, vb, Object);
             t1.SetTrangleType(DirectionType.top);
             t2.SetTrangleType(DirectionType.bottom);
             return;
         }
         {
             t1 = new Triangle(new Vectorx(new Vector2(vt.point_2D.x + (vc.point_2D.y - vt.point_2D.y) / k, vc.point_2D.y)
                                           , Math.Lerp(vt, vb, new Vector2(vt.point_2D.x + (vc.point_2D.y - vt.point_2D.y) / k, vc.point_2D.y))), vc, vt, Object);
             t2 = new Triangle(new Vectorx(new Vector2(vt.point_2D.x + (vc.point_2D.y - vt.point_2D.y) / k, vc.point_2D.y)
                                           , Math.Lerp(vt, vb, new Vector2(vt.point_2D.x + (vc.point_2D.y - vt.point_2D.y) / k, vc.point_2D.y))), vc, vb, Object);
             t1.SetTrangleType(DirectionType.top);
             t2.SetTrangleType(DirectionType.bottom);
             return;
         }
     }
 }
Example #2
0
        public static void DrawLine(Vectorx v1, Vectorx v2)
        {
            Vector2 v = v1.point_2D - v2.point_2D;

            v = new Vector2((int)v.x, (int)v.y);
            if (v.x == 0)
            {
                if (v.y > 0)
                {
                    for (int i = 0; i < v.y; i++)
                    {
                        SetPixel((int)v1.point_2D.x, (int)v1.point_2D.y - i, Math.Lerp(v1.color, v2.color, i / v.y));
                    }
                }
                else
                {
                    for (int i = 0; i < -v.y; i++)
                    {
                        SetPixel((int)v1.point_2D.x, (int)v1.point_2D.y + i, Math.Lerp(v1.color, v2.color, i / -v.y));
                    }
                }
                return;
            }
            float k = v.y / v.x;

            if (v.x > 0)
            {
                for (int i = 0; i < (int)v.x; i++)
                {
                    SetPixel((int)v1.point_2D.x - i, (int)v1.point_2D.y - (int)(i * k), Math.Lerp(v1.color, v2.color, Math.GetLength(v1.point_2D, new Vector2(v1.point_2D.x - i, v1.point_2D.y - (int)(i * k))) / Math.GetLength(v1.point_2D, v2.point_2D)));
                }
            }
            else
            {
                for (int i = 0; i < -(int)v.x; i++)
                {
                    //SetPixel((int)v1.point_2D.x + i, (int)v1.point_2D.y + (int)(i * k), Math.Lerp(v1.color, v2.color, Math.GetLength(v1.point_2D, new Vector2(v1.point_2D.x + i, v1.point_2D.y + (int)(i * k))) / Math.GetLength(v1.point_2D, v2.point_2D)));
                    SetPixel((int)v1.point_2D.x + i, (int)v1.point_2D.y + (int)(i * k), Math.Lerp(v1, v2, new Vector2(v1.point_2D.x + i, v1.point_2D.y + (int)(i * k))));
                }
            }
        }
Example #3
0
 public static Color Lerp(Vectorx v1, Vectorx v2, Vector2 po)
 {
     return(Lerp(v1.color, v2.color, GetLength(v1.point_2D, po) / GetLength(v1.point_2D, v2.point_2D)));
 }
Example #4
0
        public Vectorx[] GetPoint(DirectionType type)
        {
            Vectorx point0 = GetPoint2D(0);
            Vectorx point1 = GetPoint2D(1);
            Vectorx point2 = GetPoint2D(2);

            switch (type)
            {
            case DirectionType.top:
                if (point0.point_2D.y <= point1.point_2D.y && point0.point_2D.y <= point2.point_2D.y)
                {
                    if (point0.point_2D.y == point1.point_2D.y)
                    {
                        return(new Vectorx[]
                        {
                            point0,
                            point1
                        });
                    }
                    if (point0.point_2D.y == point2.point_2D.y)
                    {
                        return(new Vectorx[]
                        {
                            point0,
                            point2
                        });
                    }
                    return(new Vectorx[] { point0 });
                }
                if (point1.point_2D.y < point0.point_2D.y && point1.point_2D.y <= point2.point_2D.y)
                {
                    if (point1.point_2D.y == point2.point_2D.y)
                    {
                        return(new Vectorx[]
                        {
                            point1,
                            point2
                        });
                    }
                    return(new Vectorx[] { point1 });
                }
                if (point2.point_2D.y <= point0.point_2D.y && point2.point_2D.y < point1.point_2D.y)
                {
                    if (point2.point_2D.y == point0.point_2D.y)
                    {
                        return(new Vectorx[]
                        {
                            point0,
                            point1
                        });
                    }
                    return(new Vectorx[] { point2 });
                }
                break;

            case DirectionType.bottom:
                if (point0.point_2D.y >= point1.point_2D.y && point0.point_2D.y >= point2.point_2D.y)
                {
                    if (point0.point_2D.y == point1.point_2D.y)
                    {
                        return(new Vectorx[]
                        {
                            point0,
                            point1
                        });
                    }
                    if (point0.point_2D.y == point2.point_2D.y)
                    {
                        return(new Vectorx[]
                        {
                            point0,
                            point2
                        });
                    }
                    return(new Vectorx[] { point0 });
                }
                if (point1.point_2D.y > point0.point_2D.y && point1.point_2D.y >= point2.point_2D.y)
                {
                    if (point1.point_2D.y == point2.point_2D.y)
                    {
                        return(new Vectorx[]
                        {
                            point1,
                            point2
                        });
                    }
                    return(new Vectorx[] { point1 });
                }
                if (point2.point_2D.y >= point0.point_2D.y && point2.point_2D.y > point1.point_2D.y)
                {
                    if (point2.point_2D.y == point0.point_2D.y)
                    {
                        return(new Vectorx[]
                        {
                            point0,
                            point1
                        });
                    }
                    return(new Vectorx[] { point2 });
                }
                break;

            case DirectionType.left:
                if (point0.point_2D.x <= point1.point_2D.x && point0.point_2D.x <= point2.point_2D.x)
                {
                    if (point0.point_2D.x == point1.point_2D.x)
                    {
                        return(new Vectorx[]
                        {
                            point0,
                            point1
                        });
                    }
                    if (point0.point_2D.x == point2.point_2D.x)
                    {
                        return(new Vectorx[]
                        {
                            point0,
                            point2
                        });
                    }
                    return(new Vectorx[] { point0 });
                }
                if (point1.point_2D.x < point0.point_2D.x && point1.point_2D.x <= point2.point_2D.x)
                {
                    if (point1.point_2D.x == point2.point_2D.x)
                    {
                        return(new Vectorx[]
                        {
                            point1,
                            point2
                        });
                    }
                    return(new Vectorx[] { point1 });
                }
                if (point2.point_2D.x <= point0.point_2D.x && point2.point_2D.x < point1.point_2D.x)
                {
                    if (point2.point_2D.x == point0.point_2D.x)
                    {
                        return(new Vectorx[]
                        {
                            point0,
                            point1
                        });
                    }
                    return(new Vectorx[] { point2 });
                }
                break;

            case DirectionType.right:
                if (point0.point_2D.x >= point1.point_2D.x && point0.point_2D.x >= point2.point_2D.x)
                {
                    if (point0.point_2D.x == point1.point_2D.x)
                    {
                        return(new Vectorx[]
                        {
                            point0,
                            point1
                        });
                    }
                    if (point0.point_2D.x == point2.point_2D.x)
                    {
                        return(new Vectorx[]
                        {
                            point0,
                            point2
                        });
                    }
                    return(new Vectorx[] { point0 });
                }
                if (point1.point_2D.x > point0.point_2D.x && point1.point_2D.x >= point2.point_2D.x)
                {
                    if (point1.point_2D.x == point2.point_2D.x)
                    {
                        return(new Vectorx[]
                        {
                            point1,
                            point2
                        });
                    }
                    return(new Vectorx[] { point1 });
                }
                if (point2.point_2D.x >= point0.point_2D.x && point2.point_2D.x > point1.point_2D.x)
                {
                    if (point2.point_2D.x == point0.point_2D.x)
                    {
                        return(new Vectorx[]
                        {
                            point0,
                            point1
                        });
                    }
                    return(new Vectorx[] { point2 });
                }
                break;

            default:
                break;
            }
            return(new Vectorx[0]);
        }
Example #5
0
 public Triangle(Vectorx v1, Vectorx v2, Vectorx v3, Cube c)
 {
     points2D = new Vectorx[] { v1, v2, v3 };
 }
Example #6
0
        public static void DrawTrangle(Triangle t)
        {
            switch (renderType)
            {
            case RenderType.GouraudShading:
                if (t.GetTrangleType() == DirectionType.top)
                {
                    Vectorx vt = t.GetPoint(DirectionType.top)[0];
                    if (t.GetPoint(DirectionType.left).Length == 2)
                    {
                        Vectorx vr = t.GetPoint(DirectionType.right)[0];
                        Vectorx vl = t.GetOtherPoint(vt.point_2D, vr.point_2D);
                        Vector2 v  = vt.point_2D - vr.point_2D;
                        float   k  = v.y / v.x;
                        for (int y = 0; y < -v.y; y++)
                        {
                            //DrawLine(new Vector2(vl.point_2D.x, vl.point_2D.y - y), new Vector2(vr.point_2D.x - y / k, vl.point_2D.y - y),Math.Lerp());
                            DrawLine(new Vectorx(new Vector2(vl.point_2D.x, vl.point_2D.y - y), Math.Lerp(vl, vt, new Vector2(vl.point_2D.x, vl.point_2D.y - y))),
                                     new Vectorx(new Vector2(vr.point_2D.x - y / k, vl.point_2D.y - y), Math.Lerp(vr, vt, new Vector2(vr.point_2D.x - y / k, vl.point_2D.y - y))));
                        }
                        return;
                    }
                    if (t.GetPoint(DirectionType.right).Length == 2)
                    {
                        Vectorx vl = t.GetPoint(DirectionType.left)[0];
                        Vectorx vr = t.GetOtherPoint(vt.point_2D, vl.point_2D);
                        Vector2 v  = vt.point_2D - vl.point_2D;
                        float   k  = v.y / v.x;
                        for (int y = 0; y < -v.y; y++)
                        {
                            //DrawLine(new Vector2(vl.point_2D.x - y / k, vl.point_2D.y - y), new Vector2(vt.point_2D.x, vl.point_2D.y - y));
                            DrawLine(new Vectorx(new Vector2(vl.point_2D.x - y / k, vl.point_2D.y - y), Math.Lerp(vl, vt, new Vector2(vl.point_2D.x - y / k, vl.point_2D.y - y))),
                                     new Vectorx(new Vector2(vt.point_2D.x, vl.point_2D.y - y), Math.Lerp(vr, vt, new Vector2(vt.point_2D.x, vl.point_2D.y - y))));
                        }
                        return;
                    }
                    {
                        Vectorx vl = t.GetPoint(DirectionType.bottom)[0];
                        Vectorx vr = t.GetPoint(DirectionType.bottom)[1];
                        Vector2 v1 = vt.point_2D - vl.point_2D;
                        Vector2 v2 = vt.point_2D - vr.point_2D;
                        float   k1 = v1.y / v1.x;
                        float   k2 = v2.y / v2.x;
                        for (int y = 0; y < -v1.y; y++)
                        {
                            //DrawLine(new Vector2(vl.point_2D.x - y / k1, vl.point_2D.y - y), new Vector2(vr.point_2D.x - y / k2, vl.point_2D.y - y));
                            DrawLine(new Vectorx(new Vector2(vl.point_2D.x - y / k1, vl.point_2D.y - y), Math.Lerp(vl, vt, new Vector2(vl.point_2D.x - y / k1, vl.point_2D.y - y))),
                                     new Vectorx(new Vector2(vr.point_2D.x - y / k2, vl.point_2D.y - y), Math.Lerp(vr, vt, new Vector2(vr.point_2D.x - y / k2, vl.point_2D.y - y))));
                        }
                        return;
                    }
                }
                if (t.GetTrangleType() == DirectionType.bottom)
                {
                    Vectorx vb = t.GetPoint(DirectionType.bottom)[0];
                    if (t.GetPoint(DirectionType.left).Length == 2)
                    {
                        Vectorx vr = t.GetPoint(DirectionType.right)[0];
                        Vectorx vl = t.GetOtherPoint(vb.point_2D, vr.point_2D);
                        Vector2 v  = vb.point_2D - vr.point_2D;
                        float   k  = v.y / v.x;
                        for (int y = 0; y < v.y; y++)
                        {
                            //DrawLine(new Vector2(vl.point_2D.x, vl.point_2D.y + y), new Vector2(vr.point_2D.x + y / k, vl.point_2D.y + y));
                            DrawLine(new Vectorx(new Vector2(vl.point_2D.x, vl.point_2D.y + y), Math.Lerp(vl, vb, new Vector2(vl.point_2D.x, vl.point_2D.y + y))),
                                     new Vectorx(new Vector2(vr.point_2D.x + y / k, vl.point_2D.y + y), Math.Lerp(vr, vb, new Vector2(vr.point_2D.x + y / k, vl.point_2D.y + y))));
                        }
                        return;
                    }
                    if (t.GetPoint(DirectionType.right).Length == 2)
                    {
                        Vectorx vl = t.GetPoint(DirectionType.left)[0];
                        Vectorx vr = t.GetOtherPoint(vb.point_2D, vl.point_2D);
                        Vector2 v  = vb.point_2D - vl.point_2D;
                        float   k  = v.y / v.x;
                        for (int y = 0; y < v.y; y++)
                        {
                            //DrawLine(new Vector2(vl.point_2D.x + y / k, vl.point_2D.y + y), new Vector2(vb.point_2D.x, vl.point_2D.y + y));
                            DrawLine(new Vectorx(new Vector2(vl.point_2D.x + y / k, vl.point_2D.y + y), Math.Lerp(vl, vb, new Vector2(vl.point_2D.x + y / k, vl.point_2D.y + y))),
                                     new Vectorx(new Vector2(vb.point_2D.x, vl.point_2D.y + y), Math.Lerp(vr, vb, new Vector2(vb.point_2D.x, vl.point_2D.y + y))));
                        }
                        return;
                    }
                    {
                        Vectorx vl = t.GetPoint(DirectionType.top)[0];
                        Vectorx vr = t.GetPoint(DirectionType.top)[1];
                        Vector2 v1 = vb.point_2D - vl.point_2D;
                        Vector2 v2 = vb.point_2D - vr.point_2D;
                        float   k1 = v1.y / v1.x;
                        float   k2 = v2.y / v2.x;
                        for (int y = 0; y < v1.y; y++)
                        {
                            //DrawLine(new Vector2(vl.x + y / k1, vl.y + y), new Vector2(vr.x + y / k2, vl.y + y));
                            DrawLine(new Vectorx(new Vector2(vl.point_2D.x + y / k1, vl.point_2D.y + y), Math.Lerp(vl, vb, new Vector2(vl.point_2D.x + y / k1, vl.point_2D.y + y))),
                                     new Vectorx(new Vector2(vr.point_2D.x + y / k2, vl.point_2D.y + y), Math.Lerp(vr, vb, new Vector2(vr.point_2D.x + y / k2, vl.point_2D.y + y))));
                        }
                        return;
                    }
                }
                break;

            case RenderType.NoShading:
                if (t.GetTrangleType() == DirectionType.top)
                {
                    Vector2 vt = t.GetPoint(DirectionType.top)[0].point_2D;
                    if (t.GetPoint(DirectionType.left).Length == 2)
                    {
                        Vector2 vr = t.GetPoint(DirectionType.right)[0].point_2D;
                        Vector2 vl = new Vector2(vt.x, vr.y);
                        Vector2 v  = vt - vr;
                        float   k  = v.y / v.x;
                        for (int y = 0; y < -v.y; y++)
                        {
                            DrawLine(new Vector2(vl.x, vl.y - y), new Vector2(vr.x - y / k, vl.y - y));
                        }
                        return;
                    }
                    if (t.GetPoint(DirectionType.right).Length == 2)
                    {
                        Vector2 vl = t.GetPoint(DirectionType.left)[0].point_2D;
                        Vector2 vr = new Vector2(vt.x, vl.y);
                        Vector2 v  = vt - vl;
                        float   k  = v.y / v.x;
                        for (int y = 0; y < -v.y; y++)
                        {
                            DrawLine(new Vector2(vl.x - y / k, vl.y - y), new Vector2(vt.x, vl.y - y));
                        }
                        return;
                    }
                    {
                        Vector2 vl = t.GetPoint(DirectionType.bottom)[0].point_2D;
                        Vector2 vr = t.GetPoint(DirectionType.bottom)[1].point_2D;
                        Vector2 v1 = vt - vl;
                        Vector2 v2 = vt - vr;
                        float   k1 = v1.y / v1.x;
                        float   k2 = v2.y / v2.x;
                        for (int y = 0; y < -v1.y; y++)
                        {
                            DrawLine(new Vector2(vl.x - y / k1, vl.y - y), new Vector2(vr.x - y / k2, vl.y - y));
                        }
                        return;
                    }
                }
                if (t.GetTrangleType() == DirectionType.bottom)
                {
                    Vector2 vb = t.GetPoint(DirectionType.bottom)[0].point_2D;
                    if (t.GetPoint(DirectionType.left).Length == 2)
                    {
                        Vector2 vr = t.GetPoint(DirectionType.right)[0].point_2D;
                        Vector2 vl = new Vector2(vb.x, vr.y);
                        Vector2 v  = vb - vr;
                        float   k  = v.y / v.x;
                        for (int y = 0; y < v.y; y++)
                        {
                            DrawLine(new Vector2(vl.x, vl.y + y), new Vector2(vr.x + y / k, vl.y + y));
                        }
                        return;
                    }
                    if (t.GetPoint(DirectionType.right).Length == 2)
                    {
                        Vector2 vl = t.GetPoint(DirectionType.left)[0].point_2D;
                        Vector2 vr = new Vector2(vb.x, vl.y);
                        Vector2 v  = vb - vl;
                        float   k  = v.y / v.x;
                        for (int y = 0; y < v.y; y++)
                        {
                            DrawLine(new Vector2(vl.x + y / k, vl.y + y), new Vector2(vb.x, vl.y + y));
                        }
                        return;
                    }
                    {
                        Vector2 vl = t.GetPoint(DirectionType.top)[0].point_2D;
                        Vector2 vr = t.GetPoint(DirectionType.top)[1].point_2D;
                        Vector2 v1 = vb - vl;
                        Vector2 v2 = vb - vr;
                        float   k1 = v1.y / v1.x;
                        float   k2 = v2.y / v2.x;
                        for (int y = 0; y < v1.y; y++)
                        {
                            DrawLine(new Vector2(vl.x + y / k1, vl.y + y), new Vector2(vr.x + y / k2, vl.y + y));
                        }
                        return;
                    }
                }
                break;

            default:
                break;
            }
            t.ToTwoTrangles(out Triangle t1, out Triangle t2);
            DrawTrangle(t1);
            DrawTrangle(t2);
        }